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