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 char	*CONFIG_DB_TLS_CONNECT;
35 extern char	*CONFIG_DB_TLS_CERT_FILE;
36 extern char	*CONFIG_DB_TLS_KEY_FILE;
37 extern char	*CONFIG_DB_TLS_CA_FILE;
38 extern char	*CONFIG_DB_TLS_CIPHER;
39 extern char	*CONFIG_DB_TLS_CIPHER_13;
40 extern int	CONFIG_DBPORT;
41 extern int	CONFIG_HISTSYNCER_FORKS;
42 extern int	CONFIG_UNAVAILABLE_DELAY;
43 
44 typedef enum
45 {
46 	GRAPH_TYPE_NORMAL = 0,
47 	GRAPH_TYPE_STACKED = 1
48 }
49 zbx_graph_types;
50 
51 typedef enum
52 {
53 	CALC_FNC_MIN = 1,
54 	CALC_FNC_AVG = 2,
55 	CALC_FNC_MAX = 4,
56 	CALC_FNC_ALL = 7
57 }
58 zbx_graph_item_calc_function;
59 
60 typedef enum
61 {
62 	GRAPH_ITEM_SIMPLE = 0,
63 	GRAPH_ITEM_AGGREGATED = 1
64 }
65 zbx_graph_item_type;
66 
67 struct	_DC_TRIGGER;
68 
69 #define ZBX_DB_CONNECT_NORMAL	0
70 #define ZBX_DB_CONNECT_EXIT	1
71 #define ZBX_DB_CONNECT_ONCE	2
72 
73 /* type of database */
74 #define ZBX_DB_UNKNOWN	0
75 #define ZBX_DB_SERVER	1
76 #define ZBX_DB_PROXY	2
77 
78 #define TRIGGER_OPDATA_LEN		255
79 #define TRIGGER_URL_LEN			255
80 #define TRIGGER_DESCRIPTION_LEN		255
81 #define TRIGGER_EXPRESSION_LEN		2048
82 #define TRIGGER_EXPRESSION_LEN_MAX	(TRIGGER_EXPRESSION_LEN + 1)
83 #if defined(HAVE_ORACLE)
84 #	define TRIGGER_COMMENTS_LEN	2048
85 #else
86 #	define TRIGGER_COMMENTS_LEN	65535
87 #endif
88 #define TAG_NAME_LEN			255
89 #define TAG_VALUE_LEN			255
90 
91 #define GROUP_NAME_LEN			255
92 
93 #define HOST_HOST_LEN			MAX_ZBX_HOSTNAME_LEN
94 #define HOST_HOST_LEN_MAX		(HOST_HOST_LEN + 1)
95 #define HOST_NAME_LEN			128
96 #define HOST_ERROR_LEN			2048
97 #define HOST_ERROR_LEN_MAX		(HOST_ERROR_LEN + 1)
98 #define HOST_IPMI_USERNAME_LEN		16
99 #define HOST_IPMI_USERNAME_LEN_MAX	(HOST_IPMI_USERNAME_LEN + 1)
100 #define HOST_IPMI_PASSWORD_LEN		20
101 #define HOST_IPMI_PASSWORD_LEN_MAX	(HOST_IPMI_PASSWORD_LEN + 1)
102 #define HOST_PROXY_ADDRESS_LEN		255
103 #define HOST_PROXY_ADDRESS_LEN_MAX	(HOST_PROXY_ADDRESS_LEN + 1)
104 
105 #define INTERFACE_DNS_LEN		255
106 #define INTERFACE_DNS_LEN_MAX		(INTERFACE_DNS_LEN + 1)
107 #define INTERFACE_IP_LEN		64
108 #define INTERFACE_IP_LEN_MAX		(INTERFACE_IP_LEN + 1)
109 #define INTERFACE_ADDR_LEN		255	/* MAX(INTERFACE_DNS_LEN,INTERFACE_IP_LEN) */
110 #define INTERFACE_ADDR_LEN_MAX		(INTERFACE_ADDR_LEN + 1)
111 #define INTERFACE_PORT_LEN		64
112 #define INTERFACE_PORT_LEN_MAX		(INTERFACE_PORT_LEN + 1)
113 
114 #define ITEM_NAME_LEN			255
115 #define ITEM_KEY_LEN			2048
116 #define ITEM_DELAY_LEN			1024
117 #define ITEM_HISTORY_LEN		255
118 #define ITEM_TRENDS_LEN			255
119 #define ITEM_UNITS_LEN			255
120 #define ITEM_SNMP_COMMUNITY_LEN		64
121 #define ITEM_SNMP_COMMUNITY_LEN_MAX	(ITEM_SNMP_COMMUNITY_LEN + 1)
122 #define ITEM_SNMP_OID_LEN		512
123 #define ITEM_SNMP_OID_LEN_MAX		(ITEM_SNMP_OID_LEN + 1)
124 #define ITEM_ERROR_LEN			2048
125 #define ITEM_ERROR_LEN_MAX		(ITEM_ERROR_LEN + 1)
126 #define ITEM_TRAPPER_HOSTS_LEN		255
127 #define ITEM_TRAPPER_HOSTS_LEN_MAX	(ITEM_TRAPPER_HOSTS_LEN + 1)
128 #define ITEM_SNMPV3_SECURITYNAME_LEN		64
129 #define ITEM_SNMPV3_SECURITYNAME_LEN_MAX	(ITEM_SNMPV3_SECURITYNAME_LEN + 1)
130 #define ITEM_SNMPV3_AUTHPASSPHRASE_LEN		64
131 #define ITEM_SNMPV3_AUTHPASSPHRASE_LEN_MAX	(ITEM_SNMPV3_AUTHPASSPHRASE_LEN + 1)
132 #define ITEM_SNMPV3_PRIVPASSPHRASE_LEN		64
133 #define ITEM_SNMPV3_PRIVPASSPHRASE_LEN_MAX	(ITEM_SNMPV3_PRIVPASSPHRASE_LEN + 1)
134 #define ITEM_SNMPV3_CONTEXTNAME_LEN		255
135 #define ITEM_SNMPV3_CONTEXTNAME_LEN_MAX		(ITEM_SNMPV3_CONTEXTNAME_LEN + 1)
136 #define ITEM_LOGTIMEFMT_LEN		64
137 #define ITEM_LOGTIMEFMT_LEN_MAX		(ITEM_LOGTIMEFMT_LEN + 1)
138 #define ITEM_IPMI_SENSOR_LEN		128
139 #define ITEM_IPMI_SENSOR_LEN_MAX	(ITEM_IPMI_SENSOR_LEN + 1)
140 #define ITEM_USERNAME_LEN		64
141 #define ITEM_USERNAME_LEN_MAX		(ITEM_USERNAME_LEN + 1)
142 #define ITEM_PASSWORD_LEN		64
143 #define ITEM_PASSWORD_LEN_MAX		(ITEM_PASSWORD_LEN + 1)
144 #define ITEM_PUBLICKEY_LEN		64
145 #define ITEM_PUBLICKEY_LEN_MAX		(ITEM_PUBLICKEY_LEN + 1)
146 #define ITEM_PRIVATEKEY_LEN		64
147 #define ITEM_PRIVATEKEY_LEN_MAX		(ITEM_PRIVATEKEY_LEN + 1)
148 #define ITEM_JMX_ENDPOINT_LEN		255
149 #define ITEM_JMX_ENDPOINT_LEN_MAX	(ITEM_JMX_ENDPOINT_LEN + 1)
150 #define ITEM_TIMEOUT_LEN		255
151 #define ITEM_TIMEOUT_LEN_MAX		(ITEM_TIMEOUT_LEN + 1)
152 #define ITEM_URL_LEN			2048
153 #define ITEM_URL_LEN_MAX		(ITEM_URL_LEN + 1)
154 #define ITEM_QUERY_FIELDS_LEN		2048
155 #define ITEM_QUERY_FIELDS_LEN_MAX	(ITEM_QUERY_FIELDS_LEN + 1)
156 #define ITEM_STATUS_CODES_LEN		255
157 #define ITEM_STATUS_CODES_LEN_MAX	(ITEM_STATUS_CODES_LEN + 1)
158 #define ITEM_HTTP_PROXY_LEN		255
159 #define ITEM_HTTP_PROXY_LEN_MAX		(ITEM_HTTP_PROXY_LEN + 1)
160 #define ITEM_SSL_KEY_PASSWORD_LEN	64
161 #define ITEM_SSL_KEY_PASSWORD_LEN_MAX	(ITEM_SSL_KEY_PASSWORD_LEN + 1)
162 #define ITEM_SSL_CERT_FILE_LEN		255
163 #define ITEM_SSL_CERT_FILE_LEN_MAX	(ITEM_SSL_CERT_FILE_LEN + 1)
164 #define ITEM_SSL_KEY_FILE_LEN		255
165 #define ITEM_SSL_KEY_FILE_LEN_MAX	(ITEM_SSL_KEY_FILE_LEN + 1)
166 #if defined(HAVE_ORACLE)
167 #	define ITEM_PARAM_LEN		2048
168 #	define ITEM_DESCRIPTION_LEN	2048
169 #	define ITEM_POSTS_LEN		2048
170 #	define ITEM_HEADERS_LEN		2048
171 #else
172 #	define ITEM_PARAM_LEN		65535
173 #	define ITEM_DESCRIPTION_LEN	65535
174 #	define ITEM_POSTS_LEN		65535
175 #	define ITEM_HEADERS_LEN		65535
176 #endif
177 
178 #define APPLICATION_NAME_LEN		255
179 
180 #define HISTORY_STR_VALUE_LEN		255
181 #define HISTORY_TEXT_VALUE_LEN		65535
182 #define HISTORY_LOG_VALUE_LEN		65535
183 
184 #define HISTORY_LOG_SOURCE_LEN		64
185 #define HISTORY_LOG_SOURCE_LEN_MAX	(HISTORY_LOG_SOURCE_LEN + 1)
186 
187 #define ALERT_ERROR_LEN			2048
188 #define ALERT_ERROR_LEN_MAX		(ALERT_ERROR_LEN + 1)
189 
190 #define GRAPH_NAME_LEN			128
191 
192 #define GRAPH_ITEM_COLOR_LEN		6
193 #define GRAPH_ITEM_COLOR_LEN_MAX	(GRAPH_ITEM_COLOR_LEN + 1)
194 
195 #define DSERVICE_VALUE_LEN		255
196 #define MAX_DISCOVERED_VALUE_SIZE	(DSERVICE_VALUE_LEN * ZBX_MAX_BYTES_IN_UTF8_CHAR + 1)
197 
198 #define HTTPTEST_HTTP_USER_LEN		64
199 #define HTTPTEST_HTTP_PASSWORD_LEN	64
200 
201 #define PROXY_DHISTORY_VALUE_LEN	255
202 
203 #define ITEM_PREPROC_PARAMS_LEN		255
204 
205 #define EVENT_NAME_LEN			2048
206 
207 #define FUNCTION_PARAM_LEN		255
208 
209 #define ZBX_SQL_ITEM_FIELDS	"i.itemid,i.key_,h.host,i.type,i.history,i.hostid,i.value_type,i.delta,"	\
210 				"i.units,i.multiplier,i.formula,i.state,i.valuemapid,i.trends,i.data_type"
211 #define ZBX_SQL_ITEM_TABLES	"hosts h,items i"
212 #define ZBX_SQL_TIME_FUNCTIONS	"'nodata','date','dayofmonth','dayofweek','time','now'"
213 #define ZBX_SQL_ITEM_FIELDS_NUM	15
214 #define ZBX_SQL_ITEM_SELECT	ZBX_SQL_ITEM_FIELDS " from " ZBX_SQL_ITEM_TABLES
215 
216 #ifdef HAVE_ORACLE
217 #	define ZBX_PLSQL_BEGIN	"begin\n"
218 #	define ZBX_PLSQL_END	"end;"
219 #	define	DBbegin_multiple_update(sql, sql_alloc, sql_offset)			\
220 			zbx_strcpy_alloc(sql, sql_alloc, sql_offset, ZBX_PLSQL_BEGIN)
221 #	define	DBend_multiple_update(sql, sql_alloc, sql_offset)			\
222 			zbx_strcpy_alloc(sql, sql_alloc, sql_offset, ZBX_PLSQL_END)
223 #	if 0 == ZBX_MAX_OVERFLOW_SQL_SIZE
224 #		define	ZBX_SQL_EXEC_FROM	ZBX_CONST_STRLEN(ZBX_PLSQL_BEGIN)
225 #	else
226 #		define	ZBX_SQL_EXEC_FROM	0
227 #	endif
228 
229 #	define	ZBX_SQL_STRCMP		"%s%s%s"
230 #	define	ZBX_SQL_STRVAL_EQ(str)				\
231 			'\0' != *str ? "='"  : "",		\
232 			'\0' != *str ? str   : " is null",	\
233 			'\0' != *str ? "'"   : ""
234 #	define	ZBX_SQL_STRVAL_NE(str)				\
235 			'\0' != *str ? "<>'" : "",		\
236 			'\0' != *str ? str   : " is not null",	\
237 			'\0' != *str ? "'"   : ""
238 
239 #else
240 #	define	DBbegin_multiple_update(sql, sql_alloc, sql_offset)	do {} while (0)
241 #	define	DBend_multiple_update(sql, sql_alloc, sql_offset)	do {} while (0)
242 
243 #	define	ZBX_SQL_EXEC_FROM	0
244 #	ifdef HAVE_MYSQL
245 #		define	ZBX_SQL_STRCMP		"%s binary '%s'"
246 #	else
247 #		define	ZBX_SQL_STRCMP		"%s'%s'"
248 #	endif
249 #	define	ZBX_SQL_STRVAL_EQ(str)	"=", str
250 #	define	ZBX_SQL_STRVAL_NE(str)	"<>", str
251 #endif
252 
253 #define ZBX_SQL_NULLCMP(f1, f2)	"((" f1 " is null and " f2 " is null) or " f1 "=" f2 ")"
254 
255 #define ZBX_DBROW2UINT64(uint, row)	if (SUCCEED == DBis_null(row))		\
256 						uint = 0;			\
257 					else					\
258 						is_uint64(row, &uint)
259 
260 #define ZBX_DB_MAX_ID	(zbx_uint64_t)__UINT64_C(0x7fffffffffffffff)
261 
262 typedef struct
263 {
264 	zbx_uint64_t	druleid;
265 	zbx_uint64_t	unique_dcheckid;
266 	char		*iprange;
267 	char		*name;
268 }
269 DB_DRULE;
270 
271 typedef struct
272 {
273 	zbx_uint64_t	dcheckid;
274 	char		*ports;
275 	char		*key_;
276 	char		*snmp_community;
277 	char		*snmpv3_securityname;
278 	char		*snmpv3_authpassphrase;
279 	char		*snmpv3_privpassphrase;
280 	char		*snmpv3_contextname;
281 	int		type;
282 	unsigned char	snmpv3_securitylevel;
283 	unsigned char	snmpv3_authprotocol;
284 	unsigned char	snmpv3_privprotocol;
285 }
286 DB_DCHECK;
287 
288 typedef struct
289 {
290 	zbx_uint64_t	dhostid;
291 	int		status;
292 	int		lastup;
293 	int		lastdown;
294 }
295 DB_DHOST;
296 
297 typedef struct
298 {
299 	zbx_uint64_t	dserviceid;
300 	int		status;
301 	int		lastup;
302 	int		lastdown;
303 	char		*value;
304 }
305 DB_DSERVICE;
306 
307 typedef struct
308 {
309 	zbx_uint64_t	triggerid;
310 	char		*description;
311 	char		*expression;
312 	char		*recovery_expression;
313 	char		*url;
314 	char		*comments;
315 	char		*correlation_tag;
316 	char		*opdata;
317 	unsigned char	value;
318 	unsigned char	priority;
319 	unsigned char	type;
320 	unsigned char	recovery_mode;
321 	unsigned char	correlation_mode;
322 }
323 DB_TRIGGER;
324 
325 typedef struct
326 {
327 	zbx_uint64_t		eventid;
328 	DB_TRIGGER		trigger;
329 	zbx_uint64_t		objectid;
330 	char			*name;
331 	int			source;
332 	int			object;
333 	int			clock;
334 	int			value;
335 	int			acknowledged;
336 	int			ns;
337 	int			severity;
338 	unsigned char		suppressed;
339 
340 	zbx_vector_ptr_t	tags;	/* used for both zbx_tag_t and zbx_host_tag_t */
341 
342 #define ZBX_FLAGS_DB_EVENT_UNSET		0x0000
343 #define ZBX_FLAGS_DB_EVENT_CREATE		0x0001
344 #define ZBX_FLAGS_DB_EVENT_NO_ACTION		0x0002
345 #define ZBX_FLAGS_DB_EVENT_RECOVER		0x0004
346 	zbx_uint64_t		flags;
347 }
348 DB_EVENT;
349 
350 typedef struct
351 {
352 	zbx_uint64_t		mediatypeid;
353 	zbx_media_type_t	type;
354 	char			*description;
355 	char			*smtp_server;
356 	char			*smtp_helo;
357 	char			*smtp_email;
358 	char			*exec_path;
359 	char			*exec_params;
360 	char			*gsm_modem;
361 	char			*username;
362 	char			*passwd;
363 	unsigned short		smtp_port;
364 	unsigned char		smtp_security;
365 	unsigned char		smtp_verify_peer;
366 	unsigned char		smtp_verify_host;
367 	unsigned char		smtp_authentication;
368 }
369 DB_MEDIATYPE;
370 
371 typedef struct
372 {
373 	zbx_uint64_t	alertid;
374 	zbx_uint64_t 	actionid;
375 	int		clock;
376 	zbx_uint64_t	mediatypeid;
377 	char		*sendto;
378 	char		*subject;
379 	char		*message;
380 	zbx_alert_status_t	status;
381 	int		retries;
382 }
383 DB_ALERT;
384 
385 typedef struct
386 {
387 	zbx_uint64_t	housekeeperid;
388 	char		*tablename;
389 	char		*field;
390 	zbx_uint64_t	value;
391 }
392 DB_HOUSEKEEPER;
393 
394 typedef struct
395 {
396 	zbx_uint64_t	httptestid;
397 	char		*name;
398 	char		*agent;
399 	char		*http_user;
400 	char		*http_password;
401 	char		*http_proxy;
402 	char		*ssl_cert_file;
403 	char		*ssl_key_file;
404 	char		*ssl_key_password;
405 	char		*delay;
406 	int		authentication;
407 	int		retries;
408 	int		verify_peer;
409 	int		verify_host;
410 }
411 DB_HTTPTEST;
412 
413 typedef struct
414 {
415 	zbx_uint64_t	httpstepid;
416 	zbx_uint64_t	httptestid;
417 	char		*name;
418 	char		*url;
419 	char		*posts;
420 	char		*required;
421 	char		*status_codes;
422 	int		no;
423 	int		timeout;
424 	int		follow_redirects;
425 	int		retrieve_mode;
426 	int		post_type;
427 }
428 DB_HTTPSTEP;
429 
430 typedef struct
431 {
432 	zbx_uint64_t		escalationid;
433 	zbx_uint64_t		actionid;
434 	zbx_uint64_t		triggerid;
435 	zbx_uint64_t		itemid;
436 	zbx_uint64_t		eventid;
437 	zbx_uint64_t		r_eventid;
438 	zbx_uint64_t		acknowledgeid;
439 	int			nextcheck;
440 	int			esc_step;
441 	zbx_escalation_status_t	status;
442 }
443 DB_ESCALATION;
444 
445 typedef struct
446 {
447 	zbx_uint64_t	actionid;
448 	char		*name;
449 	int		esc_period;
450 	unsigned char	eventsource;
451 	unsigned char	pause_suppressed;
452 	unsigned char	recovery;
453 	unsigned char	status;
454 }
455 DB_ACTION;
456 
457 typedef struct
458 {
459 	zbx_uint64_t	acknowledgeid;
460 	zbx_uint64_t	userid;
461 	char		*message;
462 	int		clock;
463 	int		action;
464 	int		old_severity;
465 	int		new_severity;
466 }
467 DB_ACKNOWLEDGE;
468 
469 int	DBinit(char **error);
470 void	DBdeinit(void);
471 
472 void	DBinit_autoincrement_options(void);
473 
474 int	DBconnect(int flag);
475 void	DBclose(void);
476 
477 int	zbx_db_validate_config_features(void);
478 #if defined(HAVE_MYSQL) || defined(HAVE_POSTGRESQL)
479 void	zbx_db_validate_config(void);
480 #endif
481 
482 #ifdef HAVE_ORACLE
483 void	DBstatement_prepare(const char *sql);
484 #endif
485 int		DBexecute(const char *fmt, ...) __zbx_attr_format_printf(1, 2);
486 int		DBexecute_once(const char *fmt, ...) __zbx_attr_format_printf(1, 2);
487 DB_RESULT	DBselect_once(const char *fmt, ...) __zbx_attr_format_printf(1, 2);
488 DB_RESULT	DBselect(const char *fmt, ...) __zbx_attr_format_printf(1, 2);
489 DB_RESULT	DBselectN(const char *query, int n);
490 DB_ROW		DBfetch(DB_RESULT result);
491 int		DBis_null(const char *field);
492 void		DBbegin(void);
493 int		DBcommit(void);
494 void		DBrollback(void);
495 int		DBend(int ret);
496 
497 const ZBX_TABLE	*DBget_table(const char *tablename);
498 const ZBX_FIELD	*DBget_field(const ZBX_TABLE *table, const char *fieldname);
499 #define DBget_maxid(table)	DBget_maxid_num(table, 1)
500 zbx_uint64_t	DBget_maxid_num(const char *tablename, int num);
501 
502 void	DBcheck_capabilities(void);
503 
504 #ifdef HAVE_POSTGRESQL
505 char	*zbx_db_get_schema_esc(void);
506 #endif
507 
508 /******************************************************************************
509  *                                                                            *
510  * Type: ZBX_GRAPH_ITEMS                                                      *
511  *                                                                            *
512  * Purpose: represent graph item data                                         *
513  *                                                                            *
514  * Author: Eugene Grigorjev                                                   *
515  *                                                                            *
516  ******************************************************************************/
517 typedef struct
518 {
519 	zbx_uint64_t	itemid;	/* itemid should come first for correct sorting */
520 	zbx_uint64_t	gitemid;
521 	char		key[ITEM_KEY_LEN * ZBX_MAX_BYTES_IN_UTF8_CHAR + 1];
522 	int		drawtype;
523 	int		sortorder;
524 	char		color[GRAPH_ITEM_COLOR_LEN_MAX];
525 	int		yaxisside;
526 	int		calc_fnc;
527 	int		type;
528 	unsigned char	flags;
529 }
530 ZBX_GRAPH_ITEMS;
531 
532 typedef struct
533 {
534 	zbx_uint64_t	triggerid;
535 	unsigned char	value;
536 	unsigned char	state;
537 	unsigned char	priority;
538 	int		lastchange;
539 	int		problem_count;
540 	char		*error;
541 
542 #define ZBX_FLAGS_TRIGGER_DIFF_UNSET				0x0000
543 #define ZBX_FLAGS_TRIGGER_DIFF_UPDATE_VALUE			0x0001
544 #define ZBX_FLAGS_TRIGGER_DIFF_UPDATE_LASTCHANGE		0x0002
545 #define ZBX_FLAGS_TRIGGER_DIFF_UPDATE_STATE			0x0004
546 #define ZBX_FLAGS_TRIGGER_DIFF_UPDATE_ERROR			0x0008
547 #define ZBX_FLAGS_TRIGGER_DIFF_UPDATE										\
548 		(ZBX_FLAGS_TRIGGER_DIFF_UPDATE_VALUE | ZBX_FLAGS_TRIGGER_DIFF_UPDATE_LASTCHANGE | 		\
549 		ZBX_FLAGS_TRIGGER_DIFF_UPDATE_STATE | ZBX_FLAGS_TRIGGER_DIFF_UPDATE_ERROR)
550 #define ZBX_FLAGS_TRIGGER_DIFF_UPDATE_PROBLEM_COUNT		0x1000
551 #define ZBX_FLAGS_TRIGGER_DIFF_RECALCULATE_PROBLEM_COUNT	0x2000
552 	zbx_uint64_t			flags;
553 }
554 zbx_trigger_diff_t;
555 
556 void	zbx_process_triggers(zbx_vector_ptr_t *triggers, zbx_vector_ptr_t *trigger_diff);
557 void	zbx_db_save_trigger_changes(const zbx_vector_ptr_t *trigger_diff);
558 void	zbx_trigger_diff_free(zbx_trigger_diff_t *diff);
559 void	zbx_append_trigger_diff(zbx_vector_ptr_t *trigger_diff, zbx_uint64_t triggerid, unsigned char priority,
560 		zbx_uint64_t flags, unsigned char value, unsigned char state, int lastchange, const char *error);
561 
562 int	DBget_row_count(const char *table_name);
563 int	DBget_proxy_lastaccess(const char *hostname, int *lastaccess, char **error);
564 
565 char	*DBdyn_escape_field(const char *table_name, const char *field_name, const char *src);
566 char	*DBdyn_escape_string(const char *src);
567 char	*DBdyn_escape_string_len(const char *src, size_t length);
568 char	*DBdyn_escape_like_pattern(const char *src);
569 
570 zbx_uint64_t	DBadd_host(char *server, int port, int status, int useip, char *ip, int disable_until, int available);
571 int	DBadd_templates_to_host(int hostid, int host_templateid);
572 
573 int	DBadd_template_linkage(int hostid, int templateid, int items, int triggers, int graphs);
574 
575 int	DBadd_trigger_to_linked_hosts(int triggerid, int hostid);
576 void	DBdelete_sysmaps_hosts_by_hostid(zbx_uint64_t hostid);
577 
578 int	DBadd_graph_item_to_linked_hosts(int gitemid, int hostid);
579 
580 int	DBcopy_template_elements(zbx_uint64_t hostid, zbx_vector_uint64_t *lnk_templateids, char **error);
581 int	DBdelete_template_elements(zbx_uint64_t hostid, zbx_vector_uint64_t *del_templateids, char **error);
582 
583 void	DBdelete_items(zbx_vector_uint64_t *itemids);
584 void	DBdelete_graphs(zbx_vector_uint64_t *graphids);
585 void	DBdelete_triggers(zbx_vector_uint64_t *triggerids);
586 void	DBdelete_hosts(zbx_vector_uint64_t *hostids);
587 void	DBdelete_hosts_with_prototypes(zbx_vector_uint64_t *hostids);
588 
589 int	DBupdate_itservices(const zbx_vector_ptr_t *trigger_diff);
590 int	DBremove_triggers_from_itservices(zbx_uint64_t *triggerids, int triggerids_num);
591 
592 int	zbx_create_itservices_lock(char **error);
593 void	zbx_destroy_itservices_lock(void);
594 
595 void	DBadd_condition_alloc(char **sql, size_t *sql_alloc, size_t *sql_offset, const char *fieldname,
596 		const zbx_uint64_t *values, const int num);
597 void	DBadd_str_condition_alloc(char **sql, size_t *sql_alloc, size_t *sql_offset, const char *fieldname,
598 		const char **values, const int num);
599 
600 int	zbx_check_user_permissions(const zbx_uint64_t *userid, const zbx_uint64_t *recipient_userid);
601 
602 const char	*zbx_host_string(zbx_uint64_t hostid);
603 const char	*zbx_host_key_string(zbx_uint64_t itemid);
604 const char	*zbx_user_string(zbx_uint64_t userid);
605 int	DBget_user_names(zbx_uint64_t userid, char **alias, char **name, char **surname);
606 
607 void	DBregister_host(zbx_uint64_t proxy_hostid, const char *host, const char *ip, const char *dns,
608 		unsigned short port, unsigned int connection_type, const char *host_metadata, unsigned short flag,
609 		int now);
610 void	DBregister_host_prepare(zbx_vector_ptr_t *autoreg_hosts, const char *host, const char *ip, const char *dns,
611 		unsigned short port, unsigned int connection_type, const char *host_metadata, unsigned short flag,
612 		int now);
613 void	DBregister_host_flush(zbx_vector_ptr_t *autoreg_hosts, zbx_uint64_t proxy_hostid);
614 void	DBregister_host_clean(zbx_vector_ptr_t *autoreg_hosts);
615 
616 void	DBproxy_register_host(const char *host, const char *ip, const char *dns, unsigned short port,
617 		unsigned int connection_type, const char *host_metadata, unsigned short flag);
618 int	DBexecute_overflowed_sql(char **sql, size_t *sql_alloc, size_t *sql_offset);
619 char	*DBget_unique_hostname_by_sample(const char *host_name_sample, const char *field_name);
620 
621 const char	*DBsql_id_ins(zbx_uint64_t id);
622 const char	*DBsql_id_cmp(zbx_uint64_t id);
623 
624 typedef enum
625 {
626 	ZBX_CONN_DEFAULT = 0,
627 	ZBX_CONN_IP,
628 	ZBX_CONN_DNS,
629 }
630 zbx_conn_flags_t;
631 
632 zbx_uint64_t	DBadd_interface(zbx_uint64_t hostid, unsigned char type, unsigned char useip, const char *ip,
633 		const char *dns, unsigned short port, zbx_conn_flags_t flags);
634 void	DBadd_interface_snmp(const zbx_uint64_t interfaceid, const unsigned char version, const unsigned char bulk,
635 		const char *community, const char *securityname, const unsigned char securitylevel,
636 		const char *authpassphrase, const char *privpassphrase, const unsigned char authprotocol,
637 		const unsigned char privprotocol, const char *contextname);
638 
639 const char	*DBget_inventory_field(unsigned char inventory_link);
640 
641 void	DBset_host_inventory(zbx_uint64_t hostid, int inventory_mode);
642 void	DBadd_host_inventory(zbx_uint64_t hostid, int inventory_mode);
643 
644 int	DBtxn_status(void);
645 int	DBtxn_ongoing(void);
646 
647 int	DBtable_exists(const char *table_name);
648 int	DBfield_exists(const char *table_name, const char *field_name);
649 #ifndef HAVE_SQLITE3
650 int	DBindex_exists(const char *table_name, const char *index_name);
651 #endif
652 
653 int	DBexecute_multiple_query(const char *query, const char *field_name, zbx_vector_uint64_t *ids);
654 int	DBlock_record(const char *table, zbx_uint64_t id, const char *add_field, zbx_uint64_t add_id);
655 int	DBlock_records(const char *table, const zbx_vector_uint64_t *ids);
656 int	DBlock_ids(const char *table_name, const char *field_name, zbx_vector_uint64_t *ids);
657 
658 #define DBlock_hostid(id)			DBlock_record("hosts", id, NULL, 0)
659 #define DBlock_druleid(id)			DBlock_record("drules", id, NULL, 0)
660 #define DBlock_dcheckid(dcheckid, druleid)	DBlock_record("dchecks", dcheckid, "druleid", druleid)
661 #define DBlock_graphid(id)			DBlock_record("graphs", id, NULL, 0)
662 #define DBlock_hostids(ids)			DBlock_records("hosts", ids)
663 #define DBlock_triggerids(ids)			DBlock_records("triggers", ids)
664 #define DBlock_itemids(ids)			DBlock_records("items", ids)
665 #define DBlock_group_prototypeids(ids)		DBlock_records("group_prototype", ids)
666 
667 void	DBdelete_groups(zbx_vector_uint64_t *groupids);
668 
669 void	DBselect_uint64(const char *sql, zbx_vector_uint64_t *ids);
670 
671 void	DBcheck_character_set(void);
672 
673 /* bulk insert support */
674 
675 /* database bulk insert data */
676 typedef struct
677 {
678 	/* the target table */
679 	const ZBX_TABLE		*table;
680 	/* the fields to insert (pointers to the ZBX_FIELD structures from database schema) */
681 	zbx_vector_ptr_t	fields;
682 	/* the values rows to insert (pointers to arrays of zbx_db_value_t structures) */
683 	zbx_vector_ptr_t	rows;
684 	/* index of autoincrement field */
685 	int			autoincrement;
686 }
687 zbx_db_insert_t;
688 
689 void	zbx_db_insert_prepare_dyn(zbx_db_insert_t *self, const ZBX_TABLE *table, const ZBX_FIELD **fields,
690 		int fields_num);
691 void	zbx_db_insert_prepare(zbx_db_insert_t *self, const char *table, ...);
692 void	zbx_db_insert_add_values_dyn(zbx_db_insert_t *self, const zbx_db_value_t **values, int values_num);
693 void	zbx_db_insert_add_values(zbx_db_insert_t *self, ...);
694 int	zbx_db_insert_execute(zbx_db_insert_t *self);
695 void	zbx_db_insert_clean(zbx_db_insert_t *self);
696 void	zbx_db_insert_autoincrement(zbx_db_insert_t *self, const char *field_name);
697 int	zbx_db_get_database_type(void);
698 
699 /* agent (ZABBIX, SNMP, IPMI, JMX) availability data */
700 typedef struct
701 {
702 	/* flags specifying which fields are set, see ZBX_FLAGS_AGENT_STATUS_* defines */
703 	unsigned char	flags;
704 
705 	/* agent availability fields */
706 	unsigned char	available;
707 	char		*error;
708 	int		errors_from;
709 	int		disable_until;
710 }
711 zbx_agent_availability_t;
712 
713 #define ZBX_FLAGS_AGENT_STATUS_NONE		0x00000000
714 #define ZBX_FLAGS_AGENT_STATUS_AVAILABLE	0x00000001
715 #define ZBX_FLAGS_AGENT_STATUS_ERROR		0x00000002
716 #define ZBX_FLAGS_AGENT_STATUS_ERRORS_FROM	0x00000004
717 #define ZBX_FLAGS_AGENT_STATUS_DISABLE_UNTIL	0x00000008
718 
719 #define ZBX_FLAGS_AGENT_STATUS		(ZBX_FLAGS_AGENT_STATUS_AVAILABLE |	\
720 					ZBX_FLAGS_AGENT_STATUS_ERROR |		\
721 					ZBX_FLAGS_AGENT_STATUS_ERRORS_FROM |	\
722 					ZBX_FLAGS_AGENT_STATUS_DISABLE_UNTIL)
723 
724 #define ZBX_AGENT_ZABBIX	(INTERFACE_TYPE_AGENT - 1)
725 #define ZBX_AGENT_SNMP		(INTERFACE_TYPE_SNMP - 1)
726 #define ZBX_AGENT_IPMI		(INTERFACE_TYPE_IPMI - 1)
727 #define ZBX_AGENT_JMX		(INTERFACE_TYPE_JMX - 1)
728 #define ZBX_AGENT_UNKNOWN 	255
729 #define ZBX_AGENT_MAX		INTERFACE_TYPE_COUNT
730 
731 typedef struct
732 {
733 	zbx_uint64_t			hostid;
734 
735 	zbx_agent_availability_t	agents[ZBX_AGENT_MAX];
736 }
737 zbx_host_availability_t;
738 
739 int	zbx_sql_add_host_availability(char **sql, size_t *sql_alloc, size_t *sql_offset,
740 		const zbx_host_availability_t *ha);
741 int	DBget_user_by_active_session(const char *sessionid, zbx_user_t *user);
742 
743 typedef struct
744 {
745 	zbx_uint64_t	itemid;
746 	zbx_uint64_t	lastlogsize;
747 	unsigned char	state;
748 	int		mtime;
749 	int		lastclock;
750 	const char	*error;
751 
752 	zbx_uint64_t	flags;
753 #define ZBX_FLAGS_ITEM_DIFF_UNSET			__UINT64_C(0x0000)
754 #define ZBX_FLAGS_ITEM_DIFF_UPDATE_STATE		__UINT64_C(0x0001)
755 #define ZBX_FLAGS_ITEM_DIFF_UPDATE_ERROR		__UINT64_C(0x0002)
756 #define ZBX_FLAGS_ITEM_DIFF_UPDATE_MTIME		__UINT64_C(0x0004)
757 #define ZBX_FLAGS_ITEM_DIFF_UPDATE_LASTLOGSIZE		__UINT64_C(0x0008)
758 #define ZBX_FLAGS_ITEM_DIFF_UPDATE_LASTCLOCK		__UINT64_C(0x1000)
759 #define ZBX_FLAGS_ITEM_DIFF_UPDATE_DB			\
760 	(ZBX_FLAGS_ITEM_DIFF_UPDATE_STATE | ZBX_FLAGS_ITEM_DIFF_UPDATE_ERROR |\
761 	ZBX_FLAGS_ITEM_DIFF_UPDATE_MTIME | ZBX_FLAGS_ITEM_DIFF_UPDATE_LASTLOGSIZE)
762 #define ZBX_FLAGS_ITEM_DIFF_UPDATE	(ZBX_FLAGS_ITEM_DIFF_UPDATE_DB | ZBX_FLAGS_ITEM_DIFF_UPDATE_LASTCLOCK)
763 }
764 zbx_item_diff_t;
765 
766 /* event support */
767 void	zbx_db_get_events_by_eventids(zbx_vector_uint64_t *eventids, zbx_vector_ptr_t *events);
768 void	zbx_db_free_event(DB_EVENT *event);
769 void	zbx_db_get_eventid_r_eventid_pairs(zbx_vector_uint64_t *eventids, zbx_vector_uint64_pair_t *event_pairs,
770 		zbx_vector_uint64_t *r_eventids);
771 
772 void	zbx_db_trigger_clean(DB_TRIGGER *trigger);
773 
774 typedef struct
775 {
776 	zbx_uint64_t		hostid;
777 	unsigned char		compress;
778 	int			version;
779 	int			lastaccess;
780 	int			last_version_error_time;
781 	int			proxy_delay;
782 	int			more_data;
783 	zbx_proxy_suppress_t	nodata_win;
784 
785 #define ZBX_FLAGS_PROXY_DIFF_UNSET				__UINT64_C(0x0000)
786 #define ZBX_FLAGS_PROXY_DIFF_UPDATE_COMPRESS			__UINT64_C(0x0001)
787 #define ZBX_FLAGS_PROXY_DIFF_UPDATE_VERSION			__UINT64_C(0x0002)
788 #define ZBX_FLAGS_PROXY_DIFF_UPDATE_LASTACCESS			__UINT64_C(0x0004)
789 #define ZBX_FLAGS_PROXY_DIFF_UPDATE_LASTERROR			__UINT64_C(0x0008)
790 #define ZBX_FLAGS_PROXY_DIFF_UPDATE_PROXYDELAY			__UINT64_C(0x0010)
791 #define ZBX_FLAGS_PROXY_DIFF_UPDATE_SUPPRESS_WIN		__UINT64_C(0x0020)
792 #define ZBX_FLAGS_PROXY_DIFF_UPDATE_HEARTBEAT			__UINT64_C(0x0040)
793 #define ZBX_FLAGS_PROXY_DIFF_UPDATE_CONFIG			__UINT64_C(0x0080)
794 #define ZBX_FLAGS_PROXY_DIFF_UPDATE (			\
795 		ZBX_FLAGS_PROXY_DIFF_UPDATE_COMPRESS |	\
796 		ZBX_FLAGS_PROXY_DIFF_UPDATE_VERSION |	\
797 		ZBX_FLAGS_PROXY_DIFF_UPDATE_LASTACCESS)
798 	zbx_uint64_t	flags;
799 }
800 zbx_proxy_diff_t;
801 
802 int	zbx_db_lock_maintenanceids(zbx_vector_uint64_t *maintenanceids);
803 
804 void	zbx_db_save_item_changes(char **sql, size_t *sql_alloc, size_t *sql_offset, const zbx_vector_ptr_t *item_diff,
805 		zbx_uint64_t mask);
806 
807 /* mock field to estimate how much data can be stored in characters, bytes or both, */
808 /* depending on database backend                                                    */
809 
810 typedef struct
811 {
812 	int	bytes_num;
813 	int	chars_num;
814 }
815 zbx_db_mock_field_t;
816 
817 void	zbx_db_mock_field_init(zbx_db_mock_field_t *field, int field_type, int field_len);
818 int	zbx_db_mock_field_append(zbx_db_mock_field_t *field, const char *text);
819 
820 int	zbx_db_check_instanceid(void);
821 #endif
822