1 /*
2 ** Zabbix
3 ** Copyright (C) 2001-2021 Zabbix SIA
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 **/
19 
20 #include <assert.h>
21 
22 #include "common.h"
23 #include "log.h"
24 
25 #include "db.h"
26 #include "zbxjson.h"
27 #include "zbxtasks.h"
28 
29 /******************************************************************************
30  *                                                                            *
31  * Function: zbx_tm_get_remote_tasks                                          *
32  *                                                                            *
33  * Purpose: get tasks scheduled to be executed on the server                  *
34  *                                                                            *
35  * Parameters: tasks        - [OUT] the tasks to execute                      *
36  *             proxy_hostid - [IN] (ignored)                                  *
37  *                                                                            *
38  * Comments: This function is used by proxy to get tasks to be sent to the    *
39  *           server.                                                          *
40  *                                                                            *
41  ******************************************************************************/
zbx_tm_get_remote_tasks(zbx_vector_ptr_t * tasks,zbx_uint64_t proxy_hostid)42 void	zbx_tm_get_remote_tasks(zbx_vector_ptr_t *tasks, zbx_uint64_t proxy_hostid)
43 {
44 	DB_RESULT	result;
45 	DB_ROW		row;
46 
47 	ZBX_UNUSED(proxy_hostid);
48 
49 	result = DBselect(
50 			"select t.taskid,t.type,t.clock,t.ttl,"
51 				"r.status,r.parent_taskid,r.info,"
52 				"tr.status,tr.parent_taskid,tr.info"
53 			" from task t"
54 			" left join task_remote_command_result r"
55 				" on t.taskid=r.taskid"
56 			" left join task_result tr"
57 				" on t.taskid=tr.taskid"
58 			" where t.status=%d"
59 				" and t.type in (%d,%d)"
60 			" order by t.taskid",
61 			ZBX_TM_STATUS_NEW, ZBX_TM_TASK_REMOTE_COMMAND_RESULT, ZBX_TM_TASK_DATA_RESULT);
62 
63 	while (NULL != (row = DBfetch(result)))
64 	{
65 		zbx_uint64_t	taskid, parent_taskid;
66 		zbx_tm_task_t	*task;
67 
68 		ZBX_STR2UINT64(taskid, row[0]);
69 		task = zbx_tm_task_create(taskid, atoi(row[1]), ZBX_TM_STATUS_NEW, atoi(row[2]), atoi(row[3]), 0);
70 
71 		switch (task->type)
72 		{
73 			case ZBX_TM_TASK_REMOTE_COMMAND_RESULT:
74 				if (SUCCEED == DBis_null(row[4]))
75 				{
76 					zbx_free(task);
77 					continue;
78 				}
79 
80 				ZBX_DBROW2UINT64(parent_taskid, row[5]);
81 
82 				task->data = zbx_tm_remote_command_result_create(parent_taskid, atoi(row[4]), row[6]);
83 				break;
84 			case ZBX_TM_TASK_DATA_RESULT:
85 				if (SUCCEED == DBis_null(row[7]))
86 				{
87 					zbx_free(task);
88 					continue;
89 				}
90 
91 				ZBX_DBROW2UINT64(parent_taskid, row[8]);
92 
93 				task->data = zbx_tm_data_result_create(parent_taskid, atoi(row[7]), row[9]);
94 				break;
95 		}
96 
97 		zbx_vector_ptr_append(tasks, task);
98 	}
99 
100 	DBfree_result(result);
101 }
102 
103