static const char* parse_date(char* date) { // date in the format of yyyyMMdd'T'HHmmSS // 01234567 8 9abcde uint8_t event_second, event_minute, event_hour; uint8_t event_day, event_month; uint16_t event_year; event_second = atoi(&date[0x0d]); date[0x0d] = '\0'; event_minute = atoi(&date[0x0b]); date[0x0b] = '\0'; event_hour = atoi(&date[0x09]); date[0x08] = '\0'; event_day = atoi(&date[0x06]); date[0x06] = '\0'; event_month = atoi(&date[0x04]); date[0x04] = '\0'; event_year = atoi(&date[0x00]); uint32_t event_timestamp = calc_timestamp(event_year - 2000, event_month, event_day, event_hour, event_minute, event_second); uint32_t now_timestamp = rtc_readtime32(); if (event_timestamp > now_timestamp) { // event happen later than now, this should not happen, adjust rtc rtc_settime(event_hour, event_minute, event_second); rtc_setdate(event_year, event_month, event_day); now_timestamp = event_timestamp; } return toEnglishPeriod(now_timestamp - event_timestamp, date); }
uint32_t rtc_readtime32() { uint16_t year = 0; uint8_t month = 0; uint8_t day = 0; uint8_t wday = 0; rtc_readdate(&year, &month, &day, &wday); uint8_t hour = 0; uint8_t min = 0; uint8_t sec = 0; rtc_readtime(&hour, &min, &sec); return calc_timestamp(year - 2000, month, day, hour, min, sec); }
/****************************************************************************** * * * Function: set_item_value * * * * Purpose: set item value for an SNMP Trap item * * * * Author: Rudolfs Kreicbergs * * * ******************************************************************************/ static void set_item_value(DC_ITEM *item, char *trap, zbx_timespec_t *ts) { AGENT_RESULT value; int timestamp = 0; init_result(&value); if (SUCCEED == set_result_type(&value, item->value_type, item->data_type, trap)) { if (ITEM_VALUE_TYPE_LOG == item->value_type) calc_timestamp(trap, ×tamp, item->logtimefmt); dc_add_history(item->itemid, item->value_type, item->flags, &value, ts, ITEM_STATUS_ACTIVE, NULL, timestamp, NULL, 0, 0, 0, 0); } else { dc_add_history(item->itemid, item->value_type, item->flags, NULL, ts, ITEM_STATUS_NOTSUPPORTED, value.msg, 0, NULL, 0, 0, 0, 0); } free_result(&value); }
/****************************************************************************** * * * Function: process_trap_for_interface * * * * Purpose: add trap to all matching items for the specified interface * * * * Return value: SUCCEED - a matching item was found * * FAIL - no matching item was found (including fallback items) * * * * Author: Rudolfs Kreicbergs * * * ******************************************************************************/ static int process_trap_for_interface(zbx_uint64_t interfaceid, char *trap, zbx_timespec_t *ts) { DC_ITEM *items = NULL; const char *regex; char error[ITEM_ERROR_LEN_MAX]; size_t num, i; int ret = FAIL, fb = -1, *lastclocks = NULL, *errcodes = NULL; zbx_uint64_t *itemids = NULL; unsigned char *states = NULL; AGENT_RESULT *results = NULL; AGENT_REQUEST request; zbx_vector_ptr_t regexps; zbx_vector_ptr_create(®exps); num = DCconfig_get_snmp_items_by_interfaceid(interfaceid, &items); itemids = zbx_malloc(itemids, sizeof(zbx_uint64_t) * num); states = zbx_malloc(states, sizeof(unsigned char) * num); lastclocks = zbx_malloc(lastclocks, sizeof(int) * num); errcodes = zbx_malloc(errcodes, sizeof(int) * num); results = zbx_malloc(results, sizeof(AGENT_RESULT) * num); for (i = 0; i < num; i++) { init_result(&results[i]); errcodes[i] = FAIL; items[i].key = zbx_strdup(items[i].key, items[i].key_orig); if (SUCCEED != substitute_key_macros(&items[i].key, NULL, &items[i], NULL, MACRO_TYPE_ITEM_KEY, error, sizeof(error))) { SET_MSG_RESULT(&results[i], zbx_strdup(NULL, error)); errcodes[i] = NOTSUPPORTED; continue; } if (0 == strcmp(items[i].key, "snmptrap.fallback")) { fb = i; continue; } init_request(&request); if (SUCCEED != parse_item_key(items[i].key, &request)) goto next; if (0 != strcmp(get_rkey(&request), "snmptrap")) goto next; if (1 < get_rparams_num(&request)) goto next; if (NULL != (regex = get_rparam(&request, 0))) { if ('@' == *regex) { DCget_expressions_by_name(®exps, regex + 1); if (0 == regexps.values_num) { SET_MSG_RESULT(&results[i], zbx_dsprintf(NULL, "Global regular expression \"%s\" does not exist.", regex + 1)); errcodes[i] = NOTSUPPORTED; goto next; } } if (SUCCEED != regexp_match_ex(®exps, trap, regex, ZBX_CASE_SENSITIVE)) goto next; } if (SUCCEED == set_result_type(&results[i], items[i].value_type, items[i].data_type, trap)) errcodes[i] = SUCCEED; else errcodes[i] = NOTSUPPORTED; ret = SUCCEED; next: free_request(&request); } if (FAIL == ret && -1 != fb) { if (SUCCEED == set_result_type(&results[fb], items[fb].value_type, items[fb].data_type, trap)) errcodes[fb] = SUCCEED; else errcodes[fb] = NOTSUPPORTED; ret = SUCCEED; } for (i = 0; i < num; i++) { switch (errcodes[i]) { case SUCCEED: if (ITEM_VALUE_TYPE_LOG == items[i].value_type) { calc_timestamp(results[i].log->value, &results[i].log->timestamp, items[i].logtimefmt); } items[i].state = ITEM_STATE_NORMAL; dc_add_history(items[i].itemid, items[i].value_type, items[i].flags, &results[i], ts, items[i].state, NULL); itemids[i] = items[i].itemid; states[i] = items[i].state; lastclocks[i] = ts->sec; break; case NOTSUPPORTED: items[i].state = ITEM_STATE_NOTSUPPORTED; dc_add_history(items[i].itemid, items[i].value_type, items[i].flags, NULL, ts, items[i].state, results[i].msg); itemids[i] = items[i].itemid; states[i] = items[i].state; lastclocks[i] = ts->sec; break; } zbx_free(items[i].key); free_result(&results[i]); } zbx_free(results); DCrequeue_items(itemids, states, lastclocks, NULL, NULL, errcodes, num); zbx_free(errcodes); zbx_free(lastclocks); zbx_free(states); zbx_free(itemids); DCconfig_clean_items(items, NULL, num); zbx_free(items); zbx_regexp_clean_expressions(®exps); zbx_vector_ptr_destroy(®exps); dc_flush_history(); return ret; }
/****************************************************************************** * * * Function: process_data * * * * Purpose: process new item value * * * * Parameters: sockfd - descriptor of agent-server socket connection * * server - server name * * key - item's key * * value - new value of server:key * * lastlogsize - if key=log[*], last size of log file * * * * Return value: SUCCEED - new value processed sucesfully * * FAIL - otherwise * * * * Author: Alexei Vladishev * * * * Comments: for trapper server process * * * ******************************************************************************/ static void process_mass_data(zbx_sock_t *sock, zbx_uint64_t proxy_hostid, AGENT_VALUE *values, int value_num, int *processed, time_t proxy_timediff) { AGENT_RESULT agent; DB_RESULT result; DB_ROW row; DB_ITEM item; char host_esc[MAX_STRING_LEN], key_esc[MAX_STRING_LEN]; static char *sql = NULL; static int sql_allocated = 65536; int sql_offset = 0, i; zabbix_log(LOG_LEVEL_DEBUG, "In process_mass_data()"); if (NULL == sql) sql = zbx_malloc(sql, sql_allocated); DCinit_nextchecks(); zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 2048, "select %s where h.hostid=i.hostid and h.proxy_hostid=" ZBX_FS_UI64 " and h.status=%d and i.status in (%d,%d)", ZBX_SQL_ITEM_SELECT, proxy_hostid, HOST_STATUS_MONITORED, ITEM_STATUS_ACTIVE, ITEM_STATUS_NOTSUPPORTED); if (proxy_hostid == 0) { zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 64, " and i.type in (%d,%d)", ITEM_TYPE_TRAPPER, ITEM_TYPE_ZABBIX_ACTIVE); } else { zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 64, " and i.type in (%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)", ITEM_TYPE_ZABBIX, ITEM_TYPE_SNMPv1, ITEM_TYPE_TRAPPER, ITEM_TYPE_SIMPLE, ITEM_TYPE_SNMPv2c, ITEM_TYPE_SNMPv3, ITEM_TYPE_ZABBIX_ACTIVE, ITEM_TYPE_HTTPTEST, ITEM_TYPE_EXTERNAL, ITEM_TYPE_IPMI); } zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 8, " and ("); for (i = 0; i < value_num; i++) { DBescape_string(values[i].host_name, host_esc, sizeof(host_esc)); DBescape_string(values[i].key, key_esc, sizeof(key_esc)); zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, "(h.host='%s' and i.key_='%s') or ", host_esc, key_esc); } sql_offset -= 4; zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, ")" DB_NODE, DBnode_local("h.hostid")); result = DBselect("%s", sql); while (NULL != (row = DBfetch(result))) { DBget_item_from_db(&item, row); if (item.type == ITEM_TYPE_ZABBIX_ACTIVE && FAIL == zbx_tcp_check_security(sock, item.trapper_hosts, 1)) continue; if (item.maintenance_status == HOST_MAINTENANCE_STATUS_ON && item.maintenance_type == MAINTENANCE_TYPE_NODATA && item.maintenance_from <= values[i].clock) continue; for (i = 0; i < value_num; i++) { if (0 == strcmp(item.host_name, values[i].host_name) && 0 == strcmp(item.key_orig, values[i].key)) { /* zabbix_log(LOG_LEVEL_DEBUG, "Processing [%s@%s: \"%s\"]", item.key, item.host_name, values[i].value);*/ if (0 == strcmp(values[i].value, "ZBX_NOTSUPPORTED")) { zabbix_log(LOG_LEVEL_WARNING, "Active parameter [%s] is not supported by agent on host [%s]", item.key_orig, item.host_name); zabbix_syslog("Active parameter [%s] is not supported by agent on host [%s]", item.key_orig, item.host_name); DCadd_nextcheck(&item, values[i].clock, proxy_timediff, "Not supported by ZABBIX agent"); if (NULL != processed) (*processed)++; } else { if (0 == strncmp(item.key, "log[", 4) || 0 == strncmp(item.key, "eventlog[", 9)) { item.lastlogsize = values[i].lastlogsize; item.timestamp = values[i].timestamp; calc_timestamp(values[i].value, &item.timestamp, item.logtimefmt); item.eventlog_severity = values[i].severity; item.eventlog_source = values[i].source; /* zabbix_log(LOG_LEVEL_DEBUG, "Value [%s] Lastlogsize [%s] Timestamp [%s]", values[i].value, item.lastlogsize, item.timestamp);*/ } init_result(&agent); if (SUCCEED == set_result_type(&agent, item.value_type, item.data_type, values[i].value)) { if (0 == CONFIG_DBSYNCER_FORKS) DBbegin(); switch (zbx_process) { case ZBX_PROCESS_SERVER: process_new_value(&item, &agent, values[i].clock); break; case ZBX_PROCESS_PROXY: proxy_process_new_value(&item, &agent, values[i].clock); break; } if (0 == CONFIG_DBSYNCER_FORKS) DBcommit(); if (NULL != processed) (*processed)++; /* only for screen Administration|Queue */ if (item.type != ITEM_TYPE_TRAPPER && item.type != ITEM_TYPE_HTTPTEST && item.value_type != ITEM_VALUE_TYPE_LOG && 0 != strcmp(item.key, SERVER_STATUS_KEY) && 0 != strcmp(item.key, SERVER_ICMPPING_KEY) && 0 != strcmp(item.key, SERVER_ICMPPINGSEC_KEY) && 0 != strcmp(item.key, SERVER_ZABBIXLOG_KEY)) DCadd_nextcheck(&item, values[i].clock, proxy_timediff, NULL); } else { if (GET_MSG_RESULT(&agent)) zabbix_log(LOG_LEVEL_WARNING, "Item [%s] error: %s", zbx_host_key_string_by_item(&item), agent.msg); } free_result(&agent); } } } } DBfree_result(result); DCflush_nextchecks(); }