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; }
/****************************************************************************** * * * Function: update_checksums * * * * Purpose: overwrite old checksums with new ones * * * * Parameters: * * * * Return value: SUCCESS - calculated succesfully * * FAIL - an error occured * * * * Author: Alexei Vladishev * * * * Comments: * * * ******************************************************************************/ static int update_checksums() { DBexecute("delete from node_cksum where cksumtype=%d", NODE_CKSUM_TYPE_OLD); DBexecute("update node_cksum set cksumtype=%d where cksumtype=%d", NODE_CKSUM_TYPE_OLD, NODE_CKSUM_TYPE_NEW); return SUCCEED; }
/****************************************************************************** * * * Function: housekeeping_process_log * * * * Purpose: process table 'housekeeper' and remove data if required * * * * Parameters: * * * * Return value: SUCCEED - information removed succesfully * * FAIL - otherwise * * * * Author: Alexei Vladishev * * * * Comments: * * * ******************************************************************************/ static int housekeeping_process_log() { DB_HOUSEKEEPER housekeeper; DB_RESULT result; DB_ROW row; int res = SUCCEED; long deleted; zabbix_log( LOG_LEVEL_DEBUG, "In housekeeping_process_log()"); /* order by tablename to effectively use DB cache */ result = DBselect("select housekeeperid, tablename, field, value from housekeeper order by tablename"); while((row=DBfetch(result))) { ZBX_STR2UINT64(housekeeper.housekeeperid,row[0]); housekeeper.tablename=row[1]; housekeeper.field=row[2]; ZBX_STR2UINT64(housekeeper.value,row[3]); #ifdef HAVE_ORACLE deleted = DBexecute("delete from %s where %s=" ZBX_FS_UI64 " and rownum<500", housekeeper.tablename, housekeeper.field, housekeeper.value); #elif defined(HAVE_POSTGRESQL) deleted = DBexecute("delete from %s where oid in (select oid from %s where %s=" ZBX_FS_UI64 " limit 500)", housekeeper.tablename, housekeeper.tablename, housekeeper.field, housekeeper.value); #else deleted = DBexecute("delete from %s where %s=" ZBX_FS_UI64 " limit 500", housekeeper.tablename, housekeeper.field, housekeeper.value); #endif if(deleted == 0) { DBexecute("delete from housekeeper where housekeeperid=" ZBX_FS_UI64, housekeeper.housekeeperid); } else { zabbix_log( LOG_LEVEL_DEBUG, "Deleted [%ld] records from table [%s]", deleted, housekeeper.tablename); } } DBfree_result(result); return res; }
/****************************************************************************** * * * Function: process_deleted_records * * * * Purpose: * * * * Parameters: * * * * Return value: * * * * Author: Alexander Vladishev * * * * Comments: * * * ******************************************************************************/ static void process_deleted_records(int nodeid, char *data, int sender_nodetype) { const char *__function_name = "process_deleted_records"; char *r, *lf; const ZBX_TABLE *table = NULL; zbx_uint64_t recid, *ids = NULL; int ids_alloc = 0, ids_num = 0; char *sql = NULL; size_t sql_alloc = 4 * ZBX_KIBIBYTE, sql_offset = 0; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); sql = zbx_malloc(sql, sql_alloc); DBbegin_multiple_update(&sql, &sql_alloc, &sql_offset); for (r = data; '\0' != *r;) { if (NULL != (lf = strchr(r, '\n'))) *lf = '\0'; zbx_get_next_field((const char **)&r, &buf, &buf_alloc, ZBX_DM_DELIMITER); if (NULL == table || 0 != strcmp(table->table, buf)) { make_delete_sql(&sql, &sql_alloc, &sql_offset, table, ids, &ids_num); if (NULL == (table = DBget_table(buf))) { zabbix_log(LOG_LEVEL_DEBUG, "%s(): cannot find table [%s]", __function_name, buf); goto next; } } zbx_get_next_field((const char **)&r, &buf, &buf_alloc, ZBX_DM_DELIMITER); ZBX_STR2UINT64(recid, buf); if ('2' == *r) /* NODE_CONFIGLOG_OP_DELETE */ uint64_array_add(&ids, &ids_alloc, &ids_num, recid, 64); next: if (lf != NULL) { *lf++ = '\n'; r = lf; } else break; } make_delete_sql(&sql, &sql_alloc, &sql_offset, table, ids, &ids_num); DBend_multiple_update(&sql, &sql_alloc, &sql_offset); if (sql_offset > 16) /* In ORACLE always present begin..end; */ DBexecute("%s", sql); zbx_free(sql); zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
/****************************************************************************** * * * 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; } }
static int DBpatch_2030113(void) { if (ZBX_DB_OK > DBexecute("delete from profiles where idx in ('web.latest.php.sort', 'web.httpmon.php.sort')")) return FAIL; return SUCCEED; }
static int DBpatch_2030028(void) { if (ZBX_DB_OK > DBexecute("update items set formula='' where flags=%d", ZBX_FLAG_DISCOVERY_RULE)) return FAIL; return SUCCEED; }
/****************************************************************************** * * * Function: op_host_disable * * * * Purpose: disable host * * * * Author: Alexander Vladishev * * * ******************************************************************************/ void op_host_disable(DB_EVENT *event) { const char *__function_name = "op_host_disable"; zbx_uint64_t hostid; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); if (event->source != EVENT_SOURCE_DISCOVERY && event->source != EVENT_SOURCE_AUTO_REGISTRATION) return; if (event->object != EVENT_OBJECT_DHOST && event->object != EVENT_OBJECT_DSERVICE && event->object != EVENT_OBJECT_ZABBIX_ACTIVE) return; if (0 == (hostid = add_discovered_host(event))) return; DBexecute( "update hosts" " set status=%d" " where hostid=" ZBX_FS_UI64, HOST_STATUS_NOT_MONITORED, hostid); zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
void zbx_db_init(char *dbname) { #if defined(HAVE_SQLITE3) struct stat buf; if (0 != stat(dbname, &buf)) { zabbix_log(LOG_LEVEL_WARNING, "cannot open database file \"%s\": %s", dbname, zbx_strerror(errno)); zabbix_log(LOG_LEVEL_WARNING, "creating database ..."); if (SQLITE_OK != sqlite3_open(dbname, &conn)) { zabbix_errlog(ERR_Z3002, dbname, 0, sqlite3_errmsg(conn)); exit(FAIL); } zbx_create_sqlite3_mutex(dbname); DBexecute("%s", db_schema); DBclose(); } else zbx_create_sqlite3_mutex(dbname); #endif /* HAVE_SQLITE3 */ }
static int DBpatch_2010068(void) { if (ZBX_DB_OK <= DBexecute( "update config" " set hk_events_mode=0," "hk_services_mode=0," "hk_audit_mode=0," "hk_sessions_mode=0," "hk_history_mode=0," "hk_trends_mode=0," "hk_events_trigger=" "case when event_history>alert_history" " then event_history else alert_history end," "hk_events_discovery=" "case when event_history>alert_history" " then event_history else alert_history end," "hk_events_autoreg=" "case when event_history>alert_history" " then event_history else alert_history end," "hk_events_internal=" "case when event_history>alert_history" " then event_history else alert_history end")) { return SUCCEED; } return FAIL; }
/****************************************************************************** * * * 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()"); }
static int DBpatch_2010037(void) { if (ZBX_DB_OK <= DBexecute("update config set server_check_interval=10")) return SUCCEED; return FAIL; }
static int DBpatch_2010050(void) { char *fields[] = {"ts_from", "ts_to", NULL}; DB_RESULT result; DB_ROW row; int i; time_t ts; struct tm *tm; for (i = 0; NULL != fields[i]; i++) { result = DBselect( "select timeid,%s" " from services_times" " where type in (%d,%d)" " and %s>%d", fields[i], 0 /* SERVICE_TIME_TYPE_UPTIME */, 1 /* SERVICE_TIME_TYPE_DOWNTIME */, fields[i], SEC_PER_WEEK); while (NULL != (row = DBfetch(result))) { if (SEC_PER_WEEK < (ts = (time_t)atoi(row[1]))) { tm = localtime(&ts); ts = tm->tm_wday * SEC_PER_DAY + tm->tm_hour * SEC_PER_HOUR + tm->tm_min * SEC_PER_MIN; DBexecute("update services_times set %s=%d where timeid=%s", fields[i], (int)ts, row[0]); } } DBfree_result(result); } return SUCCEED; }
static int DBpatch_2010185(void) { if (ZBX_DB_OK > DBexecute("update sysmaps_elements set label_location=-1 where label_location is null")) return FAIL; return SUCCEED; }
static int DBpatch_2010176(void) { DB_RESULT result; DB_ROW row; char *name, *name_esc; int ret = SUCCEED; result = DBselect("select scriptid,name from scripts"); while (SUCCEED == ret && NULL != (row = DBfetch(result))) { name = zbx_dyn_escape_string(row[1], "/\\"); if (0 != strcmp(name, row[1])) { name_esc = DBdyn_escape_string_len(name, 255); if (ZBX_DB_OK > DBexecute("update scripts set name='%s' where scriptid=%s", name_esc, row[0])) ret = FAIL; zbx_free(name_esc); } zbx_free(name); } DBfree_result(result); return ret; }
static int DBpatch_2030065(void) { DB_RESULT result; DB_ROW row; int local_nodeid = 0; zbx_uint64_t min, max; /* 1 - ZBX_NODE_LOCAL */ if (NULL == (result = DBselect("select nodeid from nodes where nodetype=1"))) return FAIL; if (NULL != (row = DBfetch(result))) local_nodeid = atoi(row[0]); DBfree_result(result); if (0 == local_nodeid) return SUCCEED; min = local_nodeid * __UINT64_C(100000000000000); max = min + __UINT64_C(100000000000000) - 1; if (ZBX_DB_OK <= DBexecute( "delete from config where not configid between " ZBX_FS_UI64 " and " ZBX_FS_UI64, min, max)) { return SUCCEED; } return FAIL; }
static int DBcreate_dbversion_table(void) { const ZBX_TABLE table = {"dbversion", "", 0, { {"mandatory", "0", NULL, NULL, 0, ZBX_TYPE_INT, ZBX_NOTNULL, 0}, {"optional", "0", NULL, NULL, 0, ZBX_TYPE_INT, ZBX_NOTNULL, 0}, {NULL} } }; int ret; DBbegin(); if (SUCCEED == (ret = DBcreate_table(&table))) { if (ZBX_DB_OK > DBexecute("insert into dbversion (mandatory,optional) values (%d,%d)", ZBX_FIRST_DB_VERSION, ZBX_FIRST_DB_VERSION)) { ret = FAIL; } } DBend(ret); return ret; }
static int DBpatch_2010080(void) { DB_RESULT result; DB_ROW row; zbx_uint64_t applicationid, templateid, application_templateid = 1; int ret = FAIL; result = DBselect("select applicationid,templateid from applications where templateid is not null"); while (NULL != (row = DBfetch(result))) { ZBX_STR2UINT64(applicationid, row[0]); ZBX_STR2UINT64(templateid, row[1]); if (ZBX_DB_OK > DBexecute( "insert into application_template" " (application_templateid,applicationid,templateid)" " values (" ZBX_FS_UI64 "," ZBX_FS_UI64 "," ZBX_FS_UI64 ")", application_templateid++, applicationid, templateid)) { goto out; } } ret = SUCCEED; out: DBfree_result(result); return ret; }
/****************************************************************************** * * * Function: tm_remove_old_tasks * * * * Purpose: remove old done/expired tasks * * * ******************************************************************************/ static void tm_remove_old_tasks(int now) { DBbegin(); DBexecute("delete from task where status in (%d,%d) and clock<=%d", ZBX_TM_STATUS_DONE, ZBX_TM_STATUS_EXPIRED, now - ZBX_TM_CLEANUP_TASK_AGE); DBcommit(); }
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; }
static void add_command_alert(DB_ESCALATION *escalation, DB_EVENT *event, DB_ACTION *action, char *command) { zbx_uint64_t alertid; int now; char *command_esc; zabbix_log(LOG_LEVEL_DEBUG, "In add_command_alert()"); alertid = DBget_maxid("alerts", "alertid"); now = time(NULL); command_esc = DBdyn_escape_string(command); DBexecute("insert into alerts (alertid,actionid,eventid,clock,message,status,alerttype,esc_step)" " values (" ZBX_FS_UI64 "," ZBX_FS_UI64 "," ZBX_FS_UI64 ",%d,'%s',%d,%d,%d)", alertid, action->actionid, event->eventid, now, command_esc, ALERT_STATUS_SENT, ALERT_TYPE_COMMAND, escalation->esc_step); op_run_commands(command); zbx_free(command_esc); }
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: op_group_del * * * * Purpose: delete group from discovered host * * * * Parameters: event - [IN] event data * * groupid - [IN] group identificator from database * * * * Author: Alexei Vladishev * * * ******************************************************************************/ void op_group_del(DB_EVENT *event, zbx_uint64_t groupid) { const char *__function_name = "op_group_del"; zbx_uint64_t hostid; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); if (event->source != EVENT_SOURCE_DISCOVERY) return; if (event->object != EVENT_OBJECT_DHOST && event->object != EVENT_OBJECT_DSERVICE) return; if (0 == (hostid = select_discovered_host(event))) return; DBexecute( "delete from hosts_groups" " where hostid=" ZBX_FS_UI64 " and groupid=" ZBX_FS_UI64, hostid, groupid); zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
/****************************************************************************** * * * 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); }
static void add_command_alert(DC_HOST *host, zbx_uint64_t eventid, zbx_uint64_t actionid, int esc_step, const char *command, zbx_alert_status_t status, const char *error) { const char *__function_name = "add_command_alert"; zbx_uint64_t alertid; int now; char *tmp = NULL, *command_esc, *error_esc; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); alertid = DBget_maxid("alerts"); now = (int)time(NULL); tmp = zbx_dsprintf(tmp, "%s:%s", host->host, NULL == command ? "" : command); command_esc = DBdyn_escape_string_len(tmp, ALERT_MESSAGE_LEN); error_esc = DBdyn_escape_string_len(error, ALERT_ERROR_LEN); zbx_free(tmp); DBexecute("insert into alerts" " (alertid,actionid,eventid,clock,message,status,error,alerttype,esc_step)" " values " "(" ZBX_FS_UI64 "," ZBX_FS_UI64 "," ZBX_FS_UI64 ",%d,'%s',%d,'%s',%d,%d)", alertid, actionid, eventid, now, command_esc, (int)status, error_esc, ALERT_TYPE_COMMAND, esc_step); zbx_free(error_esc); zbx_free(command_esc); zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
static int DBpatch_2030110(void) { if (ZBX_DB_OK > DBexecute("delete from profiles where idx='web.view.application'")) return FAIL; return SUCCEED; }
/****************************************************************************** * * * 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; }
static int DBpatch_2030114(void) { if (ZBX_DB_OK > DBexecute("delete from profiles where idx='web.httpconf.php.sort' and value_str='h.hostid'")) return FAIL; return SUCCEED; }
/****************************************************************************** * * * 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 DBpatch_2030109(void) { if (ZBX_DB_OK <= DBexecute("update screens_items set rowspan=1 where rowspan=0")) return SUCCEED; return FAIL; }