static void execute_operations(DB_ESCALATION *escalation, DB_EVENT *event, DB_ACTION *action) { const char *__function_name = "execute_operations"; DB_RESULT result; DB_ROW row; DB_OPERATION operation; int esc_period = 0, operations = 0; ZBX_USER_MSG *user_msg = NULL, *p; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); if (0 == action->esc_period) { result = DBselect( "select o.operationid,o.operationtype,o.esc_period,o.evaltype," "m.operationid,m.default_msg,subject,message,mediatypeid" " from operations o" " left join opmessage m" " on m.operationid=o.operationid" " where o.actionid=" ZBX_FS_UI64 " and o.operationtype in (%d,%d)", action->actionid, OPERATION_TYPE_MESSAGE, OPERATION_TYPE_COMMAND); } else { escalation->esc_step++; result = DBselect( "select o.operationid,o.operationtype,o.esc_period,o.evaltype," "m.operationid,m.default_msg,subject,message,mediatypeid" " from operations o" " left join opmessage m" " on m.operationid=o.operationid" " where o.actionid=" ZBX_FS_UI64 " and o.operationtype in (%d,%d)" " and o.esc_step_from<=%d" " and (o.esc_step_to=0 or o.esc_step_to>=%d)", action->actionid, OPERATION_TYPE_MESSAGE, OPERATION_TYPE_COMMAND, escalation->esc_step, escalation->esc_step); } while (NULL != (row = DBfetch(result))) { memset(&operation, 0, sizeof(operation)); ZBX_STR2UINT64(operation.operationid, row[0]); operation.actionid = action->actionid; operation.operationtype = atoi(row[1]); operation.esc_period = atoi(row[2]); operation.evaltype = (unsigned char)atoi(row[3]); if (SUCCEED == check_operation_conditions(event, operation.operationid, operation.evaltype)) { unsigned char default_msg; char *subject, *message; zbx_uint64_t mediatypeid; zabbix_log(LOG_LEVEL_DEBUG, "Conditions match our event. Execute operation."); if (0 == esc_period || esc_period > operation.esc_period) esc_period = operation.esc_period; switch (operation.operationtype) { case OPERATION_TYPE_MESSAGE: if (SUCCEED == DBis_null(row[4])) break; default_msg = (unsigned char)atoi(row[5]); ZBX_DBROW2UINT64(mediatypeid, row[8]); if (0 == default_msg) { subject = row[6]; message = row[7]; } else { subject = action->shortdata; message = action->longdata; } add_object_msg(operation.operationid, mediatypeid, &user_msg, subject, message, event->source, event->objectid); break; case OPERATION_TYPE_COMMAND: execute_commands(event, action->actionid, operation.operationid, escalation->esc_step); break; } } else zabbix_log(LOG_LEVEL_DEBUG, "Conditions do not match our event. Do not execute operation."); operations = 1; } DBfree_result(result); while (NULL != user_msg) { p = user_msg; user_msg = user_msg->next; add_message_alert(escalation, event, action, p->userid, p->mediatypeid, p->subject, p->message); zbx_free(p->subject); zbx_free(p->message); zbx_free(p); } if (0 == action->esc_period) { escalation->status = (action->recovery_msg == 1) ? ESCALATION_STATUS_SLEEP : ESCALATION_STATUS_COMPLETED; } else { if (0 == operations) { result = DBselect("select operationid from operations where actionid=" ZBX_FS_UI64 " and esc_step_from>%d", action->actionid, escalation->esc_step); if (NULL != (row = DBfetch(result)) && SUCCEED != DBis_null(row[0])) operations = 1; DBfree_result(result); } if (1 == operations) { esc_period = (0 != esc_period) ? esc_period : action->esc_period; escalation->nextcheck = time(NULL) + esc_period; } else escalation->status = (action->recovery_msg == 1) ? ESCALATION_STATUS_SLEEP : ESCALATION_STATUS_COMPLETED; } zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
static void execute_operations(DB_ESCALATION *escalation, DB_EVENT *event, DB_ACTION *action) { DB_RESULT result; DB_ROW row; DB_OPERATION operation; int esc_period = 0, operations = 0; ZBX_USER_MSG *user_msg = NULL, *p; char *shortdata, *longdata; if (0 == action->esc_period) { result = DBselect("select operationid,operationtype,object,objectid,default_msg,shortdata,longdata" ",esc_period,evaltype from operations where actionid=" ZBX_FS_UI64 " and operationtype in (%d,%d)", action->actionid, OPERATION_TYPE_MESSAGE, OPERATION_TYPE_COMMAND); } else { escalation->esc_step++; result = DBselect("select operationid,operationtype,object,objectid,default_msg,shortdata,longdata" ",esc_period,evaltype from operations where actionid=" ZBX_FS_UI64 " and operationtype in (%d,%d)" " and esc_step_from<=%d and (esc_step_to=0 or esc_step_to>=%d)", action->actionid, OPERATION_TYPE_MESSAGE, OPERATION_TYPE_COMMAND, escalation->esc_step, escalation->esc_step); } while (NULL != (row = DBfetch(result))) { memset(&operation, 0, sizeof(operation)); ZBX_STR2UINT64(operation.operationid, row[0]); operation.actionid = action->actionid; operation.operationtype = atoi(row[1]); operation.object = atoi(row[2]); ZBX_STR2UINT64(operation.objectid, row[3]); operation.default_msg = atoi(row[4]); operation.shortdata = strdup(row[5]); operation.longdata = strdup(row[6]); operation.esc_period = atoi(row[7]); operation.evaltype = atoi(row[8]); if (SUCCEED == check_operation_conditions(event, &operation)) { zabbix_log(LOG_LEVEL_DEBUG, "Conditions match our event. Execute operation."); substitute_macros(event, action, NULL, &operation.shortdata); substitute_macros(event, action, NULL, &operation.longdata); if (0 == esc_period || esc_period > operation.esc_period) esc_period = operation.esc_period; switch (operation.operationtype) { case OPERATION_TYPE_MESSAGE: if (0 == operation.default_msg) { shortdata = operation.shortdata; longdata = operation.longdata; } else { shortdata = action->shortdata; longdata = action->longdata; } add_object_msg(event->source, escalation->triggerid, &operation, &user_msg, shortdata, longdata); break; case OPERATION_TYPE_COMMAND: add_command_alert(escalation, event, action, operation.longdata); break; default: break; } } else zabbix_log(LOG_LEVEL_DEBUG, "Conditions do not match our event. Do not execute operation."); zbx_free(operation.shortdata); zbx_free(operation.longdata); operations = 1; } DBfree_result(result); while (NULL != user_msg) { p = user_msg; user_msg = user_msg->next; add_message_alert(escalation, event, action, p->userid, p->mediatypeid, p->subject, p->message); zbx_free(p->subject); zbx_free(p->message); zbx_free(p); } if (0 == action->esc_period) { escalation->status = (action->recovery_msg == 1) ? ESCALATION_STATUS_SLEEP : ESCALATION_STATUS_COMPLETED; } else { if (0 == operations) { result = DBselect("select operationid from operations where actionid=" ZBX_FS_UI64 " and esc_step_from>%d", action->actionid, escalation->esc_step); if (NULL != (row = DBfetch(result)) && SUCCEED != DBis_null(row[0])) operations = 1; DBfree_result(result); } if (1 == operations) { esc_period = (0 != esc_period) ? esc_period : action->esc_period; escalation->nextcheck = time(NULL) + esc_period; } else escalation->status = (action->recovery_msg == 1) ? ESCALATION_STATUS_SLEEP : ESCALATION_STATUS_COMPLETED; } }