/****************************************************************************** * * * Function: ja_get_zbxsnd_on * * * * Purpose: gets the zabbix notification existence * * * * Parameters: * * * * Return value: value of Zabbix notification existence that get * * * * Comments: * * * ******************************************************************************/ int ja_get_zbxsnd_on() { DB_RESULT result; DB_ROW row; int vl; const char *__function_name = "ja_get_zbxsnd_on"; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); result = DBselect("select value from ja_parameter_table where parameter_name = '%s'", ZBXSND_ON); if (NULL == (row = DBfetch(result))) { DBfree_result(result); return ZBXSND_NOTICE_ON; } if (strlen(row[0]) != 1) { DBfree_result(result); return ZBXSND_NOTICE_ON; } vl = atoi(row[0]); DBfree_result(result); if (vl == ZBXSND_NOTICE_OFF || vl == ZBXSND_NOTICE_ON) { return vl; } return ZBXSND_NOTICE_ON; }
/****************************************************************************** * * * Function: delete_history * * * * Purpose: remove outdated information from historical table * * * * Parameters: now - current timestamp * * * * Return value: number of rows deleted * * * * Author: Alexei Vladishev * * * * Comments: * * * ******************************************************************************/ static int delete_history(const char *table, zbx_uint64_t itemid, int keep_history, int now) { const char *__function_name = "delete_history"; DB_RESULT result; DB_ROW row; int min_clock, deleted; zabbix_log(LOG_LEVEL_DEBUG, "In %s() table:'%s' itemid:" ZBX_FS_UI64 " keep_history:%d now:%d", __function_name, table, itemid, keep_history, now); result = DBselect("select min(clock) from %s where itemid=" ZBX_FS_UI64, table, itemid); if (NULL == (row = DBfetch(result)) || SUCCEED == DBis_null(row[0])) { DBfree_result(result); return 0; } min_clock = atoi(row[0]); min_clock = MIN(now - keep_history * SEC_PER_DAY, min_clock + 4 * CONFIG_HOUSEKEEPING_FREQUENCY * SEC_PER_HOUR); DBfree_result(result); deleted = DBexecute("delete from %s where itemid=" ZBX_FS_UI64 " and clock<%d", table, itemid, min_clock); zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%d", __function_name, deleted); return deleted; }
int DBpatch_3020001(void) { DB_RESULT result; zbx_vector_uint64_t eventids; DB_ROW row; zbx_uint64_t eventid; int sources[] = {EVENT_SOURCE_TRIGGERS, EVENT_SOURCE_INTERNAL}; int objects[] = {EVENT_OBJECT_ITEM, EVENT_OBJECT_LLDRULE}, i; zbx_vector_uint64_create(&eventids); for (i = 0; i < (int)ARRSIZE(sources); i++) { result = DBselect( "select p.eventid" " from problem p" " where p.source=%d and p.object=%d and not exists (" "select null" " from triggers t" " where t.triggerid=p.objectid" ")", sources[i], EVENT_OBJECT_TRIGGER); while (NULL != (row = DBfetch(result))) { ZBX_STR2UINT64(eventid, row[0]); zbx_vector_uint64_append(&eventids, eventid); } DBfree_result(result); } for (i = 0; i < (int)ARRSIZE(objects); i++) { result = DBselect( "select p.eventid" " from problem p" " where p.source=%d and p.object=%d and not exists (" "select null" " from items i" " where i.itemid=p.objectid" ")", EVENT_SOURCE_INTERNAL, objects[i]); while (NULL != (row = DBfetch(result))) { ZBX_STR2UINT64(eventid, row[0]); zbx_vector_uint64_append(&eventids, eventid); } DBfree_result(result); } zbx_vector_uint64_sort(&eventids, ZBX_DEFAULT_UINT64_COMPARE_FUNC); if (0 != eventids.values_num) DBexecute_multiple_query("delete from problem where", "eventid", &eventids); zbx_vector_uint64_destroy(&eventids); return SUCCEED; }
/****************************************************************************** * * * Function: * * * * Purpose: * * * * Parameters: * * * * Return value: * * * * Comments: * * * ******************************************************************************/ int jarun_icon_info_get_status(const zbx_uint64_t inner_jobnet_id, char *get_job_id, const zbx_uint64_t inner_job_id) { char *tp; DB_RESULT result; DB_ROW row; int status; zbx_uint64_t sub_inner_job_id, sub_inner_jobnet_id; const char *__function_name = "jarun_icon_info_get_status"; zabbix_log(LOG_LEVEL_DEBUG, "In %s() inner_jobnet_id: " ZBX_FS_UI64 " get_job_id: %s", __function_name, inner_jobnet_id, get_job_id); status = -1; sub_inner_job_id = 0; sub_inner_jobnet_id = inner_jobnet_id; tp = strtok(get_job_id, "/"); while (tp != NULL) { if (sub_inner_job_id > 0) { result = DBselect ("select link_inner_jobnet_id from ja_run_icon_jobnet_table" " where inner_job_id = " ZBX_FS_UI64, sub_inner_job_id); if (NULL != (row = DBfetch(result))) { ZBX_STR2UINT64(sub_inner_jobnet_id, row[0]); } else { ja_log("JARUNICONINFO200001", 0, NULL, inner_job_id, __function_name, sub_inner_job_id); status = -1; DBfree_result(result); break; } DBfree_result(result); } result = DBselect ("select inner_job_id, status from ja_run_job_table" " where inner_jobnet_id = " ZBX_FS_UI64 " and job_id = '%s'", sub_inner_jobnet_id, tp); if (NULL != (row = DBfetch(result))) { ZBX_STR2UINT64(sub_inner_job_id, row[0]); status = atoi(row[1]); } else { ja_log("JARUNICONINFO200002", 0, NULL, inner_job_id, __function_name, tp, inner_job_id); status = -1; DBfree_result(result); break; } DBfree_result(result); tp = strtok(NULL, "/"); } return status; }
/* * Get value of autoincrement field for last insert or update statement */ zbx_uint64_t zbx_db_insert_id(int exec_result, const char *table, const char *field) { #ifdef HAVE_MYSQL zabbix_log(LOG_LEVEL_DEBUG, "In DBinsert_id()" ); if(exec_result == FAIL) return 0; return mysql_insert_id(conn); #endif #ifdef HAVE_POSTGRESQL DB_RESULT tmp_res; zbx_uint64_t id_res = FAIL; zabbix_log(LOG_LEVEL_DEBUG, "In DBinsert_id()" ); if(exec_result < 0) return 0; if(exec_result == FAIL) return 0; if((Oid)exec_result == InvalidOid) return 0; tmp_res = zbx_db_select("select %s from %s where oid=%i", field, table, exec_result); ZBX_STR2UINT64(id_res, PQgetvalue(tmp_res->pg_result, 0, 0)); /* id_res = atoi(PQgetvalue(tmp_res->pg_result, 0, 0));*/ DBfree_result(tmp_res); return id_res; #endif #ifdef HAVE_ORACLE DB_ROW row; char sql[MAX_STRING_LEN]; DB_RESULT result; zbx_uint64_t id; zabbix_log(LOG_LEVEL_DEBUG, "In DBinsert_id()" ); if(exec_result == FAIL) return 0; zbx_snprintf(sql, sizeof(sql), "select %s_%s.currval from dual", table, field); result=DBselect("%s", sql); row = DBfetch(result); ZBX_STR2UINT64(id, row[0]); /* id = atoi(row[0]);*/ DBfree_result(result); return id; #endif #ifdef HAVE_SQLITE3 return (zbx_uint64_t)sqlite3_last_insert_rowid(conn); #endif }
/****************************************************************************** * * * Function: * * * * Purpose: * * * * Parameters: * * * * Return value: * * * * Comments: * * * ******************************************************************************/ int jarun_icon_info(const zbx_uint64_t inner_job_id) { DB_RESULT result; DB_ROW row; int status; char str_status[4]; zbx_uint64_t inner_jobnet_id; int info_flag; char *get_job_id, *get_calendar_id; const char *__function_name = "jarun_icon_info"; zabbix_log(LOG_LEVEL_DEBUG, "In %s() inner_job_id: " ZBX_FS_UI64, __function_name, inner_job_id); result = DBselect ("select inner_jobnet_id, info_flag, get_job_id, get_calendar_id" " from ja_run_icon_info_table" " where inner_job_id = " ZBX_FS_UI64, inner_job_id); status = -1; if (NULL != (row = DBfetch(result))) { ZBX_STR2UINT64(inner_jobnet_id, row[0]); info_flag = atoi(row[1]); get_job_id = row[2]; get_calendar_id = row[3]; switch (info_flag) { case 0: status = jarun_icon_info_get_status(inner_jobnet_id, get_job_id, inner_job_id); break; case 3: status = jarun_icon_info_get_calendar(get_calendar_id, inner_job_id); break; default: break; } } else { ja_log("JARUNICONINFO200003", inner_jobnet_id, NULL, inner_job_id, __function_name, inner_job_id); DBfree_result(result); return FAIL; } DBfree_result(result); if (status < 0) return ja_set_runerr(inner_job_id, 2); zbx_snprintf(str_status, sizeof(str_status), "%d", status); ja_set_value_after(inner_job_id, inner_jobnet_id, "LAST_STATUS", str_status); return ja_flow(inner_job_id, JA_FLOW_TYPE_NORMAL, 1); }
static int check_script_permissions(zbx_uint64_t groupid, zbx_uint64_t hostid, char *error, size_t max_error_len) { const char *__function_name = "check_script_permissions"; DB_RESULT result; int ret = SUCCEED; zabbix_log(LOG_LEVEL_DEBUG, "In %s() groupid:" ZBX_FS_UI64 " hostid:" ZBX_FS_UI64, __function_name, groupid, hostid); if (0 == groupid) goto exit; result = DBselect( "select hostid" " from hosts_groups" " where hostid=" ZBX_FS_UI64 " and groupid=" ZBX_FS_UI64, hostid, groupid); if (NULL == DBfetch(result)) { zbx_strlcpy(error, "Insufficient permissions. Host is not in an allowed host group.", max_error_len); ret = FAIL; } DBfree_result(result); exit: zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; }
static int DBget_script_by_scriptid(zbx_uint64_t scriptid, zbx_script_t *script, zbx_uint64_t *groupid) { const char *__function_name = "DBget_script_by_scriptid"; DB_RESULT result; DB_ROW row; int ret = FAIL; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); result = DBselect( "select type,execute_on,command,groupid" " from scripts" " where scriptid=" ZBX_FS_UI64, scriptid); if (NULL != (row = DBfetch(result))) { script->type = (unsigned char)atoi(row[0]); script->execute_on = (unsigned char)atoi(row[1]); script->command = zbx_strdup(script->command, row[2]); ZBX_DBROW2UINT64(*groupid, row[3]); ret = SUCCEED; } DBfree_result(result); zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; }
/****************************************************************************** * * * Function: get_proxy_id * * * * Purpose: * * * * Parameters: * * * * Return value: SUCCEED - processed successfully * * FAIL - an error occurred * * * * Author: Aleksander Vladishev * * * * Comments: * * * ******************************************************************************/ int get_proxy_id(struct zbx_json_parse *jp, zbx_uint64_t *hostid) { DB_RESULT result; DB_ROW row; char host[HOST_HOST_LEN_MAX], host_esc[MAX_STRING_LEN]; int res = FAIL; if (SUCCEED == zbx_json_value_by_name(jp, ZBX_PROTO_TAG_HOST, host, sizeof(host))) { DBescape_string(host, host_esc, sizeof(host_esc)); result = DBselect("select hostid from hosts where host='%s'" " and status in (%d)" DB_NODE, host_esc, HOST_STATUS_PROXY, DBnode_local("hostid")); if (NULL != (row = DBfetch(result)) && FAIL == DBis_null(row[0])) { *hostid = zbx_atoui64(row[0]); res = SUCCEED; } else zabbix_log(LOG_LEVEL_WARNING, "Unknown proxy \"%s\"", host); DBfree_result(result); } else { zabbix_log(LOG_LEVEL_WARNING, "Incorrect data. %s", zbx_json_strerror()); zabbix_syslog("Incorrect data. %s", zbx_json_strerror()); } return res; }
static int housekeeping_events(int now) { const char *__function_name = "housekeeping_events"; int event_history, deleted = 0; DB_RESULT result; DB_ROW row; zbx_uint64_t eventid; zabbix_log(LOG_LEVEL_DEBUG, "In %s() now:%d", __function_name, now); result = DBselect("select eventid from events where clock<%d", now - *(int *)DCconfig_get_config_data(&event_history, CONFIG_EVENT_HISTORY) * SEC_PER_DAY); while (NULL != (row = DBfetch(result))) { ZBX_STR2UINT64(eventid, row[0]); DBexecute("delete from acknowledges where eventid=" ZBX_FS_UI64, eventid); deleted += DBexecute("delete from events where eventid=" ZBX_FS_UI64, eventid); } DBfree_result(result); zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%d", __function_name, deleted); return deleted; }
int connect_to_node(int nodeid, zbx_sock_t *sock) { DB_RESULT result; DB_ROW row; unsigned short port; int res = FAIL; zabbix_log(LOG_LEVEL_DEBUG, "In connect_to_node(nodeid:%d)", nodeid); result = DBselect("select ip,port from nodes where nodeid=%d", nodeid); if (NULL != (row = DBfetch(result))) { port = (unsigned short)atoi(row[1]); if (SUCCEED == zbx_tcp_connect(sock, CONFIG_SOURCE_IP, row[0], port, 0)) res = SUCCEED; else zabbix_log(LOG_LEVEL_ERR, "NODE %d: Unable to connect to Node [%d] error: %s", CONFIG_NODEID, nodeid, zbx_tcp_strerror()); } else zabbix_log(LOG_LEVEL_ERR, "NODE %d: Node [%d] is unknown", CONFIG_NODEID, nodeid); DBfree_result(result); return res; }
static void process_recovery_msg(DB_ESCALATION *escalation, DB_EVENT *r_event, DB_ACTION *action) { const char *__function_name = "process_recovery_msg"; DB_RESULT result; DB_ROW row; zbx_uint64_t userid, mediatypeid; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); if (1 == action->recovery_msg) { result = DBselect("select distinct userid,mediatypeid from alerts where actionid=" ZBX_FS_UI64 " and eventid=" ZBX_FS_UI64 " and mediatypeid is not null and alerttype=%d", action->actionid, escalation->eventid, ALERT_TYPE_MESSAGE); while (NULL != (row = DBfetch(result))) { ZBX_DBROW2UINT64(userid, row[0]); ZBX_STR2UINT64(mediatypeid, row[1]); escalation->esc_step = 0; add_message_alert(escalation, r_event, action, userid, mediatypeid, action->shortdata, action->longdata); } DBfree_result(result); } else zabbix_log(LOG_LEVEL_DEBUG, "escalation stopped: recovery message not defined", escalation->actionid); escalation->status = ESCALATION_STATUS_COMPLETED; zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
/****************************************************************************** * * * Function: get_trigger_permission * * * * Purpose: Return user permissions for access to trigger * * * * Parameters: * * * * Return value: PERM_DENY - if host or user not found, * * or permission otherwise * * * * Author: * * * * Comments: * * * ******************************************************************************/ static int get_trigger_permission(zbx_uint64_t userid, zbx_uint64_t triggerid) { const char *__function_name = "get_trigger_permission"; DB_RESULT result; DB_ROW row; int perm = PERM_DENY, host_perm; zbx_uint64_t hostid; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); result = DBselect( "select distinct i.hostid" " from items i,functions f" " where i.itemid=f.itemid" " and f.triggerid=" ZBX_FS_UI64, triggerid); while (NULL != (row = DBfetch(result))) { ZBX_STR2UINT64(hostid, row[0]); host_perm = get_host_permission(userid, hostid); if (perm < host_perm) perm = host_perm; } DBfree_result(result); zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_permission_string(perm)); return perm; }
/****************************************************************************** * * * Function: * * * * Purpose: * * * * Parameters: * * * * Return value: * * * * Comments: * * * ******************************************************************************/ int ja_host_lock(const char *host, const zbx_uint64_t inner_job_id) { int db_ret; char *host_esc; const char *__function_name = "ja_host_lock"; zabbix_log(LOG_LEVEL_DEBUG, "In %s() host: %s", __function_name, host); if (ja_host_getip(host, NULL, inner_job_id, NULL, JA_TXN_ON) == 0) return FAIL; DBfree_result(DBselect ("select lock_host_name from ja_host_lock_table where lock_host_name = 'HOST_LOCK_RECORD' for update")); host_esc = DBdyn_escape_string(host); db_ret = DBexecute ("insert into ja_host_lock_table (lock_host_name) values ('%s')", host_esc); zbx_free(host_esc); if (db_ret < ZBX_DB_OK) { ja_log("JAHOST200005", 0, NULL, inner_job_id, __function_name, host, inner_job_id); return FAIL; } else { return SUCCEED; } }
/****************************************************************************** * * * Function: ja_host_getport * * * * Purpose: get the port that is specified in the host macro * * * * Parameters: hostid (in) - host id * * macro_flag (in) - macro flag * * 0: job agent listen port * * 1: ssh connect port * * * * * * Return value: return the port number * * * * Comments: * * * ******************************************************************************/ int ja_host_getport(zbx_uint64_t hostid, int macro_flag) { DB_RESULT result; DB_ROW row; int port; char *macro_name; const char *__function_name = "ja_host_getport"; zabbix_log(LOG_LEVEL_DEBUG, "In %s() hostid: " ZBX_FS_UI64, __function_name, hostid); if (macro_flag == 0) { port = CONFIG_AGENT_LISTEN_PORT; macro_name = JA_AGENT_PORT; } else { port = JA_SSH_CONNECT_PORT; macro_name = JA_SSH_PORT; } result = DBselect("select value from hostmacro where hostid = " ZBX_FS_UI64 " and macro = '%s'", hostid, macro_name); row = DBfetch(result); if (row != NULL) { port = atoi(row[0]); } DBfree_result(result); return port; }
static int get_minnextcheck(int now) { DB_RESULT result; DB_ROW row; int res = FAIL; result = DBselect( "select count(*),min(nextcheck)" " from drules" " where proxy_hostid is null" " and status=%d" " and " ZBX_SQL_MOD(druleid,%d) "=%d" DB_NODE, DRULE_STATUS_MONITORED, CONFIG_DISCOVERER_FORKS, process_num - 1, DBnode_local("druleid")); row = DBfetch(result); if (NULL == row || DBis_null(row[0]) == SUCCEED || DBis_null(row[1]) == SUCCEED) zabbix_log(LOG_LEVEL_DEBUG, "get_minnextcheck(): no items to update"); else if (0 != atoi(row[0])) res = atoi(row[1]); DBfree_result(result); return res; }
/****************************************************************************** * * * Function: zbx_session_validate * * * * Purpose: validates active session by access level * * * * Parameters: sessionid - [IN] the session id to validate * * access_level - [IN] the required access rights * * * * Return value: SUCCEED - the session is active and user has the required * * access rights. * * FAIL - the session is not active or usr has not enough * * access rights. * * * ******************************************************************************/ static int zbx_session_validate(const char *sessionid, int access_level) { char *sessionid_esc; int ret = FAIL; DB_RESULT result; DB_ROW row; sessionid_esc = DBdyn_escape_string(sessionid); result = DBselect( "select null" " from users u,sessions s" " where u.userid=s.userid" " and s.status=%d" " and s.sessionid='%s'" " and u.type>=%d", ZBX_SESSION_ACTIVE, sessionid_esc, access_level); if (NULL != (row = DBfetch(result))) ret = SUCCEED; DBfree_result(result); zbx_free(sessionid_esc); return ret; }
/****************************************************************************** * * * Function: run_commands * * * * Purpose: run remote commandlist for specific action * * * * Parameters: trigger - trigger data * * action - action data * * * * Author: Eugene Grigorjev * * * * Comments: commands separated with newline * * * ******************************************************************************/ void op_run_commands(char *cmd_list) { DB_RESULT result; DB_ROW row; char *alias, *alias_esc, *command; int is_group; assert(cmd_list); zabbix_log(LOG_LEVEL_DEBUG, "In run_commands()"); while (1 != get_next_command(&cmd_list, &alias, &is_group, &command)) { if (!alias || *alias == '\0' || !command || *command == '\0') continue; if (is_group) { alias_esc = DBdyn_escape_string(alias); result = DBselect("select distinct h.host from hosts_groups hg,hosts h,groups g" " where hg.hostid=h.hostid and hg.groupid=g.groupid and g.name='%s'" DB_NODE, alias_esc, DBnode_local("h.hostid")); zbx_free(alias_esc); while (NULL != (row = DBfetch(result))) run_remote_command(row[0], command); DBfree_result(result); } else run_remote_command(alias, command); } zabbix_log( LOG_LEVEL_DEBUG, "End run_commands()"); }
/****************************************************************************** * * * Function: get_hostid_by_host * * * * Purpose: check for host name and return hostid * * * * Parameters: host - host name * * * * Return value: SUCCEED - host is found * * FAIL - an error occured or host not found * * * * Author: Aleksander Vladishev * * * * Comments: * * * ******************************************************************************/ static int get_hostid_by_host(const char *host, zbx_uint64_t *hostid, char *error) { char *host_esc; DB_RESULT result; DB_ROW row; int res = FAIL; zabbix_log(LOG_LEVEL_DEBUG, "In get_hostid_by_host(host:'%s')", host); host_esc = DBdyn_escape_string(host); result = DBselect("select hostid from hosts where host='%s'" DB_NODE, host_esc, DBnode_local("hostid")); if (NULL != (row = DBfetch(result))) { *hostid = zbx_atoui64(row[0]); res = SUCCEED; } else zbx_snprintf(error, MAX_STRING_LEN, "host [%s] not found", host); DBfree_result(result); zbx_free(host_esc); return res; }
/****************************************************************************** * * * Function: lld_items_get * * * * Purpose: returns the list of items which are related to the trigger * * prototype * * * * Parameters: parent_triggerid - [IN] trigger prototype identificator * * items - [OUT] sorted list of items * * * ******************************************************************************/ static void lld_items_get(zbx_uint64_t parent_triggerid, zbx_vector_ptr_t *items) { const char *__function_name = "lld_items_get"; DB_RESULT result; DB_ROW row; zbx_lld_item_t *item; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); result = DBselect( "select distinct i.itemid,i.flags" " from items i,functions f" " where i.itemid=f.itemid" " and f.triggerid=" ZBX_FS_UI64, parent_triggerid); while (NULL != (row = DBfetch(result))) { item = zbx_malloc(NULL, sizeof(zbx_lld_item_t)); ZBX_STR2UINT64(item->itemid, row[0]); ZBX_STR2UCHAR(item->flags, row[1]); zbx_vector_ptr_append(items, item); } DBfree_result(result); zbx_vector_ptr_sort(items, ZBX_DEFAULT_UINT64_PTR_COMPARE_FUNC); zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
static void add_object_msg(zbx_uint64_t operationid, zbx_uint64_t mediatypeid, ZBX_USER_MSG **user_msg, const char *subject, const char *message, unsigned char source, zbx_uint64_t triggerid) { DB_RESULT result; DB_ROW row; zbx_uint64_t userid; result = DBselect( "select userid" " from opmessage_usr" " where operationid=" ZBX_FS_UI64 " union " "select g.userid" " from opmessage_grp m,users_groups g" " where m.usrgrpid=g.usrgrpid" " and m.operationid=" ZBX_FS_UI64, operationid, operationid); while (NULL != (row = DBfetch(result))) { ZBX_STR2UINT64(userid, row[0]); add_user_msg(userid, mediatypeid, user_msg, subject, message, source, triggerid); } DBfree_result(result); }
/****************************************************************************** * * * Function: get_minnextcheck * * * * Purpose: calculate when we have to process earliest httptest * * * * Parameters: now - current timestamp (not used) * * * * Return value: timestamp of earliest check or -1 if not found * * * * Author: Alexei Vladishev * * * * Comments: * * * ******************************************************************************/ static int get_minnextcheck(int now) { DB_RESULT result; DB_ROW row; int res; result = DBselect( "select min(t.nextcheck)" " from httptest t,applications a,hosts h" " where t.applicationid=a.applicationid" " and a.hostid=h.hostid" " and " ZBX_SQL_MOD(t.httptestid,%d) "=%d" " and t.status=%d" " and h.proxy_hostid is null" " and h.status=%d" " and (h.maintenance_status=%d or h.maintenance_type=%d)" DB_NODE, CONFIG_HTTPPOLLER_FORKS, process_num - 1, HTTPTEST_STATUS_MONITORED, HOST_STATUS_MONITORED, HOST_MAINTENANCE_STATUS_OFF, MAINTENANCE_TYPE_NORMAL, DBnode_local("t.httptestid")); if (NULL == (row = DBfetch(result)) || SUCCEED == DBis_null(row[0])) { zabbix_log(LOG_LEVEL_DEBUG, "No httptests to process in get_minnextcheck."); res = FAIL; } else res = atoi(row[0]); DBfree_result(result); return res; }
/****************************************************************************** * * * Function: hk_history_prepare * * * * Purpose: prepares history housekeeping rule * * * * Parameters: rule - [IN/OUT] the history housekeeping rule * * now - [IN] the current timestmap * * * * Author: Andris Zeila * * * * Comments: This function is called to initialize history rule data either * * at start or when housekeeping is enabled for this rule. * * It caches item history data and also prepares delete queue to be * * processed during the first run. * * * ******************************************************************************/ static void hk_history_prepare(zbx_hk_history_rule_t *rule, int now) { DB_RESULT result; DB_ROW row; zbx_hashset_create(&rule->item_cache, 1024, zbx_default_uint64_hash_func, zbx_default_uint64_compare_func); zbx_vector_ptr_create(&rule->delete_queue); zbx_vector_ptr_reserve(&rule->delete_queue, HK_INITIAL_DELETE_QUEUE_SIZE); result = DBselect("select itemid,min(clock) from %s group by itemid", rule->table); while (NULL != (row = DBfetch(result))) { zbx_uint64_t itemid; int min_clock; zbx_hk_item_cache_t item_record; ZBX_STR2UINT64(itemid, row[0]); min_clock = atoi(row[1]); item_record.itemid = itemid; item_record.min_clock = min_clock; zbx_hashset_insert(&rule->item_cache, &item_record, sizeof(zbx_hk_item_cache_t)); } DBfree_result(result); }
/****************************************************************************** * * * Function: convert_condition_values * * * * Purpose: special processing for "value" field in "conditions" table * * * * Parameters: old_id - old id, new_id - new node id * * * * Author: Aleksandrs Saveljevs * * * * Comments: * * * ******************************************************************************/ static void convert_condition_values(int old_id, int new_id, const char *rel_table_name, int type) { zbx_uint64_t prefix; const ZBX_TABLE *r_table; DB_RESULT result; DB_ROW row; zbx_uint64_t value; r_table = DBget_table(rel_table_name); assert(NULL != r_table); prefix = (zbx_uint64_t)__UINT64_C(100000000000000) * (zbx_uint64_t)new_id; if (0 != (r_table->flags & ZBX_SYNC)) prefix += (zbx_uint64_t)__UINT64_C(100000000000) * (zbx_uint64_t)new_id; result = DBselect("select conditionid,value" " from conditions" " where conditiontype=%d", type); while (NULL != (row = DBfetch(result))) { ZBX_STR2UINT64(value, row[1]); value += prefix; DBexecute("update conditions" " set value='" ZBX_FS_UI64 "'" " where conditionid=%s", value, row[0]); } DBfree_result(result); }
/****************************************************************************** * * * Function: is_slave_node * * * * Purpose: * * * * Parameters: * * * * Return value: SUCCEED - slave_nodeid is a slave node of current_nodeid * * FAIL - otherwise * * * * Author: Aleksandrs Saveljevs * * * * Comments: * * * ******************************************************************************/ int is_slave_node(int current_nodeid, int slave_nodeid) { DB_RESULT result; DB_ROW row; int nodeid; int ret = FAIL; result = DBselect( "select nodeid" " from nodes" " where masterid=%d", current_nodeid); while (FAIL == ret && NULL != (row = DBfetch(result))) { nodeid = atoi(row[0]); if (nodeid == slave_nodeid) ret = SUCCEED; else ret = is_slave_node(nodeid, slave_nodeid); } DBfree_result(result); return ret; }
static int housekeeping_events(int now) { int event_history; DB_RESULT result; DB_ROW row1; int res = SUCCEED; zabbix_log( LOG_LEVEL_DEBUG, "In housekeeping_events(%d)", now); result = DBselect("select event_history from config"); row1=DBfetch(result); if(!row1 || DBis_null(row1[0])==SUCCEED) { zabbix_log( LOG_LEVEL_ERR, "No records in table 'config'."); res = FAIL; } else { event_history=atoi(row1[0]); DBexecute ("delete from events where clock < %d", now-24*3600*event_history); } DBfree_result(result); return res; }
/****************************************************************************** * * * Function: register_host * * * * Purpose: register host if one does not exist * * * * Parameters: host ip address * * * * Return value: dhostid or 0 if we didn't add host * * * * Author: Alexei Vladishev * * * * Comments: * * * ******************************************************************************/ static void register_host(DB_DHOST *host,DB_DCHECK *check, zbx_uint64_t druleid, char *ip) { DB_RESULT result; DB_ROW row; char hostname[MAX_STRING_LEN], hostname_esc[MAX_STRING_LEN]; assert(host); assert(check); assert(ip); zabbix_log(LOG_LEVEL_DEBUG, "In register_host(ip:%s)", ip); host->dhostid=0; result = DBselect("select dhostid,druleid,ip,status,lastup,lastdown from dhosts where ip='%s'", ip); row=DBfetch(result); if(!row || DBis_null(row[0])==SUCCEED) { /* Add host only if service is up */ if(check->status == DOBJECT_STATUS_UP) { alarm(CONFIG_TIMEOUT); zbx_gethost_by_ip(ip, hostname, sizeof(hostname)); alarm(0); if (hostname[0] != '\0') DBescape_string(hostname, hostname_esc, sizeof(hostname_esc)); else hostname_esc[0] = '\0'; zabbix_log(LOG_LEVEL_DEBUG, "New host discovered at %s (%s)", ip, hostname); host->dhostid = DBget_maxid("dhosts","dhostid"); DBexecute("insert into dhosts (dhostid, druleid, dns, ip) values (" ZBX_FS_UI64 "," ZBX_FS_UI64 ", '%s','%s')", host->dhostid, druleid, hostname_esc, ip); host->druleid = druleid; strscpy(host->ip,ip); host->status = 0; host->lastup = 0; host->lastdown = 0; } } else { zabbix_log(LOG_LEVEL_DEBUG, "Host is already in database"); ZBX_STR2UINT64(host->dhostid,row[0]); ZBX_STR2UINT64(host->druleid,row[1]); strscpy(host->ip, row[2]); host->status = atoi(row[3]); host->lastup = atoi(row[4]); host->lastdown = atoi(row[5]); } DBfree_result(result); zabbix_log(LOG_LEVEL_DEBUG, "End register_host()"); }
/****************************************************************************** * * * Function: housekeeping_history_and_trends * * * * Purpose: remove outdated information from history and trends * * * * Parameters: now - current timestamp * * * * Return value: SUCCEED - information removed succesfully * * FAIL - otherwise * * * * Author: Alexei Vladishev * * * * Comments: * * * ******************************************************************************/ static int housekeeping_history_and_trends(int now) { DB_ITEM item; DB_RESULT result; DB_ROW row; int deleted = 0; zabbix_log( LOG_LEVEL_DEBUG, "In housekeeping_history_and_trends(%d)", now); result = DBselect("select itemid,history,trends from items"); while((row=DBfetch(result))) { ZBX_STR2UINT64(item.itemid,row[0]); item.history=atoi(row[1]); item.trends=atoi(row[2]); deleted += delete_history("history", item.itemid, item.history, now); deleted += delete_history("history_uint", item.itemid, item.history, now); deleted += delete_history("history_str", item.itemid, item.history, now); deleted += delete_history("history_text", item.itemid, item.history, now); deleted += delete_history("history_log", item.itemid, item.history, now); deleted += delete_history("trends", item.itemid, item.trends, now); } DBfree_result(result); return deleted; }
/****************************************************************************** * * * Function: convert_triggers_expression * * * * Purpose: convert trigger expressions to new node ID * * * * Parameters: old_id - old id, new_id - new node id * * * * Author: Alexander Vladishev * * * * Comments: * * * ******************************************************************************/ static void convert_triggers_expression(int old_id, int new_id) { zbx_uint64_t prefix; const ZBX_TABLE *r_table; DB_RESULT result; DB_ROW row; char new_expression[MAX_STRING_LEN], *new_expression_esc; r_table = DBget_table("functions"); assert(NULL != r_table); prefix = (zbx_uint64_t)__UINT64_C(100000000000000) * (zbx_uint64_t)new_id; if (0 != (r_table->flags & ZBX_SYNC)) prefix += (zbx_uint64_t)__UINT64_C(100000000000) * (zbx_uint64_t)new_id; result = DBselect("select expression,triggerid from triggers"); while (NULL != (row = DBfetch(result))) { memset(new_expression, 0, sizeof(new_expression)); convert_expression(old_id, new_id, prefix, row[0], new_expression); new_expression_esc = DBdyn_escape_string_len(new_expression, TRIGGER_EXPRESSION_LEN); DBexecute("update triggers set expression='%s' where triggerid=%s", new_expression_esc, row[1]); zbx_free(new_expression_esc); } DBfree_result(result); }
static int housekeeping_alerts(int now) { int alert_history; DB_RESULT result; DB_ROW row; int res = SUCCEED; int deleted; zabbix_log( LOG_LEVEL_DEBUG, "In housekeeping_alerts(%d)", now); result = DBselect("select alert_history from config"); row=DBfetch(result); if(!row || DBis_null(row[0])==SUCCEED) { zabbix_log( LOG_LEVEL_ERR, "No records in table 'config'."); res = FAIL; } else { alert_history=atoi(row[0]); deleted = DBexecute("delete from alerts where clock<%d", now-24*3600*alert_history); zabbix_log( LOG_LEVEL_DEBUG, "Deleted [%ld] records from table [alerts]", deleted); } DBfree_result(result); return res; }