/****************************************************************************** * * * Function: lld_items_get * * * * Purpose: returns the list of items which are related to the trigger * * prototype * * * * Parameters: parent_triggerid - [IN] trigger prototype identificator * * items - [OUT] sorted list of items * * * ******************************************************************************/ static void lld_items_get(zbx_uint64_t parent_triggerid, zbx_vector_ptr_t *items) { const char *__function_name = "lld_items_get"; DB_RESULT result; DB_ROW row; zbx_lld_item_t *item; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); result = DBselect( "select distinct i.itemid,i.flags" " from items i,functions f" " where i.itemid=f.itemid" " and f.triggerid=" ZBX_FS_UI64, parent_triggerid); while (NULL != (row = DBfetch(result))) { item = zbx_malloc(NULL, sizeof(zbx_lld_item_t)); ZBX_STR2UINT64(item->itemid, row[0]); ZBX_STR2UCHAR(item->flags, row[1]); zbx_vector_ptr_append(items, item); } DBfree_result(result); zbx_vector_ptr_sort(items, ZBX_DEFAULT_UINT64_PTR_COMPARE_FUNC); zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
/****************************************************************************** * * * Function: generate_events * * * * Purpose: Generate events for triggers after maintenance period. Events * * will be generated if trigger value changed during maintenance. * * * * Parameters: hostid - host identifier from database * * maintenance_from, maintenance_to - maintenance period bounds * * * ******************************************************************************/ static void generate_events(zbx_uint64_t hostid, int maintenance_from, int maintenance_to) { const char *__function_name = "generate_events"; DB_RESULT result; DB_ROW row; zbx_uint64_t triggerid; zbx_timespec_t ts; unsigned char value_before, value_inside, value_after; ts.sec = maintenance_to; ts.ns = 0; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); result = DBselect( "select distinct t.triggerid,t.description,t.expression,t.priority,t.type,t.lastchange,t.value" " from triggers t,functions f,items i" " where t.triggerid=f.triggerid" " and f.itemid=i.itemid" " and t.status=%d" " and i.status=%d" " and i.state=%d" " and i.hostid=" ZBX_FS_UI64, TRIGGER_STATUS_ENABLED, ITEM_STATUS_ACTIVE, ITEM_STATE_NORMAL, hostid); while (NULL != (row = DBfetch(result))) { if (atoi(row[5]) < maintenance_from) /* if no events inside maintenance */ continue; ZBX_STR2UINT64(triggerid, row[0]); ZBX_STR2UCHAR(value_after, row[6]); get_trigger_values(triggerid, maintenance_from, &value_before, &value_inside, value_after); if (value_before == value_inside && value_inside == value_after) continue; add_event(0, EVENT_SOURCE_TRIGGERS, EVENT_OBJECT_TRIGGER, triggerid, &ts, value_after, row[1], row[2], (unsigned char)atoi(row[3]), (unsigned char)atoi(row[4])); } DBfree_result(result); process_events(); zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
/****************************************************************************** * * * Function: tm_process_tasks * * * * Purpose: process task manager tasks depending on task type * * * * Return value: The number of successfully processed tasks * * * ******************************************************************************/ static int tm_process_tasks(int now) { DB_ROW row; DB_RESULT result; int processed_num = 0, clock, ttl; zbx_uint64_t taskid; unsigned char type; zbx_vector_uint64_t check_now_taskids; zbx_vector_uint64_create(&check_now_taskids); result = DBselect("select taskid,type,clock,ttl" " from task" " where status=%d" " and type in (%d, %d)" " order by taskid", ZBX_TM_STATUS_NEW, ZBX_TM_TASK_REMOTE_COMMAND, ZBX_TM_TASK_CHECK_NOW); while (NULL != (row = DBfetch(result))) { ZBX_STR2UINT64(taskid, row[0]); ZBX_STR2UCHAR(type, row[1]); clock = atoi(row[2]); ttl = atoi(row[3]); switch (type) { case ZBX_TM_TASK_REMOTE_COMMAND: if (SUCCEED == tm_execute_remote_command(taskid, clock, ttl, now)) processed_num++; break; case ZBX_TM_TASK_CHECK_NOW: zbx_vector_uint64_append(&check_now_taskids, taskid); break; default: THIS_SHOULD_NEVER_HAPPEN; break; } } DBfree_result(result); if (0 < check_now_taskids.values_num) processed_num += tm_process_check_now(&check_now_taskids); zbx_vector_uint64_destroy(&check_now_taskids); return processed_num; }
/* auxiliary function for DBcopy_template_items() */ static void DBget_interfaces_by_hostid(zbx_uint64_t hostid, zbx_uint64_t *interfaceids) { DB_RESULT result; DB_ROW row; unsigned char type; result = DBselect( "select type,interfaceid" " from interface" " where hostid=" ZBX_FS_UI64 " and type in (%d,%d,%d,%d)" " and main=1", hostid, INTERFACE_TYPE_AGENT, INTERFACE_TYPE_SNMP, INTERFACE_TYPE_IPMI, INTERFACE_TYPE_JMX); while (NULL != (row = DBfetch(result))) { ZBX_STR2UCHAR(type, row[0]); ZBX_STR2UINT64(interfaceids[type - 1], row[1]); } DBfree_result(result); }
/****************************************************************************** * * * Function: get_template_lld_rule_map * * * * Purpose: reads template lld rule conditions and host lld_rule identifiers * * from database * * * * Parameters: items - [IN] the host items including lld rules * * rules - [OUT] the ldd rule mapping * * * ******************************************************************************/ static void get_template_lld_rule_map(const zbx_vector_ptr_t *items, zbx_vector_ptr_t *rules) { zbx_template_item_t *item; zbx_lld_rule_map_t *rule; zbx_lld_rule_condition_t *condition; int i, index; zbx_vector_uint64_t itemids; DB_RESULT result; DB_ROW row; char *sql = NULL; size_t sql_alloc = 0, sql_offset = 0; zbx_uint64_t itemid, item_conditionid; zbx_vector_uint64_create(&itemids); /* prepare discovery rules */ for (i = 0; i < items->values_num; i++) { item = items->values[i]; if (0 == (ZBX_FLAG_DISCOVERY_RULE & item->flags)) continue; rule = zbx_malloc(NULL, sizeof(zbx_lld_rule_map_t)); rule->itemid = item->itemid; rule->templateid = item->templateid; rule->conditionid = 0; zbx_vector_uint64_create(&rule->conditionids); zbx_vector_ptr_create(&rule->conditions); zbx_vector_ptr_append(rules, rule); if (0 != rule->itemid) zbx_vector_uint64_append(&itemids, rule->itemid); zbx_vector_uint64_append(&itemids, rule->templateid); } if (0 != itemids.values_num) { zbx_vector_ptr_sort(rules, ZBX_DEFAULT_UINT64_PTR_COMPARE_FUNC); zbx_vector_uint64_sort(&itemids, ZBX_DEFAULT_UINT64_COMPARE_FUNC); zbx_strcpy_alloc(&sql, &sql_alloc, &sql_offset, "select item_conditionid,itemid,operator,macro,value from item_condition where"); DBadd_condition_alloc(&sql, &sql_alloc, &sql_offset, "itemid", itemids.values, itemids.values_num); result = DBselect("%s", sql); while (NULL != (row = DBfetch(result))) { ZBX_STR2UINT64(itemid, row[1]); index = zbx_vector_ptr_bsearch(rules, &itemid, ZBX_DEFAULT_UINT64_PTR_COMPARE_FUNC); if (FAIL != index) { /* read template lld conditions */ rule = (zbx_lld_rule_map_t *)rules->values[index]; condition = zbx_malloc(NULL, sizeof(zbx_lld_rule_condition_t)); ZBX_STR2UINT64(condition->item_conditionid, row[0]); ZBX_STR2UCHAR(condition->operator, row[2]); condition->macro = zbx_strdup(NULL, row[3]); condition->value = zbx_strdup(NULL, row[4]); zbx_vector_ptr_append(&rule->conditions, condition); } else { /* read host lld conditions identifiers */ for (i = 0; i < rules->values_num; i++) { rule = (zbx_lld_rule_map_t *)rules->values[i]; if (itemid != rule->itemid) continue; ZBX_STR2UINT64(item_conditionid, row[0]); zbx_vector_uint64_append(&rule->conditionids, item_conditionid); break; } if (i == rules->values_num) THIS_SHOULD_NEVER_HAPPEN; } } DBfree_result(result); zbx_free(sql); } zbx_vector_uint64_destroy(&itemids); }
/****************************************************************************** * * * Function: get_template_items * * * * Purpose: read template items from database * * * * Parameters: hostid - [IN] host id * * templateids - [IN] array of template IDs * * items - [OUT] the item data * * * * Comments: The itemid and key are set depending on whether the item exists * * for the specified host. * * If item exists itemid will be set to its itemid and key will be * * set to NULL. * * If item does not exist, itemid will be set to 0 and key will be * * set to item key. * * * ******************************************************************************/ static void get_template_items(zbx_uint64_t hostid, const zbx_vector_uint64_t *templateids, zbx_vector_ptr_t *items) { DB_RESULT result; DB_ROW row; char *sql = NULL; size_t sql_alloc = 0, sql_offset = 0, i; unsigned char interface_type; zbx_template_item_t *item; zbx_uint64_t interfaceids[4]; memset(&interfaceids, 0, sizeof(interfaceids)); DBget_interfaces_by_hostid(hostid, interfaceids); zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset, "select ti.itemid,ti.name,ti.key_,ti.type,ti.value_type,ti.data_type,ti.delay,ti.delay_flex," "ti.history,ti.trends,ti.status,ti.trapper_hosts,ti.units,ti.multiplier,ti.delta," "ti.formula,ti.logtimefmt,ti.valuemapid,ti.params,ti.ipmi_sensor,ti.snmp_community," "ti.snmp_oid,ti.snmpv3_securityname,ti.snmpv3_securitylevel,ti.snmpv3_authprotocol," "ti.snmpv3_authpassphrase,ti.snmpv3_privprotocol,ti.snmpv3_privpassphrase,ti.authtype," "ti.username,ti.password,ti.publickey,ti.privatekey,ti.flags,ti.description," "ti.inventory_link,ti.lifetime,ti.snmpv3_contextname,hi.itemid,ti.evaltype,ti.port" " from items ti" " left join items hi on hi.key_=ti.key_" " and hi.hostid=" ZBX_FS_UI64 " where", hostid); DBadd_condition_alloc(&sql, &sql_alloc, &sql_offset, "ti.hostid", templateids->values, templateids->values_num); result = DBselect("%s", sql); while (NULL != (row = DBfetch(result))) { item = zbx_malloc(NULL, sizeof(zbx_template_item_t)); ZBX_STR2UINT64(item->templateid, row[0]); ZBX_STR2UCHAR(item->type, row[3]); ZBX_STR2UCHAR(item->value_type, row[4]); ZBX_STR2UCHAR(item->data_type, row[5]); item->delay = atoi(row[6]); item->history = atoi(row[8]); item->trends = atoi(row[9]); ZBX_STR2UCHAR(item->status, row[10]); ZBX_STR2UCHAR(item->multiplier, row[13]); ZBX_STR2UCHAR(item->delta, row[14]); ZBX_DBROW2UINT64(item->valuemapid, row[17]); ZBX_STR2UCHAR(item->snmpv3_securitylevel, row[23]); ZBX_STR2UCHAR(item->snmpv3_authprotocol, row[24]); ZBX_STR2UCHAR(item->snmpv3_privprotocol, row[26]); ZBX_STR2UCHAR(item->authtype, row[28]); ZBX_STR2UCHAR(item->flags, row[33]); ZBX_STR2UCHAR(item->inventory_link, row[35]); ZBX_STR2UCHAR(item->evaltype, row[39]); switch (interface_type = get_interface_type_by_item_type(item->type)) { case INTERFACE_TYPE_UNKNOWN: item->interfaceid = 0; break; case INTERFACE_TYPE_ANY: for (i = 0; INTERFACE_TYPE_COUNT > i; i++) { if (0 != interfaceids[INTERFACE_TYPE_PRIORITY[i] - 1]) break; } item->interfaceid = interfaceids[INTERFACE_TYPE_PRIORITY[i] - 1]; break; default: item->interfaceid = interfaceids[interface_type - 1]; } item->name = zbx_strdup(NULL, row[1]); item->delay_flex = zbx_strdup(NULL, row[7]); item->trapper_hosts = zbx_strdup(NULL, row[11]); item->units = zbx_strdup(NULL, row[12]); item->formula = zbx_strdup(NULL, row[15]); item->logtimefmt = zbx_strdup(NULL, row[16]); item->params = zbx_strdup(NULL, row[18]); item->ipmi_sensor = zbx_strdup(NULL, row[19]); item->snmp_community = zbx_strdup(NULL, row[20]); item->snmp_oid = zbx_strdup(NULL, row[21]); item->snmpv3_securityname = zbx_strdup(NULL, row[22]); item->snmpv3_authpassphrase = zbx_strdup(NULL, row[25]); item->snmpv3_privpassphrase = zbx_strdup(NULL, row[27]); item->username = zbx_strdup(NULL, row[29]); item->password = zbx_strdup(NULL, row[30]); item->publickey = zbx_strdup(NULL, row[31]); item->privatekey = zbx_strdup(NULL, row[32]); item->description = zbx_strdup(NULL, row[34]); item->lifetime = zbx_strdup(NULL, row[36]); item->snmpv3_contextname = zbx_strdup(NULL, row[37]); item->port = zbx_strdup(NULL, row[40]); if (SUCCEED != DBis_null(row[38])) { item->key = NULL; ZBX_STR2UINT64(item->itemid, row[38]); } else { item->key = zbx_strdup(NULL, row[2]); item->itemid = 0; } zbx_vector_ptr_append(items, item); } DBfree_result(result); zbx_free(sql); zbx_vector_ptr_sort(items, ZBX_DEFAULT_UINT64_PTR_COMPARE_FUNC); }
/****************************************************************************** * * * Function: lld_items_get * * * * Purpose: returns the list of items which are related to the graph * * prototype * * * * Parameters: gitems_proto - [IN] graph prototype's graphs_items * * ymin_itemid_proto - [IN] graph prototype's ymin_itemid * * ymax_itemid_proto - [IN] graph prototype's ymax_itemid * * items - [OUT] sorted list of items * * * ******************************************************************************/ static void lld_items_get(zbx_vector_ptr_t *gitems_proto, zbx_uint64_t ymin_itemid_proto, zbx_uint64_t ymax_itemid_proto, zbx_vector_ptr_t *items) { const char *__function_name = "lld_items_get"; DB_RESULT result; DB_ROW row; zbx_lld_gitem_t *gitem; zbx_lld_item_t *item; zbx_vector_uint64_t itemids; int i; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); zbx_vector_uint64_create(&itemids); for (i = 0; i < gitems_proto->values_num; i++) { gitem = (zbx_lld_gitem_t *)gitems_proto->values[i]; zbx_vector_uint64_append(&itemids, gitem->itemid); } if (0 != ymin_itemid_proto) zbx_vector_uint64_append(&itemids, ymin_itemid_proto); if (0 != ymax_itemid_proto) zbx_vector_uint64_append(&itemids, ymax_itemid_proto); if (0 != itemids.values_num) { char *sql = NULL; size_t sql_alloc = 256, sql_offset = 0; zbx_vector_uint64_sort(&itemids, ZBX_DEFAULT_UINT64_COMPARE_FUNC); sql = zbx_malloc(sql, sql_alloc); zbx_strcpy_alloc(&sql, &sql_alloc, &sql_offset, "select itemid,flags" " from items" " where"); DBadd_condition_alloc(&sql, &sql_alloc, &sql_offset, "itemid", itemids.values, itemids.values_num); result = DBselect("%s", sql); zbx_free(sql); while (NULL != (row = DBfetch(result))) { item = zbx_malloc(NULL, sizeof(zbx_lld_item_t)); ZBX_STR2UINT64(item->itemid, row[0]); ZBX_STR2UCHAR(item->flags, row[1]); zbx_vector_ptr_append(items, item); } DBfree_result(result); zbx_vector_ptr_sort(items, ZBX_DEFAULT_UINT64_PTR_COMPARE_FUNC); } zbx_vector_uint64_destroy(&itemids); zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
/****************************************************************************** * * * Function: lld_gitems_get * * * * Purpose: retrieve graphs_items which are used by the graph prototype and * * by selected graphs * * * ******************************************************************************/ static void lld_gitems_get(zbx_uint64_t parent_graphid, zbx_vector_ptr_t *gitems_proto, zbx_vector_ptr_t *graphs) { const char *__function_name = "lld_gitems_get"; int i, index; zbx_lld_graph_t *graph; zbx_lld_gitem_t *gitem; zbx_uint64_t graphid; zbx_vector_uint64_t graphids; DB_RESULT result; DB_ROW row; char *sql = NULL; size_t sql_alloc = 256, sql_offset = 0; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); zbx_vector_uint64_create(&graphids); zbx_vector_uint64_append(&graphids, parent_graphid); for (i = 0; i < graphs->values_num; i++) { graph = (zbx_lld_graph_t *)graphs->values[i]; zbx_vector_uint64_append(&graphids, graph->graphid); } zbx_vector_uint64_sort(&graphids, ZBX_DEFAULT_UINT64_COMPARE_FUNC); sql = zbx_malloc(sql, sql_alloc); zbx_strcpy_alloc(&sql, &sql_alloc, &sql_offset, "select gitemid,graphid,itemid,drawtype,sortorder,color,yaxisside,calc_fnc,type" " from graphs_items" " where"); DBadd_condition_alloc(&sql, &sql_alloc, &sql_offset, "graphid", graphids.values, graphids.values_num); result = DBselect("%s", sql); zbx_free(sql); while (NULL != (row = DBfetch(result))) { gitem = zbx_malloc(NULL, sizeof(zbx_lld_gitem_t)); ZBX_STR2UINT64(gitem->gitemid, row[0]); ZBX_STR2UINT64(graphid, row[1]); ZBX_STR2UINT64(gitem->itemid, row[2]); ZBX_STR2UCHAR(gitem->drawtype, row[3]); gitem->sortorder = atoi(row[4]); gitem->color = zbx_strdup(NULL, row[5]); ZBX_STR2UCHAR(gitem->yaxisside, row[6]); ZBX_STR2UCHAR(gitem->calc_fnc, row[7]); ZBX_STR2UCHAR(gitem->type, row[8]); gitem->flags = ZBX_FLAG_LLD_GITEM_UNSET; if (graphid == parent_graphid) { zbx_vector_ptr_append(gitems_proto, gitem); } else if (FAIL != (index = zbx_vector_ptr_bsearch(graphs, &graphid, ZBX_DEFAULT_UINT64_PTR_COMPARE_FUNC))) { graph = (zbx_lld_graph_t *)graphs->values[index]; zbx_vector_ptr_append(&graph->gitems, gitem); } else { THIS_SHOULD_NEVER_HAPPEN; lld_gitem_free(gitem); } } DBfree_result(result); zbx_vector_ptr_sort(gitems_proto, ZBX_DEFAULT_UINT64_PTR_COMPARE_FUNC); for (i = 0; i < graphs->values_num; i++) { graph = (zbx_lld_graph_t *)graphs->values[i]; zbx_vector_ptr_sort(&graph->gitems, ZBX_DEFAULT_UINT64_PTR_COMPARE_FUNC); } zbx_vector_uint64_destroy(&graphids); zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
/****************************************************************************** * * * Function: lld_update_graphs * * * * Purpose: add or update graphs for discovery item * * * * Parameters: hostid - [IN] host identificator from database * * agent - [IN] discovery item identificator from database * * jp_data - [IN] received data * * * * Author: Alexander Vladishev * * * ******************************************************************************/ void lld_update_graphs(zbx_uint64_t hostid, zbx_uint64_t lld_ruleid, zbx_vector_ptr_t *lld_rows, char **error) { const char *__function_name = "lld_update_graphs"; DB_RESULT result; DB_ROW row; zbx_vector_ptr_t graphs; zbx_vector_ptr_t gitems_proto; zbx_vector_ptr_t items; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); zbx_vector_ptr_create(&graphs); /* list of graphs which were created or will be created or */ /* updated by the graph prototype */ zbx_vector_ptr_create(&gitems_proto); /* list of graphs_items which are used by the graph prototype */ zbx_vector_ptr_create(&items); /* list of items which are related to the graph prototype */ result = DBselect( "select distinct g.graphid,g.name,g.width,g.height,g.yaxismin,g.yaxismax,g.show_work_period," "g.show_triggers,g.graphtype,g.show_legend,g.show_3d,g.percent_left,g.percent_right," "g.ymin_type,g.ymin_itemid,g.ymax_type,g.ymax_itemid" " from graphs g,graphs_items gi,items i,item_discovery id" " where g.graphid=gi.graphid" " and gi.itemid=i.itemid" " and i.itemid=id.itemid" " and id.parent_itemid=" ZBX_FS_UI64, lld_ruleid); while (NULL != (row = DBfetch(result))) { zbx_uint64_t parent_graphid, ymin_itemid_proto, ymax_itemid_proto; const char *name_proto; int width, height; double yaxismin, yaxismax, percent_left, percent_right; unsigned char show_work_period, show_triggers, graphtype, show_legend, show_3d, ymin_type, ymax_type; ZBX_STR2UINT64(parent_graphid, row[0]); name_proto = row[1]; width = atoi(row[2]); height = atoi(row[3]); yaxismin = atof(row[4]); yaxismax = atof(row[5]); ZBX_STR2UCHAR(show_work_period, row[6]); ZBX_STR2UCHAR(show_triggers, row[7]); ZBX_STR2UCHAR(graphtype, row[8]); ZBX_STR2UCHAR(show_legend, row[9]); ZBX_STR2UCHAR(show_3d, row[10]); percent_left = atof(row[11]); percent_right = atof(row[12]); ZBX_STR2UCHAR(ymin_type, row[13]); ZBX_DBROW2UINT64(ymin_itemid_proto, row[14]); ZBX_STR2UCHAR(ymax_type, row[15]); ZBX_DBROW2UINT64(ymax_itemid_proto, row[16]); lld_graphs_get(parent_graphid, &graphs, width, height, yaxismin, yaxismax, show_work_period, show_triggers, graphtype, show_legend, show_3d, percent_left, percent_right, ymin_type, ymax_type); lld_gitems_get(parent_graphid, &gitems_proto, &graphs); lld_items_get(&gitems_proto, ymin_itemid_proto, ymax_itemid_proto, &items); /* making graphs */ lld_graphs_make(&gitems_proto, &graphs, &items, name_proto, ymin_itemid_proto, ymax_itemid_proto, lld_rows); lld_graphs_validate(hostid, &graphs, error); lld_graphs_save(parent_graphid, &graphs, width, height, yaxismin, yaxismax, show_work_period, show_triggers, graphtype, show_legend, show_3d, percent_left, percent_right, ymin_type, ymax_type); lld_items_free(&items); lld_gitems_free(&gitems_proto); lld_graphs_free(&graphs); } DBfree_result(result); zbx_vector_ptr_destroy(&items); zbx_vector_ptr_destroy(&gitems_proto); zbx_vector_ptr_destroy(&graphs); zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
/****************************************************************************** * * * Function: tm_execute_remote_command * * * * Purpose: execute remote command task * * * * Parameters: taskid - [IN] the task identifier * * clock - [IN] the task creation time * * ttl - [IN] the task expiration period in seconds * * now - [IN] the current time * * * * Return value: SUCCEED - the remote command was executed * * FAIL - otherwise * * * ******************************************************************************/ static int tm_execute_remote_command(zbx_uint64_t taskid, int clock, int ttl, int now) { DB_ROW row; DB_RESULT result; zbx_uint64_t parent_taskid, hostid; zbx_tm_task_t *task = NULL; int ret = FAIL; zbx_script_t script; char *info = NULL, error[MAX_STRING_LEN]; DC_HOST host; result = DBselect("select command_type,execute_on,port,authtype,username,password,publickey,privatekey," "command,parent_taskid,hostid" " from task_remote_command" " where taskid=" ZBX_FS_UI64, taskid); if (NULL == (row = DBfetch(result))) goto finish; task = zbx_tm_task_create(0, ZBX_TM_TASK_REMOTE_COMMAND_RESULT, ZBX_TM_STATUS_NEW, time(NULL), 0, 0); ZBX_STR2UINT64(parent_taskid, row[9]); if (0 != ttl && clock + ttl < now) { task->data = zbx_tm_remote_command_result_create(parent_taskid, FAIL, "The remote command has been expired."); goto finish; } ZBX_STR2UINT64(hostid, row[10]); if (FAIL == DCget_host_by_hostid(&host, hostid)) { task->data = zbx_tm_remote_command_result_create(parent_taskid, FAIL, "Unknown host."); goto finish; } zbx_script_init(&script); ZBX_STR2UCHAR(script.type, row[0]); ZBX_STR2UCHAR(script.execute_on, row[1]); script.port = (0 == atoi(row[2]) ? (char *)"" : row[2]); ZBX_STR2UCHAR(script.authtype, row[3]); script.username = row[4]; script.password = row[5]; script.publickey = row[6]; script.privatekey = row[7]; script.command = row[8]; if (ZBX_SCRIPT_TYPE_CUSTOM_SCRIPT == script.type && ZBX_SCRIPT_EXECUTE_ON_PROXY == script.execute_on) { if (0 == CONFIG_ENABLE_REMOTE_COMMANDS) { task->data = zbx_tm_remote_command_result_create(parent_taskid, FAIL, "Remote commands are not enabled"); goto finish; } if (1 == CONFIG_LOG_REMOTE_COMMANDS) zabbix_log(LOG_LEVEL_WARNING, "Executing command '%s'", script.command); else zabbix_log(LOG_LEVEL_DEBUG, "Executing command '%s'", script.command); } if (SUCCEED != (ret = zbx_script_execute(&script, &host, &info, error, sizeof(error)))) task->data = zbx_tm_remote_command_result_create(parent_taskid, ret, error); else task->data = zbx_tm_remote_command_result_create(parent_taskid, ret, info); zbx_free(info); finish: DBfree_result(result); DBbegin(); if (NULL != task) { zbx_tm_save_task(task); zbx_tm_task_free(task); } DBexecute("update task set status=%d where taskid=" ZBX_FS_UI64, ZBX_TM_STATUS_DONE, taskid); DBcommit(); return ret; }
/****************************************************************************** * * * Function: lld_update_triggers * * * * Purpose: add or update triggers for discovered items * * * ******************************************************************************/ void lld_update_triggers(zbx_uint64_t hostid, zbx_uint64_t lld_ruleid, zbx_vector_ptr_t *lld_rows, char **error) { const char *__function_name = "lld_update_triggers"; DB_RESULT result; DB_ROW row; zbx_vector_ptr_t triggers; zbx_vector_ptr_t functions_proto; zbx_vector_ptr_t items; zbx_lld_trigger_t *trigger; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); zbx_vector_ptr_create(&triggers); /* list of triggers which were created or will be created or */ /* updated by the trigger prototype */ zbx_vector_ptr_create(&functions_proto); /* list of functions which are used by the trigger prototype */ zbx_vector_ptr_create(&items); /* list of items which are related to the trigger prototype */ result = DBselect( "select distinct t.triggerid,t.description,t.expression,t.status,t.type,t.priority,t.comments," "t.url" " from triggers t,functions f,items i,item_discovery id" " where t.triggerid=f.triggerid" " and f.itemid=i.itemid" " and i.itemid=id.itemid" " and id.parent_itemid=" ZBX_FS_UI64, lld_ruleid); /* run through trigger prototypes */ while (NULL != (row = DBfetch(result))) { zbx_uint64_t parent_triggerid; const char *description_proto, *comments_proto, *url; char *expression_proto; unsigned char status, type, priority; int i; ZBX_STR2UINT64(parent_triggerid, row[0]); description_proto = row[1]; expression_proto = zbx_strdup(NULL, row[2]); ZBX_STR2UCHAR(status, row[3]); ZBX_STR2UCHAR(type, row[4]); ZBX_STR2UCHAR(priority, row[5]); comments_proto = row[6]; url = row[7]; lld_triggers_get(parent_triggerid, &triggers, type, priority, url); lld_functions_get(parent_triggerid, &functions_proto, &triggers); lld_items_get(parent_triggerid, &items); /* simplifying trigger expressions */ lld_expression_simplify(&expression_proto, &functions_proto); for (i = 0; i < triggers.values_num; i++) { trigger = (zbx_lld_trigger_t *)triggers.values[i]; lld_expression_simplify(&trigger->expression, &trigger->functions); } /* making triggers */ lld_triggers_make(&functions_proto, &triggers, &items, description_proto, expression_proto, comments_proto, lld_rows, error); lld_triggers_validate(hostid, &triggers, error); lld_triggers_save(parent_triggerid, &triggers, status, type, priority, url); /* cleaning */ zbx_vector_ptr_clean(&items, (zbx_mem_free_func_t)lld_item_free); zbx_vector_ptr_clean(&functions_proto, (zbx_mem_free_func_t)lld_function_free); zbx_vector_ptr_clean(&triggers, (zbx_mem_free_func_t)lld_trigger_free); zbx_free(expression_proto); } DBfree_result(result); zbx_vector_ptr_destroy(&items); zbx_vector_ptr_destroy(&functions_proto); zbx_vector_ptr_destroy(&triggers); zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
/****************************************************************************** * * * Function: main_alerter_loop * * * * Purpose: periodically check table alerts and send notifications if needed * * * * Author: Alexei Vladishev * * * ******************************************************************************/ ZBX_THREAD_ENTRY(alerter_thread, args) { char error[MAX_STRING_LEN], *error_esc; int res, alerts_success, alerts_fail; double sec; DB_RESULT result; DB_ROW row; DB_ALERT alert; DB_MEDIATYPE mediatype; process_type = ((zbx_thread_args_t *)args)->process_type; server_num = ((zbx_thread_args_t *)args)->server_num; process_num = ((zbx_thread_args_t *)args)->process_num; zabbix_log(LOG_LEVEL_INFORMATION, "%s #%d started [%s #%d]", get_daemon_type_string(daemon_type), server_num, get_process_type_string(process_type), process_num); zbx_setproctitle("%s [connecting to the database]", get_process_type_string(process_type)); DBconnect(ZBX_DB_CONNECT_NORMAL); for (;;) { zbx_setproctitle("%s [sending alerts]", get_process_type_string(process_type)); sec = zbx_time(); alerts_success = alerts_fail = 0; result = DBselect( "select a.alertid,a.mediatypeid,a.sendto,a.subject,a.message,a.status,mt.mediatypeid," "mt.type,mt.description,mt.smtp_server,mt.smtp_helo,mt.smtp_email,mt.exec_path," "mt.gsm_modem,mt.username,mt.passwd,mt.smtp_port,mt.smtp_security," "mt.smtp_verify_peer,mt.smtp_verify_host,mt.smtp_authentication,a.retries" " from alerts a,media_type mt" " where a.mediatypeid=mt.mediatypeid" " and a.status=%d" " and a.alerttype=%d" " order by a.alertid", ALERT_STATUS_NOT_SENT, ALERT_TYPE_MESSAGE); while (NULL != (row = DBfetch(result))) { ZBX_STR2UINT64(alert.alertid, row[0]); ZBX_STR2UINT64(alert.mediatypeid, row[1]); alert.sendto = row[2]; alert.subject = row[3]; alert.message = row[4]; alert.status = atoi(row[5]); ZBX_STR2UINT64(mediatype.mediatypeid, row[6]); mediatype.type = atoi(row[7]); mediatype.description = row[8]; mediatype.smtp_server = row[9]; mediatype.smtp_helo = row[10]; mediatype.smtp_email = row[11]; mediatype.exec_path = row[12]; mediatype.gsm_modem = row[13]; mediatype.username = row[14]; mediatype.passwd = row[15]; mediatype.smtp_port = (unsigned short)atoi(row[16]); ZBX_STR2UCHAR(mediatype.smtp_security, row[17]); ZBX_STR2UCHAR(mediatype.smtp_verify_peer, row[18]); ZBX_STR2UCHAR(mediatype.smtp_verify_host, row[19]); ZBX_STR2UCHAR(mediatype.smtp_authentication, row[20]); alert.retries = atoi(row[21]); *error = '\0'; res = execute_action(&alert, &mediatype, error, sizeof(error)); if (SUCCEED == res) { zabbix_log(LOG_LEVEL_DEBUG, "alert ID [" ZBX_FS_UI64 "] was sent successfully", alert.alertid); DBexecute("update alerts set status=%d,error='' where alertid=" ZBX_FS_UI64, ALERT_STATUS_SENT, alert.alertid); alerts_success++; } else { zabbix_log(LOG_LEVEL_DEBUG, "error sending alert ID [" ZBX_FS_UI64 "]", alert.alertid); error_esc = DBdyn_escape_string_len(error, ALERT_ERROR_LEN); alert.retries++; if (ALERT_MAX_RETRIES > alert.retries) { DBexecute("update alerts set retries=%d,error='%s' where alertid=" ZBX_FS_UI64, alert.retries, error_esc, alert.alertid); } else { DBexecute("update alerts set status=%d,retries=%d,error='%s' where alertid=" ZBX_FS_UI64, ALERT_STATUS_FAILED, alert.retries, error_esc, alert.alertid); } zbx_free(error_esc); alerts_fail++; } } DBfree_result(result); sec = zbx_time() - sec; zbx_setproctitle("%s [sent alerts: %d success, %d fail in " ZBX_FS_DBL " sec, idle %d sec]", get_process_type_string(process_type), alerts_success, alerts_fail, sec, CONFIG_SENDER_FREQUENCY); zbx_sleep_loop(CONFIG_SENDER_FREQUENCY); } }