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_DBCACHE_H
21 #define ZABBIX_DBCACHE_H
22 
23 #include "db.h"
24 #include "comms.h"
25 #include "sysinfo.h"
26 #include "zbxalgo.h"
27 
28 #define ZBX_SYNC_DONE		0
29 #define	ZBX_SYNC_MORE		1
30 
31 #define	ZBX_NO_POLLER			255
32 #define	ZBX_POLLER_TYPE_NORMAL		0
33 #define	ZBX_POLLER_TYPE_UNREACHABLE	1
34 #define	ZBX_POLLER_TYPE_IPMI		2
35 #define	ZBX_POLLER_TYPE_PINGER		3
36 #define	ZBX_POLLER_TYPE_JAVA		4
37 #define	ZBX_POLLER_TYPE_COUNT		5	/* number of poller types */
38 
39 #define MAX_JAVA_ITEMS		32
40 #define MAX_SNMP_ITEMS		128
41 #define MAX_POLLER_ITEMS	128	/* MAX(MAX_JAVA_ITEMS, MAX_SNMP_ITEMS) */
42 #define MAX_PINGER_ITEMS	128
43 
44 #define ZBX_TRIGGER_DEPENDENCY_LEVELS_MAX	32
45 
46 #define ZBX_TRIGGER_DEPENDENCY_FAIL		1
47 #define ZBX_TRIGGER_DEPENDENCY_UNRESOLVED	2
48 
49 #define ZBX_SNMPTRAP_LOGGING_ENABLED	1
50 
51 extern int	CONFIG_TIMEOUT;
52 
53 extern zbx_uint64_t	CONFIG_CONF_CACHE_SIZE;
54 extern zbx_uint64_t	CONFIG_HISTORY_CACHE_SIZE;
55 extern zbx_uint64_t	CONFIG_HISTORY_INDEX_CACHE_SIZE;
56 extern zbx_uint64_t	CONFIG_TRENDS_CACHE_SIZE;
57 
58 extern int	CONFIG_POLLER_FORKS;
59 extern int	CONFIG_UNREACHABLE_POLLER_FORKS;
60 extern int	CONFIG_IPMIPOLLER_FORKS;
61 extern int	CONFIG_JAVAPOLLER_FORKS;
62 extern int	CONFIG_PINGER_FORKS;
63 extern int	CONFIG_UNAVAILABLE_DELAY;
64 extern int	CONFIG_UNREACHABLE_PERIOD;
65 extern int	CONFIG_UNREACHABLE_DELAY;
66 extern int	CONFIG_HISTSYNCER_FORKS;
67 extern int	CONFIG_PROXYCONFIG_FREQUENCY;
68 extern int	CONFIG_PROXYDATA_FREQUENCY;
69 
70 typedef struct
71 {
72 	zbx_uint64_t	interfaceid;
73 	char		ip_orig[INTERFACE_IP_LEN_MAX];
74 	char		dns_orig[INTERFACE_DNS_LEN_MAX];
75 	char		port_orig[INTERFACE_PORT_LEN_MAX];
76 	char		*addr;
77 	unsigned short	port;
78 	unsigned char	useip;
79 	unsigned char	type;
80 	unsigned char	main;
81 }
82 DC_INTERFACE;
83 
84 typedef struct
85 {
86 	zbx_uint64_t	interfaceid;
87 	char		*addr;
88 	unsigned char	type;
89 	unsigned char	main;
90 	unsigned char	bulk;
91 	unsigned char	useip;
92 	char		ip_orig[INTERFACE_IP_LEN_MAX];
93 	char		dns_orig[INTERFACE_DNS_LEN_MAX];
94 	char		port_orig[INTERFACE_PORT_LEN_MAX];
95 }
96 DC_INTERFACE2;
97 
98 typedef struct
99 {
100 	zbx_uint64_t	hostid;
101 	zbx_uint64_t	proxy_hostid;
102 	char		host[HOST_HOST_LEN_MAX];
103 	char		name[HOST_NAME_LEN * ZBX_MAX_BYTES_IN_UTF8_CHAR + 1];
104 	unsigned char	maintenance_status;
105 	unsigned char	maintenance_type;
106 	int		maintenance_from;
107 	int		errors_from;
108 	unsigned char	available;
109 	int		disable_until;
110 	int		snmp_errors_from;
111 	unsigned char	snmp_available;
112 	int		snmp_disable_until;
113 	int		ipmi_errors_from;
114 	unsigned char	ipmi_available;
115 	int		ipmi_disable_until;
116 	signed char	ipmi_authtype;
117 	unsigned char	ipmi_privilege;
118 	char		ipmi_username[HOST_IPMI_USERNAME_LEN_MAX];
119 	char		ipmi_password[HOST_IPMI_PASSWORD_LEN_MAX];
120 	int		jmx_errors_from;
121 	unsigned char	jmx_available;
122 	int		jmx_disable_until;
123 	char		inventory_mode;
124 	unsigned char	status;
125 	unsigned char	tls_connect;
126 	unsigned char	tls_accept;
127 #if defined(HAVE_POLARSSL) || defined(HAVE_GNUTLS) || defined(HAVE_OPENSSL)
128 	char		tls_issuer[HOST_TLS_ISSUER_LEN_MAX];
129 	char		tls_subject[HOST_TLS_SUBJECT_LEN_MAX];
130 	char		tls_psk_identity[HOST_TLS_PSK_IDENTITY_LEN_MAX];
131 	char		tls_psk[HOST_TLS_PSK_LEN_MAX];
132 #endif
133 	char		error[HOST_ERROR_LEN_MAX];
134 	char		snmp_error[HOST_ERROR_LEN_MAX];
135 	char		ipmi_error[HOST_ERROR_LEN_MAX];
136 	char		jmx_error[HOST_ERROR_LEN_MAX];
137 }
138 DC_HOST;
139 
140 typedef struct
141 {
142 	DC_HOST			host;
143 	DC_INTERFACE		interface;
144 	zbx_uint64_t		itemid;
145 	zbx_uint64_t		lastlogsize;
146 	zbx_uint64_t		valuemapid;
147 	unsigned char		type;
148 	unsigned char		value_type;
149 	unsigned char		state;
150 	unsigned char		snmpv3_securitylevel;
151 	unsigned char		authtype;
152 	unsigned char		flags;
153 	unsigned char		snmpv3_authprotocol;
154 	unsigned char		snmpv3_privprotocol;
155 	unsigned char		inventory_link;
156 	unsigned char		status;
157 	unsigned char		history;
158 	unsigned char		trends;
159 	unsigned char		follow_redirects;
160 	unsigned char		post_type;
161 	unsigned char		retrieve_mode;
162 	unsigned char		request_method;
163 	unsigned char		output_format;
164 	unsigned char		verify_peer;
165 	unsigned char		verify_host;
166 	unsigned char		allow_traps;
167 	char			key_orig[ITEM_KEY_LEN * ZBX_MAX_BYTES_IN_UTF8_CHAR + 1], *key;
168 	char			*units;
169 	char			*delay;
170 	int			history_sec;
171 	int			trends_sec;
172 	int			nextcheck;
173 	int			lastclock;
174 	int			mtime;
175 	char			trapper_hosts[ITEM_TRAPPER_HOSTS_LEN_MAX];
176 	char			logtimefmt[ITEM_LOGTIMEFMT_LEN_MAX];
177 	char			snmp_community_orig[ITEM_SNMP_COMMUNITY_LEN_MAX], *snmp_community;
178 	char			snmp_oid_orig[ITEM_SNMP_OID_LEN_MAX], *snmp_oid;
179 	char			snmpv3_securityname_orig[ITEM_SNMPV3_SECURITYNAME_LEN_MAX], *snmpv3_securityname;
180 	char			snmpv3_authpassphrase_orig[ITEM_SNMPV3_AUTHPASSPHRASE_LEN_MAX], *snmpv3_authpassphrase;
181 	char			snmpv3_privpassphrase_orig[ITEM_SNMPV3_PRIVPASSPHRASE_LEN_MAX], *snmpv3_privpassphrase;
182 	char			ipmi_sensor[ITEM_IPMI_SENSOR_LEN_MAX];
183 	char			*params;
184 	char			username_orig[ITEM_USERNAME_LEN_MAX], *username;
185 	char			publickey_orig[ITEM_PUBLICKEY_LEN_MAX], *publickey;
186 	char			privatekey_orig[ITEM_PRIVATEKEY_LEN_MAX], *privatekey;
187 	char			password_orig[ITEM_PASSWORD_LEN_MAX], *password;
188 	char			snmpv3_contextname_orig[ITEM_SNMPV3_CONTEXTNAME_LEN_MAX], *snmpv3_contextname;
189 	char			jmx_endpoint_orig[ITEM_JMX_ENDPOINT_LEN_MAX], *jmx_endpoint;
190 	char			timeout_orig[ITEM_TIMEOUT_LEN_MAX], *timeout;
191 	char			url_orig[ITEM_URL_LEN_MAX], *url;
192 	char			query_fields_orig[ITEM_QUERY_FIELDS_LEN_MAX], *query_fields;
193 	char			*posts;
194 	char			status_codes_orig[ITEM_STATUS_CODES_LEN_MAX], *status_codes;
195 	char			http_proxy_orig[ITEM_HTTP_PROXY_LEN_MAX], *http_proxy;
196 	char			*headers;
197 	char			ssl_cert_file_orig[ITEM_SSL_CERT_FILE_LEN_MAX], *ssl_cert_file;
198 	char			ssl_key_file_orig[ITEM_SSL_KEY_FILE_LEN_MAX], *ssl_key_file;
199 	char			ssl_key_password_orig[ITEM_SSL_KEY_PASSWORD_LEN_MAX], *ssl_key_password;
200 	char			*error;
201 }
202 DC_ITEM;
203 
204 typedef struct
205 {
206 	zbx_uint64_t	functionid;
207 	zbx_uint64_t	triggerid;
208 	zbx_uint64_t	itemid;
209 	char		*function;
210 	char		*parameter;
211 }
212 DC_FUNCTION;
213 
214 typedef struct
215 {
216 	char	*tag;
217 	char	*value;
218 }
219 zbx_tag_t;
220 
221 #define ZBX_DC_TRIGGER_PROBLEM_EXPRESSION	0x1	/* this flag shows that trigger value recalculation is  */
222 							/* initiated by a time-based function or a new value of */
223 							/* an item in problem expression */
224 
225 typedef struct _DC_TRIGGER
226 {
227 	zbx_uint64_t		triggerid;
228 	char			*description;
229 	char			*expression_orig;
230 	char			*recovery_expression_orig;
231 	/* temporary values, allocated during processing and freed right after */
232 	char			*expression;
233 	char			*recovery_expression;
234 
235 	char			*error;
236 	char			*new_error;
237 	char			*correlation_tag;
238 	zbx_timespec_t		timespec;
239 	int			lastchange;
240 	unsigned char		topoindex;
241 	unsigned char		priority;
242 	unsigned char		type;
243 	unsigned char		value;
244 	unsigned char		state;
245 	unsigned char		new_value;
246 	unsigned char		status;
247 	unsigned char		recovery_mode;
248 	unsigned char		correlation_mode;
249 
250 	unsigned char		flags;
251 
252 	zbx_vector_ptr_t	tags;
253 }
254 DC_TRIGGER;
255 
256 typedef struct
257 {
258 	zbx_uint64_t	hostid;
259 	char		host[HOST_HOST_LEN_MAX];
260 	int		proxy_config_nextcheck;
261 	int		proxy_data_nextcheck;
262 	int		proxy_tasks_nextcheck;
263 	int		last_cfg_error_time;	/* time when passive proxy misconfiguration error was seen */
264 						/* or 0 if no error */
265 	int		version;
266 	int		lastaccess;
267 	char		addr_orig[INTERFACE_ADDR_LEN_MAX];
268 	char		port_orig[INTERFACE_PORT_LEN_MAX];
269 	char		*addr;
270 	unsigned short	port;
271 
272 	unsigned char	auto_compress;
273 	unsigned char	tls_connect;
274 	unsigned char	tls_accept;
275 
276 #if defined(HAVE_POLARSSL) || defined(HAVE_GNUTLS) || defined(HAVE_OPENSSL)
277 	char		tls_issuer[HOST_TLS_ISSUER_LEN_MAX];
278 	char		tls_subject[HOST_TLS_SUBJECT_LEN_MAX];
279 	char		tls_psk_identity[HOST_TLS_PSK_IDENTITY_LEN_MAX];
280 	char		tls_psk[HOST_TLS_PSK_LEN_MAX];
281 #endif
282 	char		proxy_address[HOST_PROXY_ADDRESS_LEN_MAX];
283 	int		last_version_error_time;
284 }
285 DC_PROXY;
286 
287 #define ZBX_ACTION_OPCLASS_NONE			0
288 #define ZBX_ACTION_OPCLASS_NORMAL		1
289 #define ZBX_ACTION_OPCLASS_RECOVERY		2
290 #define ZBX_ACTION_OPCLASS_ACKNOWLEDGE		4
291 
292 typedef struct
293 {
294 	zbx_uint64_t		actionid;
295 	char			*formula;
296 	unsigned char		eventsource;
297 	unsigned char		evaltype;
298 	unsigned char		opflags;
299 	zbx_vector_ptr_t	conditions;
300 }
301 zbx_action_eval_t;
302 
303 typedef struct
304 {
305 	char	*host;
306 	char	*key;
307 }
308 zbx_host_key_t;
309 
310 /* housekeeping related configuration data */
311 typedef struct
312 {
313 	int		events_trigger;
314 	int		events_internal;
315 	int		events_discovery;
316 	int		events_autoreg;
317 	int		services;
318 	int		audit;
319 	int		sessions;
320 	int		trends;
321 	int		history;
322 
323 	unsigned char	services_mode;
324 	unsigned char	audit_mode;
325 	unsigned char	sessions_mode;
326 	unsigned char	events_mode;
327 	unsigned char	trends_mode;
328 	unsigned char	trends_global;
329 	unsigned char	history_mode;
330 	unsigned char	history_global;
331 }
332 zbx_config_hk_t;
333 
334 /* global configuration data (loaded from config table) */
335 typedef struct
336 {
337 	/* the fields set by zbx_config_get() function, see ZBX_CONFIG_FLAGS_ defines */
338 	zbx_uint64_t	flags;
339 
340 	char		**severity_name;
341 	zbx_uint64_t	discovery_groupid;
342 	int		default_inventory_mode;
343 	int		refresh_unsupported;
344 	unsigned char	snmptrap_logging;
345 
346 	/* housekeeping related configuration data */
347 	zbx_config_hk_t	hk;
348 }
349 zbx_config_t;
350 
351 #define ZBX_CONFIG_FLAGS_SEVERITY_NAME			0x00000001
352 #define ZBX_CONFIG_FLAGS_DISCOVERY_GROUPID		0x00000002
353 #define ZBX_CONFIG_FLAGS_DEFAULT_INVENTORY_MODE		0x00000004
354 #define ZBX_CONFIG_FLAGS_REFRESH_UNSUPPORTED		0x00000008
355 #define ZBX_CONFIG_FLAGS_SNMPTRAP_LOGGING		0x00000010
356 #define ZBX_CONFIG_FLAGS_HOUSEKEEPER			0x00000020
357 
358 typedef struct
359 {
360 	zbx_uint64_t	hostid;
361 	unsigned char	idx;
362 	const char	*field_name;
363 	char		*value;
364 }
365 zbx_inventory_value_t;
366 
367 typedef struct
368 {
369 	char	*tag;
370 }
371 zbx_corr_condition_tag_t;
372 
373 typedef struct
374 {
375 	char		*tag;
376 	char		*value;
377 	unsigned char	op;
378 }
379 zbx_corr_condition_tag_value_t;
380 
381 typedef struct
382 {
383 	zbx_uint64_t	groupid;
384 	unsigned char	op;
385 }
386 zbx_corr_condition_group_t;
387 
388 typedef struct
389 {
390 	char	*oldtag;
391 	char	*newtag;
392 }
393 zbx_corr_condition_tag_pair_t;
394 
395 typedef union
396 {
397 	zbx_corr_condition_tag_t	tag;
398 	zbx_corr_condition_tag_value_t	tag_value;
399 	zbx_corr_condition_group_t	group;
400 	zbx_corr_condition_tag_pair_t	tag_pair;
401 }
402 zbx_corr_condition_data_t;
403 
404 typedef struct
405 {
406 	zbx_uint64_t			corr_conditionid;
407 	int				type;
408 	zbx_corr_condition_data_t	data;
409 }
410 zbx_corr_condition_t;
411 
412 typedef struct
413 {
414 	unsigned char	type;
415 }
416 zbx_corr_operation_t;
417 
418 typedef struct
419 {
420 	zbx_uint64_t		correlationid;
421 	char			*name;
422 	char			*formula;
423 	unsigned char		evaltype;
424 
425 	zbx_vector_ptr_t	conditions;
426 	zbx_vector_ptr_t	operations;
427 }
428 zbx_correlation_t;
429 
430 typedef struct
431 {
432 	zbx_vector_ptr_t	correlations;
433 	zbx_hashset_t		conditions;
434 
435 	/* Configuration synchronization timestamp of the rules. */
436 	/* Update the cache if this timesamp is less than the    */
437 	/* current configuration synchronization timestamp.      */
438 	int			sync_ts;
439 }
440 zbx_correlation_rules_t;
441 
442 /* value_avg_t structure is used for item average value trend calculations. */
443 /*                                                                          */
444 /* For double values the average value is calculated on the fly with the    */
445 /* following formula: avg = (dbl * count + value) / (count + 1) and stored  */
446 /* into dbl member.                                                         */
447 /* For uint64 values the item values are summed into ui64 member and the    */
448 /* average value is calculated before flushing trends to database:          */
449 /* avg = ui64 / count                                                       */
450 typedef union
451 {
452 	double		dbl;
453 	zbx_uint128_t	ui64;
454 }
455 value_avg_t;
456 
457 typedef struct
458 {
459 	zbx_uint64_t	itemid;
460 	history_value_t	value_min;
461 	value_avg_t	value_avg;
462 	history_value_t	value_max;
463 	int		clock;
464 	int		num;
465 	int		disable_from;
466 	unsigned char	value_type;
467 }
468 ZBX_DC_TREND;
469 
470 typedef struct
471 {
472 	zbx_uint64_t		itemid;
473 	zbx_timespec_t		timestamp;
474 	zbx_variant_t		value;
475 	unsigned char		value_type;
476 }
477 zbx_item_history_value_t;
478 
479 typedef struct
480 {
481 	zbx_uint64_t	itemid;
482 	history_value_t	value;
483 	zbx_uint64_t	lastlogsize;
484 	zbx_timespec_t	ts;
485 	int		mtime;
486 	unsigned char	value_type;
487 	unsigned char	flags;		/* see ZBX_DC_FLAG_* */
488 	unsigned char	state;
489 	int		ttl;		/* time-to-live of the history value */
490 }
491 ZBX_DC_HISTORY;
492 
493 /* item queue data */
494 typedef struct
495 {
496 	zbx_uint64_t	itemid;
497 	zbx_uint64_t	proxy_hostid;
498 	int		type;
499 	int		nextcheck;
500 }
501 zbx_queue_item_t;
502 
503 typedef union
504 {
505 	zbx_uint64_t	ui64;
506 	double		dbl;
507 }
508 zbx_counter_value_t;
509 
510 typedef struct
511 {
512 	zbx_uint64_t		proxyid;
513 	zbx_counter_value_t	counter_value;
514 }
515 zbx_proxy_counter_t;
516 
517 typedef enum
518 {
519 	ZBX_COUNTER_TYPE_UI64,
520 	ZBX_COUNTER_TYPE_DBL
521 }
522 zbx_counter_type_t;
523 
524 typedef struct
525 {
526 	unsigned char	type;
527 	char		*params;
528 }
529 zbx_preproc_op_t;
530 
531 typedef struct
532 {
533 	zbx_uint64_t		itemid;
534 	unsigned char		type;
535 	unsigned char		value_type;
536 
537 	int			dep_itemids_num;
538 	int			preproc_ops_num;
539 
540 	zbx_uint64_t		*dep_itemids;
541 	zbx_preproc_op_t	*preproc_ops;
542 }
543 zbx_preproc_item_t;
544 
545 /* the configuration cache statistics */
546 typedef struct
547 {
548 	zbx_uint64_t	hosts;
549 	zbx_uint64_t	items;
550 	zbx_uint64_t	items_unsupported;
551 	double		requiredperformance;
552 }
553 zbx_config_cache_info_t;
554 
555 typedef struct
556 {
557 	zbx_uint64_t	history_counter;	/* the total number of processed values */
558 	zbx_uint64_t	history_float_counter;	/* the number of processed float values */
559 	zbx_uint64_t	history_uint_counter;	/* the number of processed uint values */
560 	zbx_uint64_t	history_str_counter;	/* the number of processed str values */
561 	zbx_uint64_t	history_log_counter;	/* the number of processed log values */
562 	zbx_uint64_t	history_text_counter;	/* the number of processed text values */
563 	zbx_uint64_t	notsupported_counter;	/* the number of processed not supported items */
564 }
565 ZBX_DC_STATS;
566 
567 /* the write cache statistics */
568 typedef struct
569 {
570 	ZBX_DC_STATS	stats;
571 	zbx_uint64_t	history_free;
572 	zbx_uint64_t	history_total;
573 	zbx_uint64_t	index_free;
574 	zbx_uint64_t	index_total;
575 	zbx_uint64_t	trend_free;
576 	zbx_uint64_t	trend_total;
577 }
578 zbx_wcache_info_t;
579 
580 int	is_item_processed_by_server(unsigned char type, const char *key);
581 int	in_maintenance_without_data_collection(unsigned char maintenance_status, unsigned char maintenance_type,
582 		unsigned char type);
583 void	dc_add_history(zbx_uint64_t itemid, unsigned char item_value_type, unsigned char item_flags,
584 		AGENT_RESULT *result, const zbx_timespec_t *ts, unsigned char state, const char *error);
585 void	dc_flush_history(void);
586 void	zbx_sync_history_cache(int *values_num, int *triggers_num, int *more);
587 void	zbx_log_sync_history_cache_progress(void);
588 int	init_database_cache(char **error);
589 void	free_database_cache(void);
590 
591 #define ZBX_STATS_HISTORY_COUNTER	0
592 #define ZBX_STATS_HISTORY_FLOAT_COUNTER	1
593 #define ZBX_STATS_HISTORY_UINT_COUNTER	2
594 #define ZBX_STATS_HISTORY_STR_COUNTER	3
595 #define ZBX_STATS_HISTORY_LOG_COUNTER	4
596 #define ZBX_STATS_HISTORY_TEXT_COUNTER	5
597 #define ZBX_STATS_NOTSUPPORTED_COUNTER	6
598 #define ZBX_STATS_HISTORY_TOTAL		7
599 #define ZBX_STATS_HISTORY_USED		8
600 #define ZBX_STATS_HISTORY_FREE		9
601 #define ZBX_STATS_HISTORY_PUSED		10
602 #define ZBX_STATS_HISTORY_PFREE		11
603 #define ZBX_STATS_TREND_TOTAL		12
604 #define ZBX_STATS_TREND_USED		13
605 #define ZBX_STATS_TREND_FREE		14
606 #define ZBX_STATS_TREND_PUSED		15
607 #define ZBX_STATS_TREND_PFREE		16
608 #define ZBX_STATS_HISTORY_INDEX_TOTAL	17
609 #define ZBX_STATS_HISTORY_INDEX_USED	18
610 #define ZBX_STATS_HISTORY_INDEX_FREE	19
611 #define ZBX_STATS_HISTORY_INDEX_PUSED	20
612 #define ZBX_STATS_HISTORY_INDEX_PFREE	21
613 void	*DCget_stats(int request);
614 void	DCget_stats_all(zbx_wcache_info_t *wcache_info);
615 
616 zbx_uint64_t	DCget_nextid(const char *table_name, int num);
617 
618 /* initial sync, get all data */
619 #define ZBX_DBSYNC_INIT		0
620 /* update sync, get changed data */
621 #define ZBX_DBSYNC_UPDATE	1
622 
623 void	DCsync_configuration(unsigned char mode);
624 int	init_configuration_cache(char **error);
625 void	free_configuration_cache(void);
626 
627 void	DCconfig_get_triggers_by_triggerids(DC_TRIGGER *triggers, const zbx_uint64_t *triggerids, int *errcode,
628 		size_t num);
629 void	DCconfig_clean_items(DC_ITEM *items, int *errcodes, size_t num);
630 int	DCget_host_by_hostid(DC_HOST *host, zbx_uint64_t hostid);
631 void	DCconfig_get_hosts_by_itemids(DC_HOST *hosts, const zbx_uint64_t *itemids, int *errcodes, size_t num);
632 void	DCconfig_get_items_by_keys(DC_ITEM *items, zbx_host_key_t *keys, int *errcodes, size_t num);
633 void	DCconfig_get_items_by_itemids(DC_ITEM *items, const zbx_uint64_t *itemids, int *errcodes, size_t num);
634 void	DCconfig_get_preprocessable_items(zbx_hashset_t *items, int *timestamp);
635 void	DCconfig_get_functions_by_functionids(DC_FUNCTION *functions,
636 		zbx_uint64_t *functionids, int *errcodes, size_t num);
637 void	DCconfig_clean_functions(DC_FUNCTION *functions, int *errcodes, size_t num);
638 void	DCconfig_clean_triggers(DC_TRIGGER *triggers, int *errcodes, size_t num);
639 int	DCconfig_lock_triggers_by_history_items(zbx_vector_ptr_t *history_items, zbx_vector_uint64_t *triggerids);
640 void	DCconfig_lock_triggers_by_triggerids(zbx_vector_uint64_t *triggerids_in, zbx_vector_uint64_t *triggerids_out);
641 void	DCconfig_unlock_triggers(const zbx_vector_uint64_t *triggerids);
642 void	DCconfig_unlock_all_triggers(void);
643 int	DCconfig_lock_lld_rule(zbx_uint64_t lld_ruleid);
644 void	DCconfig_unlock_lld_rule(zbx_uint64_t lld_ruleid);
645 void	DCconfig_get_triggers_by_itemids(zbx_hashset_t *trigger_info, zbx_vector_ptr_t *trigger_order,
646 		const zbx_uint64_t *itemids, const zbx_timespec_t *timespecs, int itemids_num);
647 void	DCfree_triggers(zbx_vector_ptr_t *triggers);
648 void	DCconfig_update_interface_snmp_stats(zbx_uint64_t interfaceid, int max_snmp_succeed, int min_snmp_fail);
649 int	DCconfig_get_suggested_snmp_vars(zbx_uint64_t interfaceid, int *bulk);
650 int	DCconfig_get_interface_by_type(DC_INTERFACE *interface, zbx_uint64_t hostid, unsigned char type);
651 int	DCconfig_get_interface(DC_INTERFACE *interface, zbx_uint64_t hostid, zbx_uint64_t itemid);
652 int	DCconfig_get_poller_nextcheck(unsigned char poller_type);
653 int	DCconfig_get_poller_items(unsigned char poller_type, DC_ITEM **items);
654 int	DCconfig_get_ipmi_poller_items(int now, DC_ITEM *items, int items_num, int *nextcheck);
655 int	DCconfig_get_snmp_interfaceids_by_addr(const char *addr, zbx_uint64_t **interfaceids);
656 size_t	DCconfig_get_snmp_items_by_interfaceid(zbx_uint64_t interfaceid, DC_ITEM **items);
657 
658 #define ZBX_HK_OPTION_DISABLED		0
659 #define ZBX_HK_OPTION_ENABLED		1
660 
661 #define ZBX_HK_HISTORY_MIN	SEC_PER_HOUR
662 #define ZBX_HK_TRENDS_MIN	SEC_PER_DAY
663 #define ZBX_HK_PERIOD_MAX	(25 * SEC_PER_YEAR)
664 
665 void	DCrequeue_items(const zbx_uint64_t *itemids, const unsigned char *states, const int *lastclocks,
666 		const int *errcodes, size_t num);
667 void	DCpoller_requeue_items(const zbx_uint64_t *itemids, const unsigned char *states, const int *lastclocks,
668 		const int *errcodes, size_t num, unsigned char poller_type, int *nextcheck);
669 void	zbx_dc_requeue_unreachable_items(zbx_uint64_t *itemids, size_t itemids_num);
670 int	DCconfig_activate_host(DC_ITEM *item);
671 int	DCconfig_deactivate_host(DC_ITEM *item, int now);
672 
673 int	DCconfig_check_trigger_dependencies(zbx_uint64_t triggerid);
674 
675 void	DCconfig_triggers_apply_changes(zbx_vector_ptr_t *trigger_diff);
676 void	DCconfig_items_apply_changes(const zbx_vector_ptr_t *item_diff);
677 
678 void	DCconfig_update_inventory_values(const zbx_vector_ptr_t *inventory_values);
679 int	DCget_host_inventory_value_by_itemid(zbx_uint64_t itemid, char **replace_to, int value_idx);
680 
681 #define ZBX_CONFSTATS_BUFFER_TOTAL	1
682 #define ZBX_CONFSTATS_BUFFER_USED	2
683 #define ZBX_CONFSTATS_BUFFER_FREE	3
684 #define ZBX_CONFSTATS_BUFFER_PUSED	4
685 #define ZBX_CONFSTATS_BUFFER_PFREE	5
686 void	*DCconfig_get_stats(int request);
687 
688 int	DCconfig_get_last_sync_time(void);
689 void	DCconfig_wait_sync(void);
690 int	DCconfig_get_proxypoller_hosts(DC_PROXY *proxies, int max_hosts);
691 int	DCconfig_get_proxypoller_nextcheck(void);
692 
693 #define ZBX_PROXY_CONFIG_NEXTCHECK	0x01
694 #define ZBX_PROXY_DATA_NEXTCHECK	0x02
695 #define ZBX_PROXY_TASKS_NEXTCHECK	0x04
696 void	DCrequeue_proxy(zbx_uint64_t hostid, unsigned char update_nextcheck, int proxy_conn_err);
697 int	DCcheck_proxy_permissions(const char *host, const zbx_socket_t *sock, zbx_uint64_t *hostid, char **error);
698 
699 #if defined(HAVE_POLARSSL) || defined(HAVE_GNUTLS) || defined(HAVE_OPENSSL)
700 size_t	DCget_psk_by_identity(const unsigned char *psk_identity, unsigned char *psk_buf, size_t psk_buf_len);
701 #endif
702 
703 void	DCget_user_macro(const zbx_uint64_t *hostids, int hostids_num, const char *macro, char **replace_to);
704 char	*DCexpression_expand_user_macros(const char *expression);
705 
706 int	DChost_activate(zbx_uint64_t hostid, unsigned char agent_type, const zbx_timespec_t *ts,
707 		zbx_agent_availability_t *in, zbx_agent_availability_t *out);
708 
709 int	DChost_deactivate(zbx_uint64_t hostid, unsigned char agent_type, const zbx_timespec_t *ts,
710 		zbx_agent_availability_t *in, zbx_agent_availability_t *out, const char *error_msg);
711 
712 #define ZBX_QUEUE_FROM_DEFAULT	6	/* default lower limit for delay (in seconds) */
713 #define ZBX_QUEUE_TO_INFINITY	-1	/* no upper limit for delay */
714 void	DCfree_item_queue(zbx_vector_ptr_t *queue);
715 int	DCget_item_queue(zbx_vector_ptr_t *queue, int from, int to);
716 
717 zbx_uint64_t	DCget_item_count(zbx_uint64_t hostid);
718 zbx_uint64_t	DCget_item_unsupported_count(zbx_uint64_t hostid);
719 zbx_uint64_t	DCget_trigger_count(void);
720 double		DCget_required_performance(void);
721 zbx_uint64_t	DCget_host_count(void);
722 void		DCget_count_stats_all(zbx_config_cache_info_t *stats);
723 
724 void	DCget_status(zbx_vector_ptr_t *hosts_monitored, zbx_vector_ptr_t *hosts_not_monitored,
725 		zbx_vector_ptr_t *items_active_normal, zbx_vector_ptr_t *items_active_notsupported,
726 		zbx_vector_ptr_t *items_disabled, zbx_uint64_t *triggers_enabled_ok,
727 		zbx_uint64_t *triggers_enabled_problem, zbx_uint64_t *triggers_disabled,
728 		zbx_vector_ptr_t *required_performance);
729 
730 void	DCget_expressions_by_names(zbx_vector_ptr_t *expressions, const char * const *names, int names_num);
731 void	DCget_expressions_by_name(zbx_vector_ptr_t *expressions, const char *name);
732 
733 int	DCget_data_expected_from(zbx_uint64_t itemid, int *seconds);
734 
735 void	DCget_hostids_by_functionids(zbx_vector_uint64_t *functionids, zbx_vector_uint64_t *hostids);
736 void	DCget_hosts_by_functionids(const zbx_vector_uint64_t *functionids, zbx_hashset_t *hosts);
737 
738 unsigned int	DCget_internal_action_count(void);
739 
740 /* global configuration support */
741 #define ZBX_DISCOVERY_GROUPID_UNDEFINED	0
742 void	zbx_config_get(zbx_config_t *cfg, zbx_uint64_t flags);
743 void	zbx_config_clean(zbx_config_t *cfg);
744 
745 int	DCset_hosts_availability(zbx_vector_ptr_t *availabilities);
746 
747 int	DCreset_hosts_availability(zbx_vector_ptr_t *hosts);
748 void	DCupdate_hosts_availability(void);
749 
750 void	zbx_dc_get_actions_eval(zbx_vector_ptr_t *actions, unsigned char opflags);
751 
752 int	DCget_hosts_availability(zbx_vector_ptr_t *hosts, int *ts);
753 void	DCtouch_hosts_availability(const zbx_vector_uint64_t *hostids);
754 
755 void	zbx_host_availability_init(zbx_host_availability_t *availability, zbx_uint64_t hostid);
756 void	zbx_host_availability_clean(zbx_host_availability_t *ha);
757 void	zbx_host_availability_free(zbx_host_availability_t *availability);
758 int	zbx_host_availability_is_set(const zbx_host_availability_t *ha);
759 
760 void	zbx_set_availability_diff_ts(int ts);
761 
762 void	zbx_dc_correlation_rules_init(zbx_correlation_rules_t *rules);
763 void	zbx_dc_correlation_rules_clean(zbx_correlation_rules_t *rules);
764 void	zbx_dc_correlation_rules_free(zbx_correlation_rules_t *rules);
765 void	zbx_dc_correlation_rules_get(zbx_correlation_rules_t *rules);
766 
767 void	zbx_dc_get_nested_hostgroupids(zbx_uint64_t *groupids, int groupids_num, zbx_vector_uint64_t *nested_groupids);
768 void	zbx_dc_get_nested_hostgroupids_by_names(zbx_vector_str_t *groups, zbx_vector_uint64_t *nested_groupids);
769 
770 #define ZBX_HC_ITEM_STATUS_NORMAL	0
771 #define ZBX_HC_ITEM_STATUS_BUSY		1
772 
773 #define ZBX_DC_FLAG_META	0x01	/* contains meta information (lastlogsize and mtime) */
774 #define ZBX_DC_FLAG_NOVALUE	0x02	/* entry contains no value */
775 #define ZBX_DC_FLAG_LLD		0x04	/* low-level discovery value */
776 #define ZBX_DC_FLAG_UNDEF	0x08	/* unsupported or undefined (delta calculation failed) value */
777 #define ZBX_DC_FLAG_NOHISTORY	0x10	/* values should not be kept in history */
778 #define ZBX_DC_FLAG_NOTRENDS	0x20	/* values should not be kept in trends */
779 
780 typedef struct zbx_hc_data
781 {
782 	history_value_t	value;
783 	zbx_uint64_t	lastlogsize;
784 	zbx_timespec_t	ts;
785 	int		mtime;
786 	unsigned char	value_type;
787 	unsigned char	flags;
788 	unsigned char	state;
789 
790 	struct zbx_hc_data	*next;
791 }
792 zbx_hc_data_t;
793 
794 typedef struct
795 {
796 	zbx_uint64_t	itemid;
797 	unsigned char	status;
798 
799 	zbx_hc_data_t	*tail;
800 	zbx_hc_data_t	*head;
801 }
802 zbx_hc_item_t;
803 
804 void	zbx_free_tag(zbx_tag_t *tag);
805 
806 int	zbx_dc_get_active_proxy_by_name(const char *name, DC_PROXY *proxy, char **error);
807 void	zbx_dc_update_proxy_version(zbx_uint64_t hostid, int version);
808 
809 typedef struct
810 {
811 	zbx_timespec_t	ts;
812 	char		*value;	/* NULL in case of meta record (see "meta" field below) */
813 	char		*source;
814 	zbx_uint64_t	lastlogsize;
815 	zbx_uint64_t	id;
816 	int		mtime;
817 	int		timestamp;
818 	int		severity;
819 	int		logeventid;
820 	unsigned char	state;
821 	unsigned char	meta;	/* non-zero if contains meta information (lastlogsize and mtime) */
822 }
823 zbx_agent_value_t;
824 
825 void	zbx_dc_items_update_nextcheck(DC_ITEM *items, zbx_agent_value_t *values, int *errcodes, size_t values_num);
826 int	zbx_dc_get_host_interfaces(zbx_uint64_t hostid, DC_INTERFACE2 **interfaces, int *n);
827 
828 void	zbx_dc_update_proxy(zbx_proxy_diff_t *diff);
829 void	zbx_dc_get_proxy_lastaccess(zbx_vector_uint64_pair_t *lastaccess);
830 
831 typedef struct
832 {
833 	zbx_uint64_t		triggerid;
834 	unsigned char		status;
835 	zbx_vector_uint64_t	masterids;
836 }
837 zbx_trigger_dep_t;
838 
839 void	zbx_dc_get_trigger_dependencies(const zbx_vector_uint64_t *triggerids, zbx_vector_ptr_t *deps);
840 
841 void	zbx_dc_reschedule_items(const zbx_vector_uint64_t *itemids, int nextcheck, zbx_uint64_t *proxy_hostids);
842 
843 void	zbx_dc_get_timer_triggerids(zbx_vector_uint64_t *triggerids, int now, int limit);
844 void	zbx_dc_get_timer_triggers_by_triggerids(zbx_hashset_t *trigger_info, zbx_vector_ptr_t *trigger_order,
845 		const zbx_vector_uint64_t *triggerids, const zbx_timespec_t *ts);
846 void	zbx_dc_clear_timer_queue(void);
847 
848 /* data session support */
849 
850 typedef struct
851 {
852 	zbx_uint64_t	hostid;
853 	zbx_uint64_t	last_valueid;
854 	const char	*token;
855 	time_t		lastaccess;
856 }
857 zbx_data_session_t;
858 
859 const char	*zbx_dc_get_session_token(void);
860 zbx_data_session_t	*zbx_dc_get_or_create_data_session(zbx_uint64_t hostid, const char *token);
861 void	zbx_dc_cleanup_data_sessions(void);
862 
863 /* maintenance support */
864 
865 typedef struct
866 {
867 	zbx_uint64_t	hostid;
868 	zbx_uint64_t	maintenanceid;
869 	int		maintenance_from;
870 	unsigned char	maintenance_type;
871 	unsigned char	maintenance_status;
872 
873 	unsigned int	flags;
874 #define ZBX_FLAG_HOST_MAINTENANCE_UPDATE_MAINTENANCEID		0x0001
875 #define ZBX_FLAG_HOST_MAINTENANCE_UPDATE_MAINTENANCE_FROM	0x0002
876 #define ZBX_FLAG_HOST_MAINTENANCE_UPDATE_MAINTENANCE_TYPE	0x0003
877 #define ZBX_FLAG_HOST_MAINTENANCE_UPDATE_MAINTENANCE_STATUS	0x0004
878 }
879 zbx_host_maintenance_diff_t;
880 
881 /* event maintenance query data, used to get event maintenances from cache */
882 typedef struct
883 {
884 	zbx_uint64_t			eventid;		/* [IN] eventid */
885 	zbx_uint64_t			r_eventid;		/* [-] recovery eventid */
886 	zbx_uint64_t			triggerid;		/* [-] triggerid */
887 	zbx_vector_uint64_t		functionids;		/* [IN] associated functionids */
888 	zbx_vector_ptr_t		tags;			/* [IN] event tags */
889 	zbx_vector_uint64_pair_t	maintenances;		/* [OUT] actual maintenance data for the event in */
890 								/* (maintenanceid, suppress_until) pairs */
891 }
892 zbx_event_suppress_query_t;
893 
894 #define ZBX_MAINTENANCE_UPDATE_TRUE	1
895 #define ZBX_MAINTENANCE_UPDATE_FALSE	0
896 
897 void	zbx_event_suppress_query_free(zbx_event_suppress_query_t *query);
898 int	zbx_dc_update_maintenances(void);
899 void	zbx_dc_get_host_maintenance_updates(const zbx_vector_uint64_t *maintenanceids, zbx_vector_ptr_t *updates);
900 void	zbx_dc_flush_host_maintenance_updates(const zbx_vector_ptr_t *updates);
901 int	zbx_dc_get_event_maintenances(zbx_vector_ptr_t *event_queries, const zbx_vector_uint64_t *maintenanceids);
902 int	zbx_dc_get_running_maintenanceids(zbx_vector_uint64_t *maintenanceids);
903 
904 void	zbx_dc_maintenance_set_update_flags(void);
905 void	zbx_dc_maintenance_reset_update_flag(int timer);
906 int	zbx_dc_maintenance_check_update_flag(int timer);
907 int	zbx_dc_maintenance_check_update_flags(void);
908 
909 #endif
910