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 #ifndef ZABBIX_ZBXTASKS_H
21 #define ZABBIX_ZBXTASKS_H
22 
23 #include "zbxalgo.h"
24 #include "zbxjson.h"
25 
26 #define ZBX_TASK_UPDATE_FREQUENCY	1
27 
28 #define ZBX_REMOTE_COMMAND_TTL		(SEC_PER_MIN * 10)
29 #define ZBX_DATA_TTL			30
30 
31 /* task manager task types */
32 #define ZBX_TM_TASK_UNDEFINED				0
33 #define ZBX_TM_TASK_CLOSE_PROBLEM			1
34 #define ZBX_TM_TASK_REMOTE_COMMAND			2
35 #define ZBX_TM_TASK_REMOTE_COMMAND_RESULT		3
36 #define ZBX_TM_TASK_ACKNOWLEDGE				4
37 #define ZBX_TM_TASK_UPDATE_EVENTNAMES			5
38 #define ZBX_TM_TASK_CHECK_NOW				6
39 #define ZBX_TM_TASK_DATA				7
40 #define ZBX_TM_TASK_DATA_RESULT				8
41 
42 /* task manager task states */
43 #define ZBX_TM_STATUS_NEW			1
44 #define ZBX_TM_STATUS_INPROGRESS		2
45 #define ZBX_TM_STATUS_DONE			3
46 #define ZBX_TM_STATUS_EXPIRED			4
47 
48 /* task data type */
49 #define ZBX_TM_DATA_TYPE_TEST_ITEM		0
50 #define ZBX_TM_DATA_TYPE_DIAGINFO		1
51 
52 /* the time period after which finished (done/expired) tasks are removed */
53 #define ZBX_TM_CLEANUP_TASK_AGE			SEC_PER_DAY
54 
55 typedef struct
56 {
57 	int		command_type;
58 	char		*command;
59 	int		execute_on;
60 	int		port;
61 	int		authtype;
62 	char		*username;
63 	char		*password;
64 	char		*publickey;
65 	char		*privatekey;
66 	zbx_uint64_t	parent_taskid;
67 	zbx_uint64_t	hostid;
68 	zbx_uint64_t	alertid;
69 }
70 zbx_tm_remote_command_t;
71 
72 typedef struct
73 {
74 	int		status;
75 	char		*info;
76 	zbx_uint64_t	parent_taskid;
77 }
78 zbx_tm_remote_command_result_t;
79 
80 typedef struct
81 {
82 	zbx_uint64_t	itemid;
83 }
84 zbx_tm_check_now_t;
85 
86 typedef struct
87 {
88 	zbx_uint64_t	parent_taskid;
89 	char		*data;
90 	int		type;
91 }
92 zbx_tm_data_t;
93 
94 typedef struct
95 {
96 	int		status;
97 	char		*info;
98 	zbx_uint64_t	parent_taskid;
99 }
100 zbx_tm_data_result_t;
101 
102 typedef struct
103 {
104 	/* the task identifier */
105 	zbx_uint64_t	taskid;
106 	/* the target proxy hostid or 0 if the task must be on server, ignored by proxy */
107 	zbx_uint64_t	proxy_hostid;
108 	/* the task type (ZBX_TM_TASK_* defines) */
109 	unsigned char	type;
110 	/* the task status (ZBX_TM_STATUS_* defines) */
111 	unsigned char	status;
112 	/* the task creation time */
113 	int		clock;
114 	/* the task expiration period in seconds */
115 	int		ttl;
116 
117 	/* the task data, depending on task type */
118 	void		*data;
119 }
120 zbx_tm_task_t;
121 
122 
123 zbx_tm_task_t	*zbx_tm_task_create(zbx_uint64_t taskid, unsigned char type, unsigned char status, int clock, int ttl,
124 		zbx_uint64_t proxy_hostid);
125 void	zbx_tm_task_clear(zbx_tm_task_t *task);
126 void	zbx_tm_task_free(zbx_tm_task_t *task);
127 
128 zbx_tm_remote_command_t	*zbx_tm_remote_command_create(int command_type, const char *command, int execute_on, int port,
129 		int authtype, const char *username, const char *password, const char *publickey, const char *privatekey,
130 		zbx_uint64_t parent_taskid, zbx_uint64_t hostid, zbx_uint64_t alertid);
131 
132 zbx_tm_remote_command_result_t	*zbx_tm_remote_command_result_create(zbx_uint64_t parent_taskid, int status,
133 		const char *info);
134 
135 zbx_tm_check_now_t	*zbx_tm_check_now_create(zbx_uint64_t itemid);
136 
137 zbx_tm_data_t		*zbx_tm_data_create(zbx_uint64_t parent_taskid, const char *str, int len, int type);
138 zbx_tm_data_result_t	*zbx_tm_data_result_create(zbx_uint64_t parent_taskid, int status, const char *info);
139 
140 int	zbx_tm_execute_task_data(const char *data, int len, zbx_uint64_t proxy_hostid, char **info);
141 
142 void	zbx_tm_save_tasks(zbx_vector_ptr_t *tasks);
143 int	zbx_tm_save_task(zbx_tm_task_t *task);
144 
145 void	zbx_tm_get_proxy_tasks(zbx_vector_ptr_t *tasks, zbx_uint64_t proxy_hostid);
146 void	zbx_tm_update_task_status(zbx_vector_ptr_t *tasks, int status);
147 void	zbx_tm_json_serialize_tasks(struct zbx_json *json, const zbx_vector_ptr_t *tasks);
148 void	zbx_tm_json_deserialize_tasks(const struct zbx_json_parse *jp, zbx_vector_ptr_t *tasks);
149 
150 /* separate implementation for proxy and server */
151 void	zbx_tm_get_remote_tasks(zbx_vector_ptr_t *tasks, zbx_uint64_t proxy_hostid);
152 
153 int	zbx_tm_get_diaginfo(const struct zbx_json_parse *jp, char **info);
154 
155 #endif
156