static void process_escalations(int now) { DB_RESULT result; DB_ROW row; DB_ESCALATION escalation; zabbix_log(LOG_LEVEL_DEBUG, "In process_escalations()"); result = DBselect("select escalationid,actionid,triggerid,eventid,r_eventid,esc_step,status" " from escalations where status in (%d,%d) and nextcheck<=%d" DB_NODE, ESCALATION_STATUS_ACTIVE, ESCALATION_STATUS_RECOVERY, now, DBnode_local("escalationid")); while (NULL != (row = DBfetch(result))) { memset(&escalation, 0, sizeof(escalation)); ZBX_STR2UINT64(escalation.escalationid, row[0]); ZBX_STR2UINT64(escalation.actionid, row[1]); ZBX_STR2UINT64(escalation.triggerid, row[2]); ZBX_STR2UINT64(escalation.eventid, row[3]); ZBX_STR2UINT64(escalation.r_eventid, row[4]); escalation.esc_step = atoi(row[5]); escalation.status = atoi(row[6]); escalation.nextcheck = 0; DBbegin(); execute_escalation(&escalation); if (escalation.status == ESCALATION_STATUS_COMPLETED) DBremove_escalation(escalation.escalationid); else DBexecute("update escalations set status=%d,esc_step=%d,nextcheck=%d" " where escalationid=" ZBX_FS_UI64, escalation.status, escalation.esc_step, escalation.nextcheck, escalation.escalationid); DBcommit(); } DBfree_result(result); }
static void process_escalations(int now) { const char *__function_name = "process_escalations"; DB_RESULT result; DB_ROW row; DB_ESCALATION escalation, last_escalation; zbx_vector_uint64_t escalationids; char *sql = NULL; size_t sql_alloc = ZBX_KIBIBYTE, sql_offset; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); zbx_vector_uint64_create(&escalationids); sql = zbx_malloc(sql, sql_alloc); result = DBselect( "select escalationid,actionid,triggerid,eventid,r_eventid,esc_step,status,nextcheck" " from escalations" " where 1=1" DB_NODE " order by actionid,triggerid,escalationid", DBnode_local("escalationid")); memset(&escalation, 0, sizeof(escalation)); do { memset(&last_escalation, 0, sizeof(last_escalation)); if (NULL != (row = DBfetch(result))) { ZBX_STR2UINT64(last_escalation.escalationid, row[0]); ZBX_STR2UINT64(last_escalation.actionid, row[1]); ZBX_DBROW2UINT64(last_escalation.triggerid, row[2]); ZBX_DBROW2UINT64(last_escalation.eventid, row[3]); ZBX_DBROW2UINT64(last_escalation.r_eventid, row[4]); last_escalation.esc_step = atoi(row[5]); last_escalation.status = atoi(row[6]); last_escalation.nextcheck = atoi(row[7]); /* just delete on the next cycle */ if (0 != last_escalation.r_eventid) last_escalation.status = ESCALATION_STATUS_COMPLETED; } if (0 != escalation.escalationid) { unsigned char esc_superseded = 0; if (ESCALATION_STATUS_COMPLETED == escalation.status) { /* delete a recovery record and skip all processing */ zbx_vector_uint64_append(&escalationids, escalation.escalationid); goto next; } if (0 != last_escalation.escalationid) { esc_superseded = (escalation.actionid == last_escalation.actionid && escalation.triggerid == last_escalation.triggerid); if (1 == esc_superseded) { if (0 != last_escalation.r_eventid) { /* recover this escalation */ escalation.r_eventid = last_escalation.r_eventid; escalation.status = ESCALATION_STATUS_ACTIVE; } else if (escalation.nextcheck > now || ESCALATION_STATUS_SLEEP == escalation.status) { zbx_vector_uint64_append(&escalationids, escalation.escalationid); goto next; } } } if (ESCALATION_STATUS_ACTIVE != escalation.status || (escalation.nextcheck > now && 0 == escalation.r_eventid)) { goto next; } DBbegin(); if (escalation.nextcheck <= now) execute_escalation(&escalation); /* execute recovery */ if (ESCALATION_STATUS_COMPLETED != escalation.status && 0 != escalation.r_eventid) { escalation.status = ESCALATION_STATUS_RECOVERY; execute_escalation(&escalation); } else if (1 == esc_superseded) escalation.status = ESCALATION_STATUS_COMPLETED; sql_offset = 0; if (ESCALATION_STATUS_COMPLETED != escalation.status) { zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset, "update escalations set status=%d", escalation.status); if (ESCALATION_STATUS_ACTIVE == escalation.status) { zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset, ",esc_step=%d,nextcheck=%d", escalation.esc_step, escalation.nextcheck); } zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset, " where escalationid=" ZBX_FS_UI64, escalation.escalationid); } else { zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset, "delete from escalations where escalationid=" ZBX_FS_UI64, escalation.escalationid); } DBexecute("%s", sql); DBcommit(); } next: if (NULL != row) memcpy(&escalation, &last_escalation, sizeof(escalation)); } while (NULL != row); DBfree_result(result); zbx_free(sql); /* delete completed escalations */ if (0 != escalationids.values_num) { zbx_vector_uint64_sort(&escalationids, ZBX_DEFAULT_UINT64_COMPARE_FUNC); DBbegin(); DBexecute_multiple_query("delete from escalations where", "escalationid", &escalationids); DBcommit(); } zbx_vector_uint64_destroy(&escalationids); zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }