/****************************************************************************** * * * Function: add_discovered_host * * * * Purpose: add discovered host if it was not added already * * * * Parameters: dhostid - discovered host id * * * * Return value: hostid - new/existing hostid * * * * Author: Alexei Vladishev * * * ******************************************************************************/ static zbx_uint64_t add_discovered_host(const DB_EVENT *event) { const char *__function_name = "add_discovered_host"; DB_RESULT result; DB_RESULT result2; DB_ROW row; DB_ROW row2; zbx_uint64_t dhostid, hostid = 0, proxy_hostid; char *host = NULL, *host_esc, *host_unique; unsigned short port; zbx_uint64_t groupid; zbx_vector_uint64_t groupids; unsigned char svc_type, interface_type; zabbix_log(LOG_LEVEL_DEBUG, "In %s() eventid:" ZBX_FS_UI64, __function_name, event->eventid); zbx_vector_uint64_create(&groupids); if (0 == *(zbx_uint64_t *)DCconfig_get_config_data(&groupid, CONFIG_DISCOVERY_GROUPID)) { zabbix_log(LOG_LEVEL_WARNING, "cannot add discovered host: group for discovered hosts is not defined"); goto clean; } zbx_vector_uint64_append(&groupids, groupid); if (EVENT_OBJECT_DHOST == event->object || EVENT_OBJECT_DSERVICE == event->object) { if (EVENT_OBJECT_DHOST == event->object) { result = DBselect( "select ds.dhostid,dr.proxy_hostid,ds.ip,ds.dns,ds.port,ds.type" " from drules dr,dchecks dc,dservices ds" " where dc.druleid=dr.druleid" " and ds.dcheckid=dc.dcheckid" " and ds.dhostid=" ZBX_FS_UI64 " order by ds.dserviceid", event->objectid); } else { result = DBselect( "select ds.dhostid,dr.proxy_hostid,ds.ip,ds.dns,ds.port,ds.type" " from drules dr,dchecks dc,dservices ds,dservices ds1" " where dc.druleid=dr.druleid" " and ds.dcheckid=dc.dcheckid" " and ds1.dhostid=ds.dhostid" " and ds1.dserviceid=" ZBX_FS_UI64 " order by ds.dserviceid", event->objectid); } while (NULL != (row = DBfetch(result))) { ZBX_STR2UINT64(dhostid, row[0]); ZBX_DBROW2UINT64(proxy_hostid, row[1]); svc_type = (unsigned char)atoi(row[5]); switch (svc_type) { case SVC_AGENT: port = (unsigned short)atoi(row[4]); interface_type = INTERFACE_TYPE_AGENT; break; case SVC_SNMPv1: case SVC_SNMPv2c: case SVC_SNMPv3: port = (unsigned short)atoi(row[4]); interface_type = INTERFACE_TYPE_SNMP; break; default: port = ZBX_DEFAULT_AGENT_PORT; interface_type = INTERFACE_TYPE_AGENT; } if (0 == hostid) { result2 = DBselect( "select distinct h.hostid" " from hosts h,interface i,dservices ds" " where h.hostid=i.hostid" " and i.ip=ds.ip" " and h.proxy_hostid%s" " and ds.dhostid=" ZBX_FS_UI64 ZBX_SQL_NODE " order by h.hostid", DBsql_id_cmp(proxy_hostid), dhostid, DBand_node_local("h.hostid")); if (NULL != (row2 = DBfetch(result2))) ZBX_STR2UINT64(hostid, row2[0]); DBfree_result(result2); } if (0 == hostid) { hostid = DBget_maxid("hosts"); /* for host uniqueness purposes */ host = zbx_strdup(host, '\0' != *row[3] ? row[3] : row[2]); make_hostname(host); /* replace not-allowed symbols */ host_unique = DBget_unique_hostname_by_sample(host); host_esc = DBdyn_escape_string(host_unique); zbx_free(host); DBexecute("insert into hosts" " (hostid,proxy_hostid,host,name)" " values" " (" ZBX_FS_UI64 ",%s,'%s','%s')", hostid, DBsql_id_ins(proxy_hostid), host_esc, host_esc); DBadd_interface(hostid, interface_type, 1, row[2], row[3], port); zbx_free(host_unique); zbx_free(host_esc); add_discovered_host_groups(hostid, &groupids); } else { DBadd_interface(hostid, interface_type, 1, row[2], row[3], port); } } DBfree_result(result); } else if (EVENT_OBJECT_ZABBIX_ACTIVE == event->object) { result = DBselect( "select proxy_hostid,host,listen_ip,listen_dns,listen_port" " from autoreg_host" " where autoreg_hostid=" ZBX_FS_UI64, event->objectid); if (NULL != (row = DBfetch(result))) { char *sql = NULL; zbx_uint64_t host_proxy_hostid; ZBX_DBROW2UINT64(proxy_hostid, row[0]); host_esc = DBdyn_escape_string_len(row[1], HOST_HOST_LEN); port = (unsigned short)atoi(row[4]); result2 = DBselect( "select null" " from hosts" " where host='%s'" " and status=%d", host_esc, HOST_STATUS_TEMPLATE); if (NULL != (row2 = DBfetch(result2))) { zabbix_log(LOG_LEVEL_WARNING, "cannot add discovered host \"%s\":" " template with the same name already exists", row[1]); DBfree_result(result2); goto out; } DBfree_result(result2); sql = zbx_dsprintf(sql, "select hostid,proxy_hostid" " from hosts" " where host='%s'" " and flags<>%d" " and status in (%d,%d)" ZBX_SQL_NODE " order by hostid", host_esc, ZBX_FLAG_DISCOVERY_PROTOTYPE, HOST_STATUS_MONITORED, HOST_STATUS_NOT_MONITORED, DBand_node_local("hostid")); result2 = DBselectN(sql, 1); zbx_free(sql); if (NULL == (row2 = DBfetch(result2))) { hostid = DBget_maxid("hosts"); DBexecute("insert into hosts" " (hostid,proxy_hostid,host,name)" " values" " (" ZBX_FS_UI64 ",%s,'%s','%s')", hostid, DBsql_id_ins(proxy_hostid), host_esc, host_esc); DBadd_interface(hostid, INTERFACE_TYPE_AGENT, 1, row[2], row[3], port); add_discovered_host_groups(hostid, &groupids); } else { ZBX_STR2UINT64(hostid, row2[0]); ZBX_DBROW2UINT64(host_proxy_hostid, row2[1]); if (host_proxy_hostid != proxy_hostid) { DBexecute("update hosts" " set proxy_hostid=%s" " where hostid=" ZBX_FS_UI64, DBsql_id_ins(proxy_hostid), hostid); } DBadd_interface(hostid, INTERFACE_TYPE_AGENT, 1, row[2], row[3], port); } DBfree_result(result2); out: zbx_free(host_esc); } DBfree_result(result); } clean: zbx_vector_uint64_destroy(&groupids); zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); return hostid; }
static void process_maintenance_hosts(zbx_host_maintenance_t **hm, int *hm_alloc, int *hm_count, time_t maintenance_from, zbx_uint64_t maintenanceid, int maintenance_type) { const char *__function_name = "process_maintenance_hosts"; DB_RESULT result; DB_ROW row; zbx_uint64_t host_hostid, host_maintenanceid; int host_maintenance_status, host_maintenance_type, host_maintenance_from; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); assert(maintenanceid); result = DBselect( "select h.hostid,h.host,h.maintenanceid,h.maintenance_status," "h.maintenance_type,h.maintenance_from" " from maintenances_hosts mh,hosts h" " where mh.hostid=h.hostid" " and h.status=%d" " and mh.maintenanceid=" ZBX_FS_UI64, HOST_STATUS_MONITORED, maintenanceid); while (NULL != (row = DBfetch(result))) { ZBX_STR2UINT64(host_hostid, row[0]); ZBX_DBROW2UINT64(host_maintenanceid, row[2]); host_maintenance_status = atoi(row[3]); host_maintenance_type = atoi(row[4]); host_maintenance_from = atoi(row[5]); get_host_maintenance(hm, hm_alloc, hm_count, host_hostid, row[1], maintenance_from, maintenanceid, maintenance_type, host_maintenanceid, host_maintenance_status, host_maintenance_type, host_maintenance_from); } DBfree_result(result); result = DBselect( "select h.hostid,h.host,h.maintenanceid,h.maintenance_status," "h.maintenance_type,h.maintenance_from" " from maintenances_groups mg,hosts_groups hg,hosts h" " where mg.groupid=hg.groupid" " and hg.hostid=h.hostid" " and h.status=%d" " and mg.maintenanceid=" ZBX_FS_UI64, HOST_STATUS_MONITORED, maintenanceid); while (NULL != (row = DBfetch(result))) { ZBX_STR2UINT64(host_hostid, row[0]); ZBX_DBROW2UINT64(host_maintenanceid, row[2]); host_maintenance_status = atoi(row[3]); host_maintenance_type = atoi(row[4]); host_maintenance_from = atoi(row[5]); get_host_maintenance(hm, hm_alloc, hm_count, host_hostid, row[1], maintenance_from, maintenanceid, maintenance_type, host_maintenanceid, host_maintenance_status, host_maintenance_type, host_maintenance_from); } DBfree_result(result); zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
/****************************************************************************** * * * Function: db_get_query_events * * * * Purpose: get open, recently resolved and resolved problems with suppress * * data from database and prepare event query, event data structures * * * ******************************************************************************/ static void db_get_query_events(zbx_vector_ptr_t *event_queries, zbx_vector_ptr_t *event_data) { DB_ROW row; DB_RESULT result; zbx_event_suppress_query_t *query; zbx_event_suppress_data_t *data = NULL; zbx_uint64_t eventid; zbx_uint64_pair_t pair; zbx_vector_uint64_t eventids; /* get open or recently closed problems */ result = DBselect("select eventid,objectid,r_eventid" " from problem" " where source=%d" " and object=%d" " and " ZBX_SQL_MOD(eventid, %d) "=%d" " order by eventid", EVENT_SOURCE_TRIGGERS, EVENT_OBJECT_TRIGGER, CONFIG_TIMER_FORKS, process_num - 1); while (NULL != (row = DBfetch(result))) { query = (zbx_event_suppress_query_t *)zbx_malloc(NULL, sizeof(zbx_event_suppress_query_t)); ZBX_STR2UINT64(query->eventid, row[0]); ZBX_STR2UINT64(query->triggerid, row[1]); ZBX_DBROW2UINT64(query->r_eventid, row[2]); zbx_vector_uint64_create(&query->functionids); zbx_vector_ptr_create(&query->tags); zbx_vector_uint64_pair_create(&query->maintenances); zbx_vector_ptr_append(event_queries, query); } DBfree_result(result); /* get event suppress data */ zbx_vector_uint64_create(&eventids); result = DBselect("select eventid,maintenanceid,suppress_until" " from event_suppress" " where " ZBX_SQL_MOD(eventid, %d) "=%d" " order by eventid", CONFIG_TIMER_FORKS, process_num - 1); while (NULL != (row = DBfetch(result))) { ZBX_STR2UINT64(eventid, row[0]); if (FAIL == zbx_vector_ptr_bsearch(event_queries, &eventid, ZBX_DEFAULT_UINT64_PTR_COMPARE_FUNC)) zbx_vector_uint64_append(&eventids, eventid); if (NULL == data || data->eventid != eventid) { data = (zbx_event_suppress_data_t *)zbx_malloc(NULL, sizeof(zbx_event_suppress_data_t)); data->eventid = eventid; zbx_vector_uint64_pair_create(&data->maintenances); zbx_vector_ptr_append(event_data, data); } ZBX_DBROW2UINT64(pair.first, row[1]); pair.second = atoi(row[2]); zbx_vector_uint64_pair_append(&data->maintenances, pair); } DBfree_result(result); /* get missing event data */ if (0 != eventids.values_num) { char *sql = NULL; size_t sql_alloc = 0, sql_offset = 0; zbx_vector_uint64_uniq(&eventids, ZBX_DEFAULT_UINT64_COMPARE_FUNC); zbx_strcpy_alloc(&sql, &sql_alloc, &sql_offset, "select e.eventid,e.objectid,er.r_eventid" " from events e" " left join event_recovery er" " on e.eventid=er.eventid" " where"); DBadd_condition_alloc(&sql, &sql_alloc, &sql_offset, "e.eventid", eventids.values, eventids.values_num); result = DBselect("%s", sql); zbx_free(sql); while (NULL != (row = DBfetch(result))) { query = (zbx_event_suppress_query_t *)zbx_malloc(NULL, sizeof(zbx_event_suppress_query_t)); ZBX_STR2UINT64(query->eventid, row[0]); ZBX_STR2UINT64(query->triggerid, row[1]); ZBX_DBROW2UINT64(query->r_eventid, row[2]); zbx_vector_uint64_create(&query->functionids); zbx_vector_ptr_create(&query->tags); zbx_vector_uint64_pair_create(&query->maintenances); zbx_vector_ptr_append(event_queries, query); } DBfree_result(result); zbx_vector_ptr_sort(event_queries, ZBX_DEFAULT_UINT64_PTR_COMPARE_FUNC); } zbx_vector_uint64_destroy(&eventids); }
/****************************************************************************** * * * Function: check_auto_registration_condition * * * * Purpose: check if event matches single condition * * * * Parameters: event - auto registration event to check * * (event->source == EVENT_SOURCE_AUTO_REGISTRATION) * * condition - condition for matching * * * * Return value: SUCCEED - matches, FAIL - otherwise * * * * Author: Alexei Vladishev * * * ******************************************************************************/ static int check_auto_registration_condition(DB_EVENT *event, DB_CONDITION *condition) { const char *__function_name = "check_auto_registration_condition"; DB_RESULT result; DB_ROW row; zbx_uint64_t condition_value, id; int ret = FAIL; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); if (CONDITION_TYPE_HOST_NAME == condition->conditiontype) { result = DBselect( "select host" " from autoreg_host" " where autoreg_hostid=" ZBX_FS_UI64, event->objectid); if (NULL != (row = DBfetch(result))) { switch (condition->operator) { case CONDITION_OPERATOR_LIKE: if (NULL != strstr(row[0], condition->value)) ret = SUCCEED; break; case CONDITION_OPERATOR_NOT_LIKE: if (NULL == strstr(row[0], condition->value)) ret = SUCCEED; break; default: ret = NOTSUPPORTED; break; } } DBfree_result(result); } else if (CONDITION_TYPE_PROXY == condition->conditiontype) { ZBX_STR2UINT64(condition_value, condition->value); result = DBselect( "select proxy_hostid" " from autoreg_host" " where autoreg_hostid=" ZBX_FS_UI64, event->objectid); if (NULL != (row = DBfetch(result))) { ZBX_DBROW2UINT64(id, row[0]); switch (condition->operator) { case CONDITION_OPERATOR_EQUAL: if (id == condition_value) ret = SUCCEED; break; case CONDITION_OPERATOR_NOT_EQUAL: if (id != condition_value) ret = SUCCEED; break; default: ret = NOTSUPPORTED; break; } } DBfree_result(result); } else { zabbix_log(LOG_LEVEL_ERR, "unsupported condition type [%d] for condition id [" ZBX_FS_UI64 "]", condition->conditiontype, condition->conditionid); } if (NOTSUPPORTED == ret) { zabbix_log(LOG_LEVEL_ERR, "unsupported operator [%d] for condition id [" ZBX_FS_UI64 "]", condition->operator, condition->conditionid); ret = FAIL; } zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; }
/****************************************************************************** * * * Function: check_trigger_condition * * * * Purpose: check if event matches single condition * * * * Parameters: event - trigger event to check * * (event->source == EVENT_SOURCE_TRIGGERS) * * condition - condition for matching * * * * Return value: SUCCEED - matches, FAIL - otherwise * * * * Author: Alexei Vladishev * * * ******************************************************************************/ static int check_trigger_condition(DB_EVENT *event, DB_CONDITION *condition) { const char *__function_name = "check_trigger_condition"; DB_RESULT result; DB_ROW row; zbx_uint64_t condition_value; int nodeid; char *tmp_str = NULL; int ret = FAIL; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); if (CONDITION_TYPE_HOST_GROUP == condition->conditiontype) { ZBX_STR2UINT64(condition_value, condition->value); result = DBselect( "select distinct hg.groupid" " from hosts_groups hg,hosts h,items i,functions f,triggers t" " where hg.hostid=h.hostid" " and h.hostid=i.hostid" " and i.itemid=f.itemid" " and f.triggerid=t.triggerid" " and t.triggerid=" ZBX_FS_UI64 " and hg.groupid=" ZBX_FS_UI64, event->objectid, condition_value); switch (condition->operator) { case CONDITION_OPERATOR_EQUAL: if (NULL != DBfetch(result)) ret = SUCCEED; break; case CONDITION_OPERATOR_NOT_EQUAL: if (NULL == DBfetch(result)) ret = SUCCEED; break; default: ret = NOTSUPPORTED; break; } DBfree_result(result); } else if (CONDITION_TYPE_HOST_TEMPLATE == condition->conditiontype) { zbx_uint64_t hostid, triggerid; ZBX_STR2UINT64(condition_value, condition->value); switch (condition->operator) { case CONDITION_OPERATOR_EQUAL: case CONDITION_OPERATOR_NOT_EQUAL: triggerid = event->objectid; /* use parent trigger ID for generated triggers */ result = DBselect( "select parent_triggerid" " from trigger_discovery" " where triggerid=" ZBX_FS_UI64, triggerid); if (NULL != (row = DBfetch(result))) { ZBX_STR2UINT64(triggerid, row[0]); zabbix_log(LOG_LEVEL_DEBUG, "%s() check host template condition," " selecting parent triggerid:" ZBX_FS_UI64, __function_name, triggerid); } DBfree_result(result); do { result = DBselect( "select distinct i.hostid,t.templateid" " from items i,functions f,triggers t" " where i.itemid=f.itemid" " and f.triggerid=t.templateid" " and t.triggerid=" ZBX_FS_UI64, triggerid); if (NULL != (row = DBfetch(result))) { ZBX_STR2UINT64(hostid, row[0]); ZBX_STR2UINT64(triggerid, row[1]); if (hostid == condition_value) { ret = SUCCEED; break; } } else triggerid = 0; DBfree_result(result); } while (SUCCEED != ret && 0 != triggerid); if (CONDITION_OPERATOR_NOT_EQUAL == condition->operator) ret = (SUCCEED == ret) ? FAIL : SUCCEED; break; default: ret = NOTSUPPORTED; break; } } else if (CONDITION_TYPE_HOST == condition->conditiontype) { ZBX_STR2UINT64(condition_value, condition->value); switch (condition->operator) { case CONDITION_OPERATOR_EQUAL: case CONDITION_OPERATOR_NOT_EQUAL: result = DBselect( "select distinct i.hostid" " from items i,functions f,triggers t" " where i.itemid=f.itemid" " and f.triggerid=t.triggerid" " and t.triggerid=" ZBX_FS_UI64 " and i.hostid=" ZBX_FS_UI64, event->objectid, condition_value); if (NULL != DBfetch(result)) ret = SUCCEED; DBfree_result(result); if (CONDITION_OPERATOR_NOT_EQUAL == condition->operator) ret = (SUCCEED == ret) ? FAIL : SUCCEED; break; default: ret = NOTSUPPORTED; break; } } else if (CONDITION_TYPE_TRIGGER == condition->conditiontype) { zbx_uint64_t triggerid; ZBX_STR2UINT64(condition_value, condition->value); switch (condition->operator) { case CONDITION_OPERATOR_EQUAL: case CONDITION_OPERATOR_NOT_EQUAL: if (event->objectid == condition_value) { ret = SUCCEED; } else { /* processing of templated triggers */ for (triggerid = event->objectid; 0 != triggerid && FAIL == ret;) { result = DBselect( "select templateid" " from triggers" " where triggerid=" ZBX_FS_UI64, triggerid); if (NULL == (row = DBfetch(result))) triggerid = 0; else { ZBX_DBROW2UINT64(triggerid, row[0]); if (triggerid == condition_value) ret = SUCCEED; } DBfree_result(result); } } if (CONDITION_OPERATOR_NOT_EQUAL == condition->operator) ret = (SUCCEED == ret) ? FAIL : SUCCEED; break; default: ret = NOTSUPPORTED; break; } } else if (CONDITION_TYPE_TRIGGER_NAME == condition->conditiontype) { tmp_str = zbx_strdup(tmp_str, event->trigger.description); substitute_simple_macros(event, NULL, NULL, NULL, &tmp_str, MACRO_TYPE_TRIGGER_DESCRIPTION, NULL, 0); switch (condition->operator) { case CONDITION_OPERATOR_LIKE: if (NULL != strstr(tmp_str, condition->value)) ret = SUCCEED; break; case CONDITION_OPERATOR_NOT_LIKE: if (NULL == strstr(tmp_str, condition->value)) ret = SUCCEED; break; default: ret = NOTSUPPORTED; break; } zbx_free(tmp_str); } else if (CONDITION_TYPE_TRIGGER_SEVERITY == condition->conditiontype) { condition_value = atoi(condition->value); switch (condition->operator) { case CONDITION_OPERATOR_EQUAL: if (event->trigger.priority == condition_value) ret = SUCCEED; break; case CONDITION_OPERATOR_NOT_EQUAL: if (event->trigger.priority != condition_value) ret = SUCCEED; break; case CONDITION_OPERATOR_MORE_EQUAL: if (event->trigger.priority >= condition_value) ret = SUCCEED; break; case CONDITION_OPERATOR_LESS_EQUAL: if (event->trigger.priority <= condition_value) ret = SUCCEED; break; default: ret = NOTSUPPORTED; break; } } else if (CONDITION_TYPE_TRIGGER_VALUE == condition->conditiontype) { condition_value = atoi(condition->value); switch (condition->operator) { case CONDITION_OPERATOR_EQUAL: if (event->value == condition_value) ret = SUCCEED; break; default: ret = NOTSUPPORTED; break; } } else if (CONDITION_TYPE_TIME_PERIOD == condition->conditiontype) { switch (condition->operator) { case CONDITION_OPERATOR_IN: if (SUCCEED == check_time_period(condition->value, (time_t)NULL)) ret = SUCCEED; break; case CONDITION_OPERATOR_NOT_IN: if (FAIL == check_time_period(condition->value, (time_t)NULL)) ret = SUCCEED; break; default: ret = NOTSUPPORTED; break; } } else if (CONDITION_TYPE_MAINTENANCE == condition->conditiontype) { switch (condition->operator) { case CONDITION_OPERATOR_IN: result = DBselect( "select count(*)" " from hosts h,items i,functions f,triggers t" " where h.hostid=i.hostid" " and h.maintenance_status=%d" " and i.itemid=f.itemid" " and f.triggerid=t.triggerid" " and t.triggerid=" ZBX_FS_UI64, HOST_MAINTENANCE_STATUS_ON, event->objectid); if (NULL != (row = DBfetch(result)) && FAIL == DBis_null(row[0]) && 0 != atoi(row[0])) ret = SUCCEED; DBfree_result(result); break; case CONDITION_OPERATOR_NOT_IN: result = DBselect( "select count(*)" " from hosts h,items i,functions f,triggers t" " where h.hostid=i.hostid" " and h.maintenance_status=%d" " and i.itemid=f.itemid" " and f.triggerid=t.triggerid" " and t.triggerid=" ZBX_FS_UI64, HOST_MAINTENANCE_STATUS_OFF, event->objectid); if (NULL != (row = DBfetch(result)) && FAIL == DBis_null(row[0]) && 0 != atoi(row[0])) ret = SUCCEED; DBfree_result(result); break; default: ret = NOTSUPPORTED; break; } } else if (CONDITION_TYPE_NODE == condition->conditiontype) { nodeid = get_nodeid_by_id(event->objectid); condition_value = atoi(condition->value); switch (condition->operator) { case CONDITION_OPERATOR_EQUAL: if (nodeid == condition_value) ret = SUCCEED; break; case CONDITION_OPERATOR_NOT_EQUAL: if (nodeid != condition_value) ret = SUCCEED; break; default: ret = NOTSUPPORTED; break; } } else if (CONDITION_TYPE_EVENT_ACKNOWLEDGED == condition->conditiontype) { result = DBselect( "select acknowledged" " from events" " where acknowledged=%d" " and eventid=" ZBX_FS_UI64, atoi(condition->value), event->eventid); switch (condition->operator) { case CONDITION_OPERATOR_EQUAL: if (NULL != (row = DBfetch(result))) ret = SUCCEED; break; default: ret = NOTSUPPORTED; break; } DBfree_result(result); } else if (CONDITION_TYPE_APPLICATION == condition->conditiontype) { result = DBselect( "select distinct a.name" " from applications a,items_applications i,functions f,triggers t" " where a.applicationid=i.applicationid" " and i.itemid=f.itemid" " and f.triggerid=t.triggerid" " and t.triggerid=" ZBX_FS_UI64, event->objectid); switch (condition->operator) { case CONDITION_OPERATOR_EQUAL: while (NULL != (row = DBfetch(result))) { if (0 == strcmp(row[0], condition->value)) { ret = SUCCEED; break; } } break; case CONDITION_OPERATOR_LIKE: while (NULL != (row = DBfetch(result))) { if (NULL != strstr(row[0], condition->value)) { ret = SUCCEED; break; } } break; case CONDITION_OPERATOR_NOT_LIKE: ret = SUCCEED; while (NULL != (row = DBfetch(result))) { if (NULL != strstr(row[0], condition->value)) { ret = FAIL; break; } } break; default: ret = NOTSUPPORTED; break; } DBfree_result(result); } else { zabbix_log(LOG_LEVEL_ERR, "unsupported condition type [%d] for condition id [" ZBX_FS_UI64 "]", condition->conditiontype, condition->conditionid); } if (NOTSUPPORTED == ret) { zabbix_log(LOG_LEVEL_ERR, "unsupported operator [%d] for condition id [" ZBX_FS_UI64 "]", condition->operator, condition->conditionid); ret = FAIL; } zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; }
/****************************************************************************** * * * Function: execute_operations * * * * Purpose: execute all operations linked to the action * * * * Parameters: action - action to execute operations for * * * * Author: Alexei Vladishev * * * ******************************************************************************/ static void execute_operations(DB_EVENT *event, zbx_uint64_t actionid) { const char *__function_name = "execute_operations"; DB_RESULT result; DB_ROW row; unsigned char operationtype; zbx_uint64_t groupid, templateid; zbx_vector_uint64_t lnk_templateids, del_templateids, new_groupids, del_groupids; zabbix_log(LOG_LEVEL_DEBUG, "In %s() actionid:" ZBX_FS_UI64, __function_name, actionid); zbx_vector_uint64_create(&lnk_templateids); zbx_vector_uint64_create(&del_templateids); zbx_vector_uint64_create(&new_groupids); zbx_vector_uint64_create(&del_groupids); result = DBselect( "select o.operationtype,g.groupid,t.templateid" " from operations o" " left join opgroup g on g.operationid=o.operationid" " left join optemplate t on t.operationid=o.operationid" " where o.actionid=" ZBX_FS_UI64, actionid); while (NULL != (row = DBfetch(result))) { operationtype = (unsigned char)atoi(row[0]); ZBX_DBROW2UINT64(groupid, row[1]); ZBX_DBROW2UINT64(templateid, row[2]); switch (operationtype) { case OPERATION_TYPE_HOST_ADD: op_host_add(event); break; case OPERATION_TYPE_HOST_REMOVE: op_host_del(event); break; case OPERATION_TYPE_HOST_ENABLE: op_host_enable(event); break; case OPERATION_TYPE_HOST_DISABLE: op_host_disable(event); break; case OPERATION_TYPE_GROUP_ADD: if (0 != groupid) zbx_vector_uint64_append(&new_groupids, groupid); break; case OPERATION_TYPE_GROUP_REMOVE: if (0 != groupid) zbx_vector_uint64_append(&del_groupids, groupid); break; case OPERATION_TYPE_TEMPLATE_ADD: if (0 != templateid) zbx_vector_uint64_append(&lnk_templateids, templateid); break; case OPERATION_TYPE_TEMPLATE_REMOVE: if (0 != templateid) zbx_vector_uint64_append(&del_templateids, templateid); break; default: ; } } DBfree_result(result); if (0 != lnk_templateids.values_num) { zbx_vector_uint64_sort(&lnk_templateids, ZBX_DEFAULT_UINT64_COMPARE_FUNC); op_template_add(event, &lnk_templateids); } if (0 != del_templateids.values_num) { zbx_vector_uint64_sort(&del_templateids, ZBX_DEFAULT_UINT64_COMPARE_FUNC); op_template_del(event, &del_templateids); } if (0 != new_groupids.values_num) { zbx_vector_uint64_sort(&new_groupids, ZBX_DEFAULT_UINT64_COMPARE_FUNC); op_groups_add(event, &new_groupids); } if (0 != del_groupids.values_num) { zbx_vector_uint64_sort(&del_groupids, ZBX_DEFAULT_UINT64_COMPARE_FUNC); op_groups_del(event, &del_groupids); } zbx_vector_uint64_destroy(&del_groupids); zbx_vector_uint64_destroy(&new_groupids); zbx_vector_uint64_destroy(&del_templateids); zbx_vector_uint64_destroy(&lnk_templateids); zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }