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_DB_H
21 #define ZABBIX_DB_H
22 
23 #include "common.h"
24 #include "zbxalgo.h"
25 #include "zbxdb.h"
26 #include "dbschema.h"
27 
28 extern char	*CONFIG_DBHOST;
29 extern char	*CONFIG_DBNAME;
30 extern char	*CONFIG_DBSCHEMA;
31 extern char	*CONFIG_DBUSER;
32 extern char	*CONFIG_DBPASSWORD;
33 extern char	*CONFIG_DBSOCKET;
34 extern int	CONFIG_DBPORT;
35 extern int	CONFIG_HISTSYNCER_FORKS;
36 extern int	CONFIG_UNAVAILABLE_DELAY;
37 
38 typedef enum
39 {
40 	GRAPH_TYPE_NORMAL = 0,
41 	GRAPH_TYPE_STACKED = 1
42 }
43 zbx_graph_types;
44 
45 typedef enum
46 {
47 	CALC_FNC_MIN = 1,
48 	CALC_FNC_AVG = 2,
49 	CALC_FNC_MAX = 4,
50 	CALC_FNC_ALL = 7
51 }
52 zbx_graph_item_calc_function;
53 
54 typedef enum
55 {
56 	GRAPH_ITEM_SIMPLE = 0,
57 	GRAPH_ITEM_AGGREGATED = 1
58 }
59 zbx_graph_item_type;
60 
61 struct	_DC_TRIGGER;
62 
63 #define ZBX_DB_CONNECT_NORMAL	0
64 #define ZBX_DB_CONNECT_EXIT	1
65 #define ZBX_DB_CONNECT_ONCE	2
66 
67 /* type of database */
68 #define ZBX_DB_UNKNOWN	0
69 #define ZBX_DB_SERVER	1
70 #define ZBX_DB_PROXY	2
71 
72 #define TRIGGER_URL_LEN			255
73 #define TRIGGER_DESCRIPTION_LEN		255
74 #define TRIGGER_EXPRESSION_LEN		2048
75 #define TRIGGER_EXPRESSION_LEN_MAX	(TRIGGER_EXPRESSION_LEN + 1)
76 #define TRIGGER_ERROR_LEN		128
77 #if defined(HAVE_IBM_DB2) || defined(HAVE_ORACLE)
78 #	define TRIGGER_COMMENTS_LEN	2048
79 #else
80 #	define TRIGGER_COMMENTS_LEN	65535
81 #endif
82 
83 #define GROUP_NAME_LEN			64
84 
85 #define HOST_HOST_LEN			MAX_ZBX_HOSTNAME_LEN
86 #define HOST_HOST_LEN_MAX		(HOST_HOST_LEN + 1)
87 #define HOST_NAME_LEN			128
88 #define HOST_ERROR_LEN			2048
89 #define HOST_ERROR_LEN_MAX		(HOST_ERROR_LEN + 1)
90 #define HOST_IPMI_USERNAME_LEN		16
91 #define HOST_IPMI_USERNAME_LEN_MAX	(HOST_IPMI_USERNAME_LEN + 1)
92 #define HOST_IPMI_PASSWORD_LEN		20
93 #define HOST_IPMI_PASSWORD_LEN_MAX	(HOST_IPMI_PASSWORD_LEN + 1)
94 
95 #define INTERFACE_DNS_LEN		64
96 #define INTERFACE_DNS_LEN_MAX		(INTERFACE_DNS_LEN + 1)
97 #define INTERFACE_IP_LEN		64
98 #define INTERFACE_IP_LEN_MAX		(INTERFACE_IP_LEN + 1)
99 #define INTERFACE_ADDR_LEN		64	/* MAX(INTERFACE_DNS_LEN,INTERFACE_IP_LEN) */
100 #define INTERFACE_ADDR_LEN_MAX		(INTERFACE_ADDR_LEN + 1)
101 #define INTERFACE_PORT_LEN		64
102 #define INTERFACE_PORT_LEN_MAX		(INTERFACE_PORT_LEN + 1)
103 
104 #define ITEM_NAME_LEN			255
105 #define ITEM_KEY_LEN			255
106 #define ITEM_UNITS_LEN			255
107 #define ITEM_SNMP_COMMUNITY_LEN		64
108 #define ITEM_SNMP_COMMUNITY_LEN_MAX	(ITEM_SNMP_COMMUNITY_LEN + 1)
109 #define ITEM_SNMP_OID_LEN		255
110 #define ITEM_SNMP_OID_LEN_MAX		(ITEM_SNMP_OID_LEN + 1)
111 #define ITEM_ERROR_LEN			2048
112 #define ITEM_ERROR_LEN_MAX		(ITEM_ERROR_LEN + 1)
113 #define ITEM_TRAPPER_HOSTS_LEN		255
114 #define ITEM_TRAPPER_HOSTS_LEN_MAX	(ITEM_TRAPPER_HOSTS_LEN + 1)
115 #define ITEM_SNMPV3_SECURITYNAME_LEN		64
116 #define ITEM_SNMPV3_SECURITYNAME_LEN_MAX	(ITEM_SNMPV3_SECURITYNAME_LEN + 1)
117 #define ITEM_SNMPV3_AUTHPASSPHRASE_LEN		64
118 #define ITEM_SNMPV3_AUTHPASSPHRASE_LEN_MAX	(ITEM_SNMPV3_AUTHPASSPHRASE_LEN + 1)
119 #define ITEM_SNMPV3_PRIVPASSPHRASE_LEN		64
120 #define ITEM_SNMPV3_PRIVPASSPHRASE_LEN_MAX	(ITEM_SNMPV3_PRIVPASSPHRASE_LEN + 1)
121 #define ITEM_SNMPV3_CONTEXTNAME_LEN		255
122 #define ITEM_SNMPV3_CONTEXTNAME_LEN_MAX		(ITEM_SNMPV3_CONTEXTNAME_LEN + 1)
123 #define ITEM_LOGTIMEFMT_LEN		64
124 #define ITEM_LOGTIMEFMT_LEN_MAX		(ITEM_LOGTIMEFMT_LEN + 1)
125 #define ITEM_DELAY_FLEX_LEN		255
126 #define ITEM_DELAY_FLEX_LEN_MAX		(ITEM_DELAY_FLEX_LEN + 1)
127 #define ITEM_IPMI_SENSOR_LEN		128
128 #define ITEM_IPMI_SENSOR_LEN_MAX	(ITEM_IPMI_SENSOR_LEN + 1)
129 #define ITEM_USERNAME_LEN		64
130 #define ITEM_USERNAME_LEN_MAX		(ITEM_USERNAME_LEN + 1)
131 #define ITEM_PASSWORD_LEN		64
132 #define ITEM_PASSWORD_LEN_MAX		(ITEM_PASSWORD_LEN + 1)
133 #define ITEM_PUBLICKEY_LEN		64
134 #define ITEM_PUBLICKEY_LEN_MAX		(ITEM_PUBLICKEY_LEN + 1)
135 #define ITEM_PRIVATEKEY_LEN		64
136 #define ITEM_PRIVATEKEY_LEN_MAX		(ITEM_PRIVATEKEY_LEN + 1)
137 #if defined(HAVE_IBM_DB2) || defined(HAVE_ORACLE)
138 #	define ITEM_PARAM_LEN		2048
139 #	define ITEM_DESCRIPTION_LEN	2048
140 #else
141 #	define ITEM_PARAM_LEN		65535
142 #	define ITEM_DESCRIPTION_LEN	65535
143 #endif
144 
145 #define HISTORY_STR_VALUE_LEN		255
146 #ifdef HAVE_IBM_DB2
147 #	define HISTORY_TEXT_VALUE_LEN	2048
148 #	define HISTORY_LOG_VALUE_LEN	2048
149 #else
150 #	define HISTORY_TEXT_VALUE_LEN	65535
151 #	define HISTORY_LOG_VALUE_LEN	65535
152 #endif
153 
154 #define HISTORY_LOG_SOURCE_LEN		64
155 #define HISTORY_LOG_SOURCE_LEN_MAX	(HISTORY_LOG_SOURCE_LEN + 1)
156 
157 #define ALERT_ERROR_LEN			128
158 #define ALERT_ERROR_LEN_MAX		(ALERT_ERROR_LEN + 1)
159 
160 #define GRAPH_NAME_LEN			128
161 
162 #define GRAPH_ITEM_COLOR_LEN		6
163 #define GRAPH_ITEM_COLOR_LEN_MAX	(GRAPH_ITEM_COLOR_LEN + 1)
164 
165 #define DSERVICE_KEY_LEN		255
166 #define DSERVICE_VALUE_LEN		255
167 
168 #define HTTPTEST_HTTP_USER_LEN		64
169 #define HTTPTEST_HTTP_PASSWORD_LEN	64
170 
171 #define PROXY_DHISTORY_KEY_LEN		255
172 #define PROXY_DHISTORY_VALUE_LEN	255
173 
174 #define ZBX_SQL_ITEM_FIELDS	"i.itemid,i.key_,h.host,i.type,i.history,i.hostid,i.value_type,i.delta,"	\
175 				"i.units,i.multiplier,i.formula,i.state,i.valuemapid,i.trends,i.data_type"
176 #define ZBX_SQL_ITEM_TABLES	"hosts h,items i"
177 #define ZBX_SQL_TIME_FUNCTIONS	"'nodata','date','dayofmonth','dayofweek','time','now'"
178 #define ZBX_SQL_ITEM_FIELDS_NUM	15
179 #define ZBX_SQL_ITEM_SELECT	ZBX_SQL_ITEM_FIELDS " from " ZBX_SQL_ITEM_TABLES
180 
181 #ifdef HAVE_ORACLE
182 #define	DBbegin_multiple_update(sql, sql_alloc, sql_offset)	zbx_strcpy_alloc(sql, sql_alloc, sql_offset, "begin\n")
183 #define	DBend_multiple_update(sql, sql_alloc, sql_offset)	zbx_strcpy_alloc(sql, sql_alloc, sql_offset, "end;")
184 
185 #define	ZBX_SQL_STRCMP		"%s%s%s"
186 #define	ZBX_SQL_STRVAL_EQ(str)	'\0' != *str ? "='"  : "",		\
187 				'\0' != *str ? str   : " is null",	\
188 				'\0' != *str ? "'"   : ""
189 #define	ZBX_SQL_STRVAL_NE(str)	'\0' != *str ? "<>'" : "",		\
190 				'\0' != *str ? str   : " is not null",	\
191 				'\0' != *str ? "'"   : ""
192 #else
193 #define	DBbegin_multiple_update(sql, sql_alloc, sql_offset)
194 #define	DBend_multiple_update(sql, sql_alloc, sql_offset)
195 
196 #define	ZBX_SQL_STRCMP		"%s'%s'"
197 #define	ZBX_SQL_STRVAL_EQ(str)	"=", str
198 #define	ZBX_SQL_STRVAL_NE(str)	"<>", str
199 #endif
200 
201 #define ZBX_SQL_NULLCMP(f1, f2)	"((" f1 " is null and " f2 " is null) or " f1 "=" f2 ")"
202 
203 #define ZBX_DBROW2UINT64(uint, row)	if (SUCCEED == DBis_null(row))		\
204 						uint = 0;			\
205 					else					\
206 						is_uint64(row, &uint)
207 
208 #define ZBX_MAX_SQL_LEN		65535
209 
210 #define ZBX_DB_MAX_ID	(zbx_uint64_t)__UINT64_C(0x7fffffffffffffff)
211 
212 typedef struct
213 {
214 	zbx_uint64_t	druleid;
215 	zbx_uint64_t	unique_dcheckid;
216 	char		*iprange;
217 	char		*name;
218 }
219 DB_DRULE;
220 
221 typedef struct
222 {
223 	zbx_uint64_t	dcheckid;
224 	char		*ports;
225 	char		*key_;
226 	char		*snmp_community;
227 	char		*snmpv3_securityname;
228 	char		*snmpv3_authpassphrase;
229 	char		*snmpv3_privpassphrase;
230 	char		*snmpv3_contextname;
231 	int		type;
232 	unsigned char	snmpv3_securitylevel;
233 	unsigned char	snmpv3_authprotocol;
234 	unsigned char	snmpv3_privprotocol;
235 }
236 DB_DCHECK;
237 
238 typedef struct
239 {
240 	zbx_uint64_t	dhostid;
241 	int		status;
242 	int		lastup;
243 	int		lastdown;
244 }
245 DB_DHOST;
246 
247 typedef struct
248 {
249 	zbx_uint64_t	dserviceid;
250 	int		status;
251 	int		lastup;
252 	int		lastdown;
253 	char		*value;
254 }
255 DB_DSERVICE;
256 
257 typedef struct
258 {
259 	zbx_uint64_t	triggerid;
260 	char		*description;
261 	char		*expression;
262 	char		*url;
263 	char		*comments;
264 	unsigned char	priority;
265 	unsigned char	type;
266 }
267 DB_TRIGGER;
268 
269 typedef struct
270 {
271 	DB_TRIGGER	trigger;
272 	zbx_uint64_t	eventid;
273 	zbx_uint64_t	objectid;
274 	int		source;
275 	int		object;
276 	int		clock;
277 	int		value;
278 	int		acknowledged;
279 	int		ns;
280 }
281 DB_EVENT;
282 
283 typedef struct
284 {
285 	zbx_uint64_t		mediatypeid;
286 	zbx_media_type_t	type;
287 	char			*description;
288 	char			*smtp_server;
289 	char			*smtp_helo;
290 	char			*smtp_email;
291 	char			*exec_path;
292 	char			*exec_params;
293 	char			*gsm_modem;
294 	char			*username;
295 	char			*passwd;
296 	unsigned short		smtp_port;
297 	unsigned char		smtp_security;
298 	unsigned char		smtp_verify_peer;
299 	unsigned char		smtp_verify_host;
300 	unsigned char		smtp_authentication;
301 }
302 DB_MEDIATYPE;
303 
304 typedef struct
305 {
306 	zbx_uint64_t	conditionid;
307 	zbx_uint64_t	actionid;
308 	char		*value;
309 	unsigned char	conditiontype;
310 	unsigned char	operator;
311 }
312 DB_CONDITION;
313 
314 typedef struct
315 {
316 	zbx_uint64_t	alertid;
317 	zbx_uint64_t 	actionid;
318 	int		clock;
319 	zbx_uint64_t	mediatypeid;
320 	char		*sendto;
321 	char		*subject;
322 	char		*message;
323 	zbx_alert_status_t	status;
324 	int		retries;
325 }
326 DB_ALERT;
327 
328 typedef struct
329 {
330 	zbx_uint64_t	housekeeperid;
331 	char		*tablename;
332 	char		*field;
333 	zbx_uint64_t	value;
334 }
335 DB_HOUSEKEEPER;
336 
337 typedef struct
338 {
339 	zbx_uint64_t	httptestid;
340 	char		*name;
341 	char		*variables;
342 	char		*headers;
343 	char		*agent;
344 	char		*http_user;
345 	char		*http_password;
346 	char		*http_proxy;
347 	char		*ssl_cert_file;
348 	char		*ssl_key_file;
349 	char		*ssl_key_password;
350 	int		authentication;
351 	int		retries;
352 	int		verify_peer;
353 	int		verify_host;
354 }
355 DB_HTTPTEST;
356 
357 typedef struct
358 {
359 	zbx_uint64_t	httpstepid;
360 	zbx_uint64_t	httptestid;
361 	char		*name;
362 	char		*url;
363 	char		*posts;
364 	char		*required;
365 	char		*status_codes;
366 	int		no;
367 	int		timeout;
368 	char		*variables;
369 	int		follow_redirects;
370 	int		retrieve_mode;
371 	char		*headers;
372 }
373 DB_HTTPSTEP;
374 
375 typedef struct
376 {
377 	zbx_uint64_t		escalationid;
378 	zbx_uint64_t		actionid;
379 	zbx_uint64_t		triggerid;
380 	zbx_uint64_t		itemid;
381 	zbx_uint64_t		eventid;
382 	zbx_uint64_t		r_eventid;
383 	int			nextcheck;
384 	int			esc_step;
385 	zbx_escalation_status_t	status;
386 }
387 DB_ESCALATION;
388 
389 int	DBconnect(int flag);
390 void	DBinit(void);
391 void	DBclose(void);
392 
393 #ifdef HAVE_ORACLE
394 void	DBstatement_prepare(const char *sql);
395 int	DBstatement_execute();
396 #endif
397 #ifdef HAVE___VA_ARGS__
398 #	define DBexecute(fmt, ...) __zbx_DBexecute(ZBX_CONST_STRING(fmt), ##__VA_ARGS__)
399 #else
400 #	define DBexecute __zbx_DBexecute
401 #endif
402 int	__zbx_DBexecute(const char *fmt, ...);
403 
404 #ifdef HAVE___VA_ARGS__
405 #	define DBselect_once(fmt, ...)	__zbx_DBselect_once(ZBX_CONST_STRING(fmt), ##__VA_ARGS__)
406 #	define DBselect(fmt, ...)	__zbx_DBselect(ZBX_CONST_STRING(fmt), ##__VA_ARGS__)
407 #else
408 #	define DBselect_once	__zbx_DBselect_once
409 #	define DBselect		__zbx_DBselect
410 #endif
411 DB_RESULT	__zbx_DBselect_once(const char *fmt, ...);
412 DB_RESULT	__zbx_DBselect(const char *fmt, ...);
413 
414 DB_RESULT	DBselectN(const char *query, int n);
415 DB_ROW		DBfetch(DB_RESULT result);
416 int		DBis_null(const char *field);
417 void		DBbegin(void);
418 void		DBcommit(void);
419 void		DBrollback(void);
420 void		DBend(int ret);
421 
422 const ZBX_TABLE	*DBget_table(const char *tablename);
423 const ZBX_FIELD	*DBget_field(const ZBX_TABLE *table, const char *fieldname);
424 #define DBget_maxid(table)	DBget_maxid_num(table, 1)
425 zbx_uint64_t	DBget_maxid_num(const char *tablename, int num);
426 
427 /******************************************************************************
428  *                                                                            *
429  * Type: ZBX_GRAPH_ITEMS                                                      *
430  *                                                                            *
431  * Purpose: represent graph item data                                         *
432  *                                                                            *
433  * Author: Eugene Grigorjev                                                   *
434  *                                                                            *
435  ******************************************************************************/
436 typedef struct
437 {
438 	zbx_uint64_t	itemid;	/* itemid should come first for correct sorting */
439 	zbx_uint64_t	gitemid;
440 	char		key[ITEM_KEY_LEN * ZBX_MAX_BYTES_IN_UTF8_CHAR + 1];
441 	int		drawtype;
442 	int		sortorder;
443 	char		color[GRAPH_ITEM_COLOR_LEN_MAX];
444 	int		yaxisside;
445 	int		calc_fnc;
446 	int		type;
447 	unsigned char	flags;
448 }
449 ZBX_GRAPH_ITEMS;
450 
451 void	process_triggers(zbx_vector_ptr_t *triggers);
452 int	process_trigger(char **sql, size_t *sql_alloc, size_t *sql_offset, const struct _DC_TRIGGER *trigger);
453 
454 int	DBget_row_count(const char *table_name);
455 int	DBget_proxy_lastaccess(const char *hostname, int *lastaccess, char **error);
456 
457 char	*DBdyn_escape_field(const char *table_name, const char *field_name, const char *src);
458 char	*DBdyn_escape_string(const char *src);
459 char	*DBdyn_escape_string_len(const char *src, size_t length);
460 char	*DBdyn_escape_like_pattern(const char *src);
461 
462 zbx_uint64_t	DBadd_host(char *server, int port, int status, int useip, char *ip, int disable_until, int available);
463 int	DBhost_exists(char *server);
464 int	DBadd_templates_to_host(int hostid, int host_templateid);
465 
466 int	DBadd_template_linkage(int hostid, int templateid, int items, int triggers, int graphs);
467 
468 int	DBadd_trigger_to_linked_hosts(int triggerid, int hostid);
469 void	DBdelete_sysmaps_hosts_by_hostid(zbx_uint64_t hostid);
470 
471 int	DBadd_graph_item_to_linked_hosts(int gitemid, int hostid);
472 
473 int	DBcopy_template_elements(zbx_uint64_t hostid, zbx_vector_uint64_t *lnk_templateids, char **error);
474 int	DBdelete_template_elements(zbx_uint64_t hostid, zbx_vector_uint64_t *del_templateids, char **error);
475 
476 void	DBdelete_items(zbx_vector_uint64_t *itemids);
477 void	DBdelete_graphs(zbx_vector_uint64_t *graphids);
478 void	DBdelete_hosts(zbx_vector_uint64_t *hostids);
479 void	DBdelete_hosts_with_prototypes(zbx_vector_uint64_t *hostids);
480 
481 void	zbx_get_itservices_updates(const DB_EVENT *events, size_t events_num, zbx_vector_ptr_t *itservice_updates);
482 int	DBupdate_itservices(const zbx_vector_ptr_t *itservice_updates);
483 int	DBremove_triggers_from_itservices(zbx_uint64_t *triggerids, int triggerids_num);
484 
485 void	zbx_create_itservices_lock(void);
486 void	zbx_destroy_itservices_lock(void);
487 
488 void	DBadd_condition_alloc(char **sql, size_t *sql_alloc, size_t *sql_offset, const char *fieldname,
489 		const zbx_uint64_t *values, const int num);
490 void	DBadd_str_condition_alloc(char **sql, size_t *sql_alloc, size_t *sql_offset, const char *fieldname,
491 		const char **values, const int num);
492 
493 int	zbx_check_user_permissions(const zbx_uint64_t *userid, const zbx_uint64_t *recipient_userid);
494 
495 const char	*zbx_host_string(zbx_uint64_t hostid);
496 const char	*zbx_host_key_string(zbx_uint64_t itemid);
497 const char	*zbx_user_string(zbx_uint64_t userid);
498 
499 void	DBregister_host(zbx_uint64_t proxy_hostid, const char *host, const char *ip, const char *dns,
500 		unsigned short port, const char *host_metadata, int now);
501 void	DBregister_host_prepare(zbx_vector_ptr_t *autoreg_hosts, const char *host, const char *ip, const char *dns,
502 		unsigned short port, const char *host_metadata, int now);
503 void	DBregister_host_flush(zbx_vector_ptr_t *autoreg_hosts, zbx_uint64_t proxy_hostid);
504 void	DBregister_host_clean(zbx_vector_ptr_t *autoreg_hosts);
505 
506 void	DBproxy_register_host(const char *host, const char *ip, const char *dns, unsigned short port,
507 		const char *host_metadata);
508 int	DBexecute_overflowed_sql(char **sql, size_t *sql_alloc, size_t *sql_offset);
509 char	*DBget_unique_hostname_by_sample(const char *host_name_sample);
510 
511 const char	*DBsql_id_ins(zbx_uint64_t id);
512 const char	*DBsql_id_cmp(zbx_uint64_t id);
513 
514 zbx_uint64_t	DBadd_interface(zbx_uint64_t hostid, unsigned char type,
515 		unsigned char useip, const char *ip, const char *dns, unsigned short port);
516 
517 const char	*DBget_inventory_field(unsigned char inventory_link);
518 
519 void	DBset_host_inventory(zbx_uint64_t hostid, int inventory_mode);
520 void	DBadd_host_inventory(zbx_uint64_t hostid, int inventory_mode);
521 
522 int	DBtxn_status(void);
523 int	DBtxn_ongoing(void);
524 
525 int	DBtable_exists(const char *table_name);
526 int	DBfield_exists(const char *table_name, const char *field_name);
527 
528 void	DBexecute_multiple_query(const char *query, const char *field_name, zbx_vector_uint64_t *ids);
529 int	DBlock_record(const char *table, zbx_uint64_t id, const char *add_field, zbx_uint64_t add_id);
530 int	DBlock_records(const char *table, const zbx_vector_uint64_t *ids);
531 
532 #define DBlock_hostid(id)			DBlock_record("hosts", id, NULL, 0)
533 #define DBlock_druleid(id)			DBlock_record("drules", id, NULL, 0)
534 #define DBlock_dcheckid(dcheckid, druleid)	DBlock_record("dchecks", dcheckid, "druleid", druleid)
535 #define DBlock_hostids(ids)			DBlock_records("hosts", ids)
536 
537 void	DBdelete_groups(zbx_vector_uint64_t *groupids);
538 
539 void	DBselect_uint64(const char *sql, zbx_vector_uint64_t *ids);
540 
541 /* bulk insert support */
542 
543 /* database bulk insert data */
544 typedef struct
545 {
546 	/* the target table */
547 	const ZBX_TABLE		*table;
548 	/* the fields to insert (pointers to the ZBX_FIELD structures from database schema) */
549 	zbx_vector_ptr_t	fields;
550 	/* the values rows to insert (pointers to arrays of zbx_db_value_t structures) */
551 	zbx_vector_ptr_t	rows;
552 	/* index of autoincrement field */
553 	int			autoincrement;
554 }
555 zbx_db_insert_t;
556 
557 void	zbx_db_insert_prepare_dyn(zbx_db_insert_t *self, const ZBX_TABLE *table, const ZBX_FIELD **fields,
558 		int fields_num);
559 void	zbx_db_insert_prepare(zbx_db_insert_t *self, const char *table, ...);
560 void	zbx_db_insert_add_values_dyn(zbx_db_insert_t *self, const zbx_db_value_t **values, int values_num);
561 void	zbx_db_insert_add_values(zbx_db_insert_t *self, ...);
562 int	zbx_db_insert_execute(zbx_db_insert_t *self);
563 void	zbx_db_insert_clean(zbx_db_insert_t *self);
564 void	zbx_db_insert_autoincrement(zbx_db_insert_t *self, const char *field_name);
565 int	zbx_db_get_database_type(void);
566 
567 /* agent (ZABBIX, SNMP, IPMI, JMX) availability data */
568 typedef struct
569 {
570 	/* flags specifying which fields are set, see ZBX_FLAGS_AGENT_STATUS_* defines */
571 	unsigned char	flags;
572 
573 	/* agent availability fields */
574 	unsigned char	available;
575 	char		*error;
576 	int		errors_from;
577 	int		disable_until;
578 }
579 zbx_agent_availability_t;
580 
581 #define ZBX_FLAGS_AGENT_STATUS_NONE		0x00000000
582 #define ZBX_FLAGS_AGENT_STATUS_AVAILABLE	0x00000001
583 #define ZBX_FLAGS_AGENT_STATUS_ERROR		0x00000002
584 #define ZBX_FLAGS_AGENT_STATUS_ERRORS_FROM	0x00000004
585 #define ZBX_FLAGS_AGENT_STATUS_DISABLE_UNTIL	0x00000008
586 
587 #define ZBX_FLAGS_AGENT_STATUS		(ZBX_FLAGS_AGENT_STATUS_AVAILABLE |	\
588 					ZBX_FLAGS_AGENT_STATUS_ERROR |		\
589 					ZBX_FLAGS_AGENT_STATUS_ERRORS_FROM |	\
590 					ZBX_FLAGS_AGENT_STATUS_DISABLE_UNTIL)
591 
592 #define ZBX_AGENT_ZABBIX	(INTERFACE_TYPE_AGENT - 1)
593 #define ZBX_AGENT_SNMP		(INTERFACE_TYPE_SNMP - 1)
594 #define ZBX_AGENT_IPMI		(INTERFACE_TYPE_IPMI - 1)
595 #define ZBX_AGENT_JMX		(INTERFACE_TYPE_JMX - 1)
596 #define ZBX_AGENT_UNKNOWN 	255
597 #define ZBX_AGENT_MAX		INTERFACE_TYPE_COUNT
598 
599 typedef struct
600 {
601 	zbx_uint64_t			hostid;
602 
603 	zbx_agent_availability_t	agents[ZBX_AGENT_MAX];
604 }
605 zbx_host_availability_t;
606 
607 
608 int	zbx_sql_add_host_availability(char **sql, size_t *sql_alloc, size_t *sql_offset,
609 		const zbx_host_availability_t *ha);
610 
611 #endif
612