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_PARTIAL 0 29 #define ZBX_SYNC_FULL 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_SNMPTRAP_LOGGING_ENABLED 1 47 48 #define ZBX_EXPAND_MACROS 1 49 50 extern char *CONFIG_FILE; 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 hostid; 87 zbx_uint64_t proxy_hostid; 88 char host[HOST_HOST_LEN_MAX]; 89 char name[HOST_NAME_LEN * ZBX_MAX_BYTES_IN_UTF8_CHAR + 1]; 90 unsigned char maintenance_status; 91 unsigned char maintenance_type; 92 int maintenance_from; 93 int errors_from; 94 unsigned char available; 95 int disable_until; 96 int snmp_errors_from; 97 unsigned char snmp_available; 98 int snmp_disable_until; 99 int ipmi_errors_from; 100 unsigned char ipmi_available; 101 int ipmi_disable_until; 102 signed char ipmi_authtype; 103 unsigned char ipmi_privilege; 104 char ipmi_username[HOST_IPMI_USERNAME_LEN_MAX]; 105 char ipmi_password[HOST_IPMI_PASSWORD_LEN_MAX]; 106 int jmx_errors_from; 107 unsigned char jmx_available; 108 int jmx_disable_until; 109 char inventory_mode; 110 unsigned char status; 111 unsigned char tls_connect; 112 unsigned char tls_accept; 113 #if defined(HAVE_POLARSSL) || defined(HAVE_GNUTLS) || defined(HAVE_OPENSSL) 114 char tls_issuer[HOST_TLS_ISSUER_LEN_MAX]; 115 char tls_subject[HOST_TLS_SUBJECT_LEN_MAX]; 116 char tls_psk_identity[HOST_TLS_PSK_IDENTITY_LEN_MAX]; 117 char tls_psk[HOST_TLS_PSK_LEN_MAX]; 118 #endif 119 char error[HOST_ERROR_LEN_MAX]; 120 char snmp_error[HOST_ERROR_LEN_MAX]; 121 char ipmi_error[HOST_ERROR_LEN_MAX]; 122 char jmx_error[HOST_ERROR_LEN_MAX]; 123 } 124 DC_HOST; 125 126 typedef struct 127 { 128 DC_HOST host; 129 DC_INTERFACE interface; 130 zbx_uint64_t itemid; 131 zbx_uint64_t lastlogsize; 132 zbx_uint64_t valuemapid; 133 unsigned char type; 134 unsigned char data_type; 135 unsigned char value_type; 136 unsigned char delta; 137 unsigned char multiplier; 138 unsigned char state; 139 unsigned char db_state; 140 unsigned char snmpv3_securitylevel; 141 unsigned char authtype; 142 unsigned char flags; 143 unsigned char snmpv3_authprotocol; 144 unsigned char snmpv3_privprotocol; 145 unsigned char inventory_link; 146 unsigned char status; 147 unsigned char unreachable; 148 char key_orig[ITEM_KEY_LEN * ZBX_MAX_BYTES_IN_UTF8_CHAR + 1], *key; 149 char *formula; 150 char *units; 151 int delay; 152 int nextcheck; 153 int lastclock; 154 int mtime; 155 int history; 156 int trends; 157 char trapper_hosts[ITEM_TRAPPER_HOSTS_LEN_MAX]; 158 char logtimefmt[ITEM_LOGTIMEFMT_LEN_MAX]; 159 char snmp_community_orig[ITEM_SNMP_COMMUNITY_LEN_MAX], *snmp_community; 160 char snmp_oid_orig[ITEM_SNMP_OID_LEN_MAX], *snmp_oid; 161 char snmpv3_securityname_orig[ITEM_SNMPV3_SECURITYNAME_LEN_MAX], *snmpv3_securityname; 162 char snmpv3_authpassphrase_orig[ITEM_SNMPV3_AUTHPASSPHRASE_LEN_MAX], *snmpv3_authpassphrase; 163 char snmpv3_privpassphrase_orig[ITEM_SNMPV3_PRIVPASSPHRASE_LEN_MAX], *snmpv3_privpassphrase; 164 char ipmi_sensor[ITEM_IPMI_SENSOR_LEN_MAX]; 165 char *params; 166 char delay_flex[ITEM_DELAY_FLEX_LEN_MAX]; 167 char username_orig[ITEM_USERNAME_LEN_MAX], *username; 168 char publickey_orig[ITEM_PUBLICKEY_LEN_MAX], *publickey; 169 char privatekey_orig[ITEM_PRIVATEKEY_LEN_MAX], *privatekey; 170 char password_orig[ITEM_PASSWORD_LEN_MAX], *password; 171 char snmpv3_contextname_orig[ITEM_SNMPV3_CONTEXTNAME_LEN_MAX], *snmpv3_contextname; 172 char *db_error; 173 } 174 DC_ITEM; 175 176 typedef struct 177 { 178 zbx_uint64_t functionid; 179 zbx_uint64_t triggerid; 180 zbx_uint64_t itemid; 181 char *function; 182 char *parameter; 183 } 184 DC_FUNCTION; 185 186 typedef struct _DC_TRIGGER 187 { 188 zbx_uint64_t triggerid; 189 char *description; 190 char *expression_orig; 191 /* temporary value, allocated during processing and freed right after */ 192 char *expression; 193 194 char *error; 195 char *new_error; 196 zbx_timespec_t timespec; 197 int lastchange; 198 unsigned char topoindex; 199 unsigned char priority; 200 unsigned char type; 201 unsigned char value; 202 unsigned char state; 203 unsigned char new_value; 204 unsigned char status; 205 } 206 DC_TRIGGER; 207 208 typedef struct 209 { 210 zbx_uint64_t hostid; 211 char host[HOST_HOST_LEN_MAX]; 212 int proxy_config_nextcheck; 213 int proxy_data_nextcheck; 214 char addr_orig[INTERFACE_ADDR_LEN_MAX]; 215 char port_orig[INTERFACE_PORT_LEN_MAX]; 216 char *addr; 217 unsigned short port; 218 unsigned char tls_connect; /* how to connect: ZBX_TCP_SEC_UNENCRYPTED, */ 219 /* ZBX_TCP_SEC_TLS_PSK or ZBX_TCP_SEC_TLS_CERT */ 220 #if HOST_TLS_ISSUER_LEN_MAX > HOST_TLS_PSK_IDENTITY_LEN_MAX 221 char tls_arg1[HOST_TLS_ISSUER_LEN_MAX]; /* for passing 'tls_issuer' or 'tls_psk_identity' */ 222 /* depending on value of 'tls_connect' */ 223 #else 224 char tls_arg1[HOST_TLS_PSK_IDENTITY_LEN_MAX]; 225 #endif 226 #if HOST_TLS_SUBJECT_LEN_MAX > HOST_TLS_PSK_LEN_MAX 227 char tls_arg2[HOST_TLS_SUBJECT_LEN_MAX]; /* for passing 'tls_subject' or 'tls_psk' */ 228 /* depending on value of 'tls_connect' */ 229 #else 230 char tls_arg2[HOST_TLS_PSK_LEN_MAX]; 231 #endif 232 } 233 DC_PROXY; 234 235 typedef struct 236 { 237 zbx_uint64_t actionid; 238 char *formula; 239 unsigned char eventsource; 240 unsigned char evaltype; 241 zbx_vector_ptr_t conditions; 242 } 243 zbx_action_eval_t; 244 245 typedef struct 246 { 247 const char *host; 248 const char *key; 249 } 250 zbx_host_key_t; 251 252 /* housekeeping related configuration data */ 253 typedef struct 254 { 255 int events_trigger; 256 int events_internal; 257 int events_discovery; 258 int events_autoreg; 259 int services; 260 int audit; 261 int sessions; 262 int trends; 263 int history; 264 265 unsigned char services_mode; 266 unsigned char audit_mode; 267 unsigned char sessions_mode; 268 unsigned char events_mode; 269 unsigned char trends_mode; 270 unsigned char trends_global; 271 unsigned char history_mode; 272 unsigned char history_global; 273 } 274 zbx_config_hk_t; 275 276 /* global configuration data (loaded from config table) */ 277 typedef struct 278 { 279 /* the fields set by zbx_config_get() function, see ZBX_CONFIG_FLAGS_ defines */ 280 zbx_uint64_t flags; 281 282 char **severity_name; 283 zbx_uint64_t discovery_groupid; 284 int default_inventory_mode; 285 int refresh_unsupported; 286 unsigned char snmptrap_logging; 287 288 /* housekeeping related configuration data */ 289 zbx_config_hk_t hk; 290 } 291 zbx_config_t; 292 293 #define ZBX_CONFIG_FLAGS_SEVERITY_NAME 0x00000001 294 #define ZBX_CONFIG_FLAGS_DISCOVERY_GROUPID 0x00000002 295 #define ZBX_CONFIG_FLAGS_DEFAULT_INVENTORY_MODE 0x00000004 296 #define ZBX_CONFIG_FLAGS_REFRESH_UNSUPPORTED 0x00000008 297 #define ZBX_CONFIG_FLAGS_SNMPTRAP_LOGGING 0x00000010 298 #define ZBX_CONFIG_FLAGS_HOUSEKEEPER 0x00000020 299 300 typedef struct 301 { 302 zbx_uint64_t itemid; 303 zbx_timespec_t timestamp; 304 history_value_t value; 305 } 306 zbx_item_history_value_t; 307 308 /* item queue data */ 309 typedef struct 310 { 311 zbx_uint64_t itemid; 312 zbx_uint64_t proxy_hostid; 313 int type; 314 int nextcheck; 315 } 316 zbx_queue_item_t; 317 318 int is_item_processed_by_server(unsigned char type, const char *key); 319 int in_maintenance_without_data_collection(unsigned char maintenance_status, unsigned char maintenance_type, 320 unsigned char type); 321 void dc_add_history(zbx_uint64_t itemid, unsigned char value_type, unsigned char item_flags, AGENT_RESULT *result, 322 const zbx_timespec_t *ts, unsigned char state, const char *error); 323 void dc_flush_history(); 324 int DCsync_history(int sync_type, int *sync_num); 325 void init_database_cache(); 326 void free_database_cache(); 327 328 void DCadd_nextcheck(zbx_uint64_t itemid, const zbx_timespec_t *ts, const char *error_msg); 329 void DCflush_nextchecks(); 330 331 #define ZBX_STATS_HISTORY_COUNTER 0 332 #define ZBX_STATS_HISTORY_FLOAT_COUNTER 1 333 #define ZBX_STATS_HISTORY_UINT_COUNTER 2 334 #define ZBX_STATS_HISTORY_STR_COUNTER 3 335 #define ZBX_STATS_HISTORY_LOG_COUNTER 4 336 #define ZBX_STATS_HISTORY_TEXT_COUNTER 5 337 #define ZBX_STATS_NOTSUPPORTED_COUNTER 6 338 #define ZBX_STATS_HISTORY_TOTAL 7 339 #define ZBX_STATS_HISTORY_USED 8 340 #define ZBX_STATS_HISTORY_FREE 9 341 #define ZBX_STATS_HISTORY_PFREE 10 342 #define ZBX_STATS_TREND_TOTAL 11 343 #define ZBX_STATS_TREND_USED 12 344 #define ZBX_STATS_TREND_FREE 13 345 #define ZBX_STATS_TREND_PFREE 14 346 #define ZBX_STATS_HISTORY_INDEX_TOTAL 15 347 #define ZBX_STATS_HISTORY_INDEX_USED 16 348 #define ZBX_STATS_HISTORY_INDEX_FREE 17 349 #define ZBX_STATS_HISTORY_INDEX_PFREE 18 350 void *DCget_stats(int request); 351 352 zbx_uint64_t DCget_nextid(const char *table_name, int num); 353 354 /* initial sync, get all data */ 355 #define ZBX_DBSYNC_INIT 0 356 /* update sync, get changed data */ 357 #define ZBX_DBSYNC_UPDATE 1 358 359 void DCsync_configuration(unsigned char mode); 360 void init_configuration_cache(void); 361 void free_configuration_cache(void); 362 363 void DCconfig_get_triggers_by_triggerids(DC_TRIGGER *triggers, const zbx_uint64_t *triggerids, int *errcode, 364 size_t num); 365 void DCconfig_clean_items(DC_ITEM *items, int *errcodes, size_t num); 366 int DCget_host_by_hostid(DC_HOST *host, zbx_uint64_t hostid); 367 void DCconfig_get_items_by_keys(DC_ITEM *items, zbx_host_key_t *keys, int *errcodes, size_t num); 368 void DCconfig_get_items_by_itemids(DC_ITEM *items, const zbx_uint64_t *itemids, int *errcodes, size_t num); 369 void DCconfig_set_item_db_state(zbx_uint64_t itemid, unsigned char state, const char *error); 370 void DCconfig_get_functions_by_functionids(DC_FUNCTION *functions, 371 zbx_uint64_t *functionids, int *errcodes, size_t num); 372 void DCconfig_clean_functions(DC_FUNCTION *functions, int *errcodes, size_t num); 373 void DCconfig_clean_triggers(DC_TRIGGER *triggers, int *errcodes, size_t num); 374 int DCconfig_lock_triggers_by_history_items(zbx_vector_ptr_t *history_items, zbx_vector_uint64_t *triggerids); 375 void DCconfig_unlock_triggers(const zbx_vector_uint64_t *triggerids); 376 void DCconfig_unlock_all_triggers(); 377 int DCconfig_lock_lld_rule(zbx_uint64_t lld_ruleid); 378 void DCconfig_unlock_lld_rule(zbx_uint64_t lld_ruleid); 379 void DCconfig_get_triggers_by_itemids(zbx_hashset_t *trigger_info, zbx_vector_ptr_t *trigger_order, 380 const zbx_uint64_t *itemids, const zbx_timespec_t *timespecs, char **errors, int itemids_num, 381 unsigned char expand); 382 void DCconfig_get_time_based_triggers(DC_TRIGGER **trigger_info, zbx_vector_ptr_t *trigger_order, int max_triggers, 383 int process_num); 384 void DCfree_triggers(zbx_vector_ptr_t *triggers); 385 void DCconfig_update_interface_snmp_stats(zbx_uint64_t interfaceid, int max_snmp_succeed, int min_snmp_fail); 386 int DCconfig_get_suggested_snmp_vars(zbx_uint64_t interfaceid, int *bulk); 387 int DCconfig_get_interface_by_type(DC_INTERFACE *interface, zbx_uint64_t hostid, unsigned char type); 388 int DCconfig_get_interface(DC_INTERFACE *interface, zbx_uint64_t hostid, zbx_uint64_t itemid); 389 int DCconfig_get_poller_nextcheck(unsigned char poller_type); 390 int DCconfig_get_poller_items(unsigned char poller_type, DC_ITEM *items); 391 int DCconfig_get_snmp_interfaceids_by_addr(const char *addr, zbx_uint64_t **interfaceids); 392 size_t DCconfig_get_snmp_items_by_interfaceid(zbx_uint64_t interfaceid, DC_ITEM **items); 393 394 #define ZBX_HK_OPTION_DISABLED 0 395 #define ZBX_HK_OPTION_ENABLED 1 396 397 void DCrequeue_items(zbx_uint64_t *itemids, unsigned char *states, int *lastclocks, zbx_uint64_t *lastlogsizes, 398 int *mtimes, int *errcodes, size_t num); 399 void DCpoller_requeue_items(zbx_uint64_t *itemids, unsigned char *states, int *lastclocks, zbx_uint64_t *lastlogsizes, 400 int *mtimes, int *errcodes, size_t num, unsigned char poller_type, int *nextcheck); 401 int DCconfig_activate_host(DC_ITEM *item); 402 int DCconfig_deactivate_host(DC_ITEM *item, int now); 403 404 int DCconfig_check_trigger_dependencies(zbx_uint64_t triggerid); 405 406 void DCconfig_set_trigger_value(zbx_uint64_t triggerid, unsigned char value, 407 unsigned char state, const char *error, int *lastchange); 408 void DCconfig_set_maintenance(const zbx_uint64_t *hostids, int hostids_num, int maintenance_status, 409 int maintenance_type, int maintenance_from); 410 411 #define ZBX_CONFSTATS_BUFFER_TOTAL 1 412 #define ZBX_CONFSTATS_BUFFER_USED 2 413 #define ZBX_CONFSTATS_BUFFER_FREE 3 414 #define ZBX_CONFSTATS_BUFFER_PFREE 4 415 void *DCconfig_get_stats(int request); 416 417 int DCconfig_get_proxypoller_hosts(DC_PROXY *proxies, int max_hosts); 418 int DCconfig_get_proxypoller_nextcheck(void); 419 void DCrequeue_proxy(zbx_uint64_t hostid, unsigned char update_nextcheck); 420 void DCconfig_set_proxy_timediff(zbx_uint64_t hostid, const zbx_timespec_t *timediff); 421 int DCcheck_proxy_permissions(const char *host, const zbx_socket_t *sock, zbx_uint64_t *hostid, char **error); 422 423 #if defined(HAVE_POLARSSL) || defined(HAVE_GNUTLS) || defined(HAVE_OPENSSL) 424 size_t DCget_psk_by_identity(const unsigned char *psk_identity, unsigned char *psk_buf, size_t psk_buf_len); 425 #endif 426 427 void DCget_user_macro(zbx_uint64_t *hostids, int host_num, const char *macro, char **replace_to); 428 char *DCexpression_expand_user_macros(const char *expression, char **error); 429 430 int DChost_activate(zbx_uint64_t hostid, unsigned char agent_type, const zbx_timespec_t *ts, 431 zbx_agent_availability_t *in, zbx_agent_availability_t *out); 432 433 int DChost_deactivate(zbx_uint64_t hostid, unsigned char agent, const zbx_timespec_t *ts, 434 zbx_agent_availability_t *in, zbx_agent_availability_t *out, const char *error); 435 436 void DCget_delta_items(zbx_hashset_t *items, const zbx_vector_uint64_t *ids); 437 void DCset_delta_items(zbx_hashset_t *items); 438 439 #define ZBX_QUEUE_FROM_DEFAULT 6 /* default lower limit for delay (in seconds) */ 440 #define ZBX_QUEUE_TO_INFINITY -1 /* no upper limit for delay */ 441 void DCfree_item_queue(zbx_vector_ptr_t *queue); 442 int DCget_item_queue(zbx_vector_ptr_t *queue, int from, int to); 443 444 int DCget_item_count(zbx_uint64_t hostid); 445 int DCget_item_unsupported_count(zbx_uint64_t hostid); 446 int DCget_trigger_count(void); 447 double DCget_required_performance(void); 448 int DCget_host_count(void); 449 450 void DCget_expressions_by_names(zbx_vector_ptr_t *expressions, const char * const *names, int names_num); 451 void DCget_expressions_by_name(zbx_vector_ptr_t *expressions, const char *name); 452 453 int DCget_data_expected_from(zbx_uint64_t itemid, int *seconds); 454 455 void DCget_hostids_by_functionids(zbx_vector_uint64_t *functionids, zbx_vector_uint64_t *hostids); 456 457 /* global configuration support */ 458 void zbx_config_get(zbx_config_t *cfg, zbx_uint64_t flags); 459 void zbx_config_clean(zbx_config_t *cfg); 460 461 int DCset_hosts_availability(zbx_vector_ptr_t *availabilities); 462 463 int DCreset_hosts_availability(zbx_vector_ptr_t *hosts); 464 void DCupdate_hosts_availability(); 465 466 void zbx_dc_get_actions_eval(zbx_vector_ptr_t *actions); 467 void zbx_action_eval_free(zbx_action_eval_t *action); 468 469 int DCget_hosts_availability(zbx_vector_ptr_t *hosts, int *ts); 470 471 void zbx_host_availability_init(zbx_host_availability_t *availability, zbx_uint64_t hostid); 472 void zbx_host_availability_clean(zbx_host_availability_t *availability); 473 void zbx_host_availability_free(zbx_host_availability_t *availability); 474 int zbx_host_availability_is_set(const zbx_host_availability_t *ha); 475 476 void zbx_set_availability_diff_ts(int ts); 477 478 #define ZBX_HC_ITEM_STATUS_NORMAL 0 479 #define ZBX_HC_ITEM_STATUS_BUSY 1 480 481 typedef struct zbx_hc_data zbx_hc_data_t; 482 483 typedef struct 484 { 485 zbx_uint64_t itemid; 486 unsigned char status; 487 488 zbx_hc_data_t *tail; 489 zbx_hc_data_t *head; 490 } 491 zbx_hc_item_t; 492 493 void zbx_dc_update_proxy_lastaccess(zbx_uint64_t hostid, int lastaccess); 494 495 #endif 496