static void execute_commands(DB_EVENT *event, zbx_uint64_t actionid, zbx_uint64_t operationid, int esc_step) { const char *__function_name = "execute_commands"; DB_RESULT result; DB_ROW row; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); result = DBselect( "select distinct h.hostid,h.host,o.type,o.scriptid,o.execute_on,o.port" ",o.authtype,o.username,o.password,o.publickey,o.privatekey,o.command" #ifdef HAVE_OPENIPMI ",h.ipmi_authtype,h.ipmi_privilege,h.ipmi_username,h.ipmi_password" #endif " from opcommand o,opcommand_grp og,hosts_groups hg,hosts h" " where o.operationid=og.operationid" " and og.groupid=hg.groupid" " and hg.hostid=h.hostid" " and o.operationid=" ZBX_FS_UI64 " and h.status=%d" " union " "select distinct h.hostid,h.host,o.type,o.scriptid,o.execute_on,o.port" ",o.authtype,o.username,o.password,o.publickey,o.privatekey,o.command" #ifdef HAVE_OPENIPMI ",h.ipmi_authtype,h.ipmi_privilege,h.ipmi_username,h.ipmi_password" #endif " from opcommand o,opcommand_hst oh,hosts h" " where o.operationid=oh.operationid" " and oh.hostid=h.hostid" " and o.operationid=" ZBX_FS_UI64 " and h.status=%d" " union " "select distinct 0,null,o.type,o.scriptid,o.execute_on,o.port" ",o.authtype,o.username,o.password,o.publickey,o.privatekey,o.command" #ifdef HAVE_OPENIPMI ",0,2,null,null" #endif " from opcommand o,opcommand_hst oh" " where o.operationid=oh.operationid" " and o.operationid=" ZBX_FS_UI64 " and oh.hostid is null", operationid, HOST_STATUS_MONITORED, operationid, HOST_STATUS_MONITORED, operationid); while (NULL != (row = DBfetch(result))) { int rc = SUCCEED; char error[ALERT_ERROR_LEN_MAX]; DC_HOST host; zbx_script_t script; zbx_alert_status_t status; *error = '\0'; memset(&host, 0, sizeof(host)); zbx_script_init(&script); ZBX_STR2UINT64(host.hostid, row[0]); if (0 != host.hostid) { strscpy(host.host, row[1]); #ifdef HAVE_OPENIPMI host.ipmi_authtype = (signed char)atoi(row[12]); host.ipmi_privilege = (unsigned char)atoi(row[13]); strscpy(host.ipmi_username, row[14]); strscpy(host.ipmi_password, row[15]); #endif } else rc = get_dynamic_hostid(event, &host, error, sizeof(error)); script.type = (unsigned char)atoi(row[2]); if (ZBX_SCRIPT_TYPE_GLOBAL_SCRIPT != script.type) { script.command = zbx_strdup(script.command, row[11]); substitute_simple_macros(event, NULL, NULL, NULL, NULL, NULL, &script.command, MACRO_TYPE_MESSAGE, NULL, 0); } if (SUCCEED == rc) { switch (script.type) { case ZBX_SCRIPT_TYPE_CUSTOM_SCRIPT: script.execute_on = (unsigned char)atoi(row[4]); break; case ZBX_SCRIPT_TYPE_SSH: script.authtype = (unsigned char)atoi(row[6]); script.publickey = zbx_strdup(script.publickey, row[9]); script.privatekey = zbx_strdup(script.privatekey, row[10]); /* break; is not missing here */ case ZBX_SCRIPT_TYPE_TELNET: script.port = zbx_strdup(script.port, row[5]); script.username = zbx_strdup(script.username, row[7]); script.password = zbx_strdup(script.password, row[8]); break; case ZBX_SCRIPT_TYPE_GLOBAL_SCRIPT: ZBX_DBROW2UINT64(script.scriptid, row[3]); break; } rc = zbx_execute_script(&host, &script, NULL, error, sizeof(error)); } status = (SUCCEED != rc ? ALERT_STATUS_FAILED : ALERT_STATUS_SENT); add_command_alert(&host, event->eventid, actionid, esc_step, script.command, status, error); zbx_script_clean(&script); } DBfree_result(result); 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; } }