void add_regexp_ex(zbx_vector_ptr_t *regexps, const char *name, const char *expression, int expression_type, char exp_delimiter, int case_sensitive) { zbx_expression_t *regexp; regexp = zbx_malloc(NULL, sizeof(zbx_expression_t)); regexp->name = zbx_strdup(NULL, name); regexp->expression = zbx_strdup(NULL, expression); regexp->expression_type = expression_type; regexp->exp_delimiter = exp_delimiter; regexp->case_sensitive = case_sensitive; zbx_vector_ptr_append(regexps, regexp); }
static size_t WRITEFUNCTION2(void *ptr, size_t size, size_t nmemb, void *userdata) { size_t r_size = size * nmemb; /* first piece of data */ if (NULL == page.data) { page.allocated = MAX(8096, r_size); page.offset = 0; page.data = zbx_malloc(page.data, page.allocated); } zbx_strncpy_alloc(&page.data, &page.allocated, &page.offset, ptr, r_size); return r_size; }
/* expand the string message from a specific event handler */ static char *expand_message6(const wchar_t *pname, EVT_HANDLE event) { const char *__function_name = "expand_message6"; wchar_t *pmessage = NULL; EVT_HANDLE provider = NULL; DWORD require = 0; char *out_message = NULL; char *tmp_pname = NULL; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); if (NULL == (provider = EvtOpenPublisherMetadata(NULL, pname, NULL, 0, 0))) { tmp_pname = zbx_unicode_to_utf8(pname); zabbix_log(LOG_LEVEL_DEBUG, "provider '%s' could not be opened: %s", strerror_from_system(GetLastError()), tmp_pname); zbx_free(tmp_pname); goto finish; } if (TRUE != EvtFormatMessage(provider, event, 0, 0, NULL, EvtFormatMessageEvent, 0, NULL, &require) ) { if (ERROR_INSUFFICIENT_BUFFER == GetLastError()) { pmessage = zbx_malloc(pmessage, sizeof(WCHAR) * require); if (TRUE != EvtFormatMessage(provider, event, 0, 0, NULL, EvtFormatMessageEvent, require, pmessage, &require)) { zabbix_log(LOG_LEVEL_DEBUG, "formatting message failed: %s", strerror_from_system(GetLastError())); goto finish; } out_message = zbx_unicode_to_utf8(pmessage); } } finish: if (NULL != provider) EvtClose(provider); zbx_free(pmessage); zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, out_message); /* should be freed*/ return out_message; }
/****************************************************************************** * * * Function: db_get_query_tags * * * * Purpose: get event query tags from database * * * ******************************************************************************/ static void db_get_query_tags(zbx_vector_ptr_t *event_queries) { DB_ROW row; DB_RESULT result; int i; char *sql = NULL; size_t sql_alloc = 0, sql_offset = 0; zbx_event_suppress_query_t *query; zbx_vector_uint64_t eventids; zbx_uint64_t eventid; zbx_tag_t *tag; zbx_vector_uint64_create(&eventids); for (i = 0; i < event_queries->values_num; i++) { query = (zbx_event_suppress_query_t *)event_queries->values[i]; zbx_vector_uint64_append(&eventids, query->eventid); } zbx_strcpy_alloc(&sql, &sql_alloc, &sql_offset, "select eventid,tag,value from problem_tag where"); DBadd_condition_alloc(&sql, &sql_alloc, &sql_offset, "eventid", eventids.values, eventids.values_num); zbx_strcpy_alloc(&sql, &sql_alloc, &sql_offset, " order by eventid"); result = DBselect("%s", sql); zbx_free(sql); i = 0; query = (zbx_event_suppress_query_t *)event_queries->values[0]; while (NULL != (row = DBfetch(result))) { ZBX_STR2UINT64(eventid, row[0]); while (query->eventid != eventid) query = (zbx_event_suppress_query_t *)event_queries->values[++i]; tag = (zbx_tag_t *)zbx_malloc(NULL, sizeof(zbx_tag_t)); tag->tag = zbx_strdup(NULL, row[1]); tag->value = zbx_strdup(NULL, row[2]); zbx_vector_ptr_append(&query->tags, tag); } DBfree_result(result); zbx_vector_uint64_destroy(&eventids); }
/* function 'zbx_get_process_username' require 'userName' with size 'MAX_NAME' */ static int zbx_get_process_username(HANDLE hProcess, char *userName) { HANDLE tok; TOKEN_USER *ptu = NULL; DWORD sz = 0, nlen, dlen; TCHAR name[MAX_NAME], dom[MAX_NAME]; int iUse, res = FAIL; assert(userName); /* clean result; */ *userName = '******'; /* open the processes token */ if (0 == OpenProcessToken(hProcess, TOKEN_QUERY, &tok)) return res; /* Get required buffer size and allocate the TOKEN_USER buffer */ if (0 == GetTokenInformation(tok, (TOKEN_INFORMATION_CLASS)1, (LPVOID)ptu, 0, &sz)) { if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) goto lbl_err; ptu = (PTOKEN_USER)zbx_malloc(ptu, sz); } /* Get the token user information from the access token. */ if (0 == GetTokenInformation(tok, (TOKEN_INFORMATION_CLASS)1, (LPVOID)ptu, sz, &sz)) goto lbl_err; /* get the account/domain name of the SID */ nlen = MAX_NAME; dlen = MAX_NAME; if (0 == LookupAccountSid(NULL, ptu->User.Sid, name, &nlen, dom, &dlen, (PSID_NAME_USE)&iUse)) goto lbl_err; zbx_unicode_to_utf8_static(name, userName, MAX_NAME); res = SUCCEED; lbl_err: zbx_free(ptu); CloseHandle(tok); return res; }
/****************************************************************************** * * * Function: main_snmptrapper_loop * * * * Purpose: SNMP trap reader's entry point * * * * Author: Rudolfs Kreicbergs * * * ******************************************************************************/ ZBX_THREAD_ENTRY(snmptrapper_thread, args) { const char *__function_name = "main_snmptrapper_loop"; double sec; 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_program_type_string(program_type), server_num, get_process_type_string(process_type), process_num); zabbix_log(LOG_LEVEL_DEBUG, "In %s() trapfile:'%s'", __function_name, CONFIG_SNMPTRAP_FILE); zbx_setproctitle("%s [connecting to the database]", get_process_type_string(process_type)); DBconnect(ZBX_DB_CONNECT_NORMAL); DBget_lastsize(); buffer = zbx_malloc(buffer, MAX_BUFFER_LEN); *buffer = '\0'; for (;;) { zbx_handle_log(); zbx_setproctitle("%s [processing data]", get_process_type_string(process_type)); sec = zbx_time(); while (SUCCEED == get_latest_data()) read_traps(); sec = zbx_time() - sec; zbx_setproctitle("%s [processed data in " ZBX_FS_DBL " sec, idle 1 sec]", get_process_type_string(process_type), sec); zbx_sleep_loop(1); } zbx_free(buffer); if (-1 != trap_fd) close(trap_fd); }
/* function 'GetProcessUsername' require 'userName' with size 'MAX_NAME' */ static int GetProcessUsername(HANDLE hProcess, char *userName) { HANDLE tok; TOKEN_USER *ptu = NULL; DWORD sz = 0, nlen, dlen; char name[MAX_NAME], dom[MAX_NAME]; int iUse, res = 0; assert(userName); //clean result; *userName = '******'; //open the processes token if (0 == OpenProcessToken(hProcess, TOKEN_QUERY, &tok)) return res; // Get required buffer size and allocate the TOKEN_USER buffer if (0 == GetTokenInformation(tok, (TOKEN_INFORMATION_CLASS)1, (LPVOID)ptu, 0, &sz)) { if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) goto lbl_err; ptu = (PTOKEN_USER)zbx_malloc(ptu, sz); } // Get the token user information from the access token. if (0 == GetTokenInformation(tok, (TOKEN_INFORMATION_CLASS)1, (LPVOID)ptu, sz, &sz)) goto lbl_err; //get the account/domain name of the SID nlen = sizeof(name); dlen = sizeof(dom); if (0 == LookupAccountSid(NULL, ptu->User.Sid, name, &nlen, dom, &dlen, (PSID_NAME_USE)&iUse)) goto lbl_err; zbx_strlcpy(userName, name, MAX_NAME); res = 1; lbl_err: zbx_free(ptu); CloseHandle(tok); return res; }
static zbx_ipmi_sensor_t *allocate_ipmi_sensor(zbx_ipmi_host_t *h, ipmi_sensor_t *sensor) { const char *__function_name = "allocate_ipmi_sensor"; char id_str[2 * IPMI_SENSOR_ID_SZ + 1]; zbx_ipmi_sensor_t *s; char id[IPMI_SENSOR_ID_SZ]; enum ipmi_str_type_e id_type; int id_sz, sz; char full_name[MAX_STRING_LEN]; id_sz = ipmi_sensor_get_id_length(sensor); memset(id, 0, sizeof(id)); ipmi_sensor_get_id(sensor, id, sizeof(id)); id_type = ipmi_sensor_get_id_type(sensor); zabbix_log(LOG_LEVEL_DEBUG, "In %s() sensor:'%s@[%s]:%d'", __function_name, sensor_id_to_str(id_str, sizeof(id_str), id, id_type, id_sz), h->ip, h->port); h->sensor_count++; sz = h->sensor_count * sizeof(zbx_ipmi_sensor_t); if (NULL == h->sensors) h->sensors = zbx_malloc(h->sensors, sz); else h->sensors = zbx_realloc(h->sensors, sz); s = &h->sensors[h->sensor_count - 1]; s->sensor = sensor; memcpy(s->id, id, sizeof(id)); s->id_type = id_type; s->id_sz = id_sz; memset(&s->value, 0, sizeof(s->value)); s->reading_type = ipmi_sensor_get_event_reading_type(sensor); s->type = ipmi_sensor_get_sensor_type(sensor); ipmi_sensor_get_name(s->sensor, full_name, sizeof(full_name)); zabbix_log(LOG_LEVEL_DEBUG, "Added sensor: host:'%s:%d' id_type:%d id_sz:%d id:'%s'" " reading_type:0x%x ('%s') type:0x%x ('%s') full_name:'%s'", h->ip, h->port, s->id_type, s->id_sz, sensor_id_to_str(id_str, sizeof(id_str), s->id, s->id_type, s->id_sz), s->reading_type, ipmi_sensor_get_event_reading_type_string(s->sensor), s->type, ipmi_sensor_get_sensor_type_string(s->sensor), full_name); zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%p", __function_name, s); return s; }
static void collect_args(char **argv, int argc, char **args, size_t *args_alloc) { int i; size_t args_offset = 0; if (0 == *args_alloc) { *args_alloc = ARGS_START_SIZE; *args = zbx_malloc(*args, *args_alloc); } for (i = 0; i < argc; i++) zbx_snprintf_alloc(args, args_alloc, &args_offset, "%s ", argv[i]); if (0 != args_offset) args_offset--; /* ' ' */ (*args)[args_offset] = '\0'; }
int VFS_FS_DISCOVERY(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { int rc, sz, i, ret = SYSINFO_RET_FAIL; struct vmount *vm = NULL; struct zbx_json j; /* check how many bytes to allocate for the mounted filesystems */ if (-1 == (rc = mntctl(MCTL_QUERY, sizeof(sz), (char *)&sz))) return ret; vm = zbx_malloc(vm, (size_t)sz); /* get the list of mounted filesystems */ /* return code is number of filesystems returned */ if (-1 == (rc = mntctl(MCTL_QUERY, sz, (char *)vm))) goto error; zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN); zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA); for (i = 0; i < rc; i++) { zbx_json_addobject(&j, NULL); zbx_json_addstring(&j, "{#FSNAME}", (char *)vm + vm->vmt_data[VMT_STUB].vmt_off, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&j, "{#FSTYPE}", zbx_get_vfs_name_by_type(vm->vmt_gfstype), ZBX_JSON_TYPE_STRING); zbx_json_close(&j); /* go to the next vmount structure */ vm = (struct vmount *)((char *)vm + vm->vmt_length); } zbx_json_close(&j); SET_STR_RESULT(result, zbx_strdup(NULL, j.buffer)); zbx_json_free(&j); ret = SYSINFO_RET_OK; error: zbx_free(vm); return ret; }
PDH_STATUS zbx_PdhMakeCounterPath(const char *function, PDH_COUNTER_PATH_ELEMENTS *cpe, char *counterpath) { DWORD dwSize = PDH_MAX_COUNTER_PATH; wchar_t *wcounterPath = NULL; PDH_STATUS pdh_status; wcounterPath = zbx_malloc(wcounterPath, sizeof(wchar_t) * PDH_MAX_COUNTER_PATH); if (ERROR_SUCCESS != (pdh_status = PdhMakeCounterPath(cpe, wcounterPath, &dwSize, 0))) { zabbix_log(LOG_LEVEL_ERR, "%s(): cannot make counterpath '%s': %s", function, counterpath, strerror_from_module(pdh_status, L"PDH.DLL")); } zbx_unicode_to_utf8_static(wcounterPath, counterpath, PDH_MAX_COUNTER_PATH); zbx_free(wcounterPath); return pdh_status; }
static void collect_args(char **argv, int argc, char **args, size_t *args_alloc) { int i, args_offset, len; if (0 == *args_alloc) { *args_alloc = ARGS_START_SIZE; *args = zbx_malloc(*args, *args_alloc); } args_offset = 0; for (i = 0; i < argc; i ++ ) { len = (int)(strlen(argv[i]) + 2/* ' '+'\0' */); zbx_snprintf_alloc(args, (int *)args_alloc, &args_offset, len, "%s ", argv[i]); } if (0 != args_offset) args_offset--; /* ' ' */ (*args)[args_offset] = '\0'; }
/****************************************************************************** * * * Function: http_substitute_macros * * * * Purpose: substitute macros in input string by value from http test config * * * * Parameters: macros - [IN] macros from httptest * * data - [IN\OUT] string to substitute macros * * * * Author: Alexei Vladishev * * * ******************************************************************************/ void http_substitute_macros(const char *macros, char **data) { const char *__function_name = "http_substitute_macros"; char c, *replace_to = NULL; size_t l, r, replace_to_alloc = 64; int rc; zabbix_log(LOG_LEVEL_DEBUG, "In %s() data:'%s'", __function_name, *data); for (l = 0; '\0' != (*data)[l]; l++) { if ('{' != (*data)[l]) continue; for (r = l + 1; '\0' != (*data)[r] && '}' != (*data)[r]; r++) ; if ('}' != (*data)[r]) break; if (NULL == replace_to) replace_to = zbx_malloc(replace_to, replace_to_alloc); c = (*data)[r + 1]; (*data)[r + 1] = '\0'; rc = http_get_macro_value(macros, &(*data)[l], &replace_to, &replace_to_alloc); (*data)[r + 1] = c; if (SUCCEED != rc) continue; zbx_replace_string(data, l, &r, replace_to); l = r; } zbx_free(replace_to); zabbix_log(LOG_LEVEL_DEBUG, "End of %s() data:'%s'", __function_name, *data); }
/****************************************************************************** * * * Function: hk_history_delete_queue_append * * * * Purpose: add item to the delete queue if necessary * * * * Parameters: rule - [IN/OUT] the history housekeeping rule * * now - [IN] the current timestmap * * item_record - [IN/OUT] the record from item cache containing * * item to process and its oldest record timestamp * * history - [IN] a number of days the history data for * * item_record must be kept. * * * * Author: Andris Zeila * * * * Comments: If item is added to delete queue, its oldest record timestamp * * (min_clock) is updated to the calculated 'cutoff' value. * * * ******************************************************************************/ static void hk_history_delete_queue_append(zbx_hk_history_rule_t *rule, int now, zbx_hk_item_cache_t *item_record, int history) { int keep_from = now - history * SEC_PER_DAY; if (keep_from > item_record->min_clock) { zbx_hk_delete_queue_t *update_record; /* update oldest timestamp in item cache */ item_record->min_clock = MIN(keep_from, item_record->min_clock + HK_MAX_DELETE_PERIODS * CONFIG_HOUSEKEEPING_FREQUENCY * SEC_PER_HOUR); update_record = zbx_malloc(NULL, sizeof(zbx_hk_delete_queue_t)); update_record->itemid = item_record->itemid; update_record->min_clock = item_record->min_clock; zbx_vector_ptr_append(&rule->delete_queue, update_record); } }
wchar_t *get_counter_name(DWORD pdhIndex) { const char *__function_name = "get_counter_name"; PERF_COUNTER_ID *counterName; DWORD dwSize; PDH_STATUS pdh_status; zabbix_log(LOG_LEVEL_DEBUG, "In %s() pdhIndex:%u", __function_name, pdhIndex); counterName = PerfCounterList; while (NULL != counterName) { if (counterName->pdhIndex == pdhIndex) break; counterName = counterName->next; } if (NULL == counterName) { counterName = (PERF_COUNTER_ID *)zbx_malloc(counterName, sizeof(PERF_COUNTER_ID)); memset(counterName, 0, sizeof(PERF_COUNTER_ID)); counterName->pdhIndex = pdhIndex; counterName->next = PerfCounterList; dwSize = PDH_MAX_COUNTER_NAME; if (ERROR_SUCCESS == (pdh_status = PdhLookupPerfNameByIndex(NULL, pdhIndex, counterName->name, &dwSize))) PerfCounterList = counterName; else { zabbix_log(LOG_LEVEL_ERR, "PdhLookupPerfNameByIndex() failed: %s", strerror_from_module(pdh_status, L"PDH.DLL")); zbx_free(counterName); zabbix_log(LOG_LEVEL_DEBUG, "End of %s():FAIL", __function_name); return L"UnknownPerformanceCounter"; } } zabbix_log(LOG_LEVEL_DEBUG, "End of %s():SUCCEED", __function_name); return counterName->name; }
/****************************************************************************** * * * Function: add_history_text * * * ******************************************************************************/ static void add_history_text(zbx_vector_ptr_t *history) { int i; zbx_db_insert_t *db_insert; db_insert = (zbx_db_insert_t *)zbx_malloc(NULL, sizeof(zbx_db_insert_t)); zbx_db_insert_prepare(db_insert, "history_text", "itemid", "clock", "ns", "value", NULL); for (i = 0; i < history->values_num; i++) { const ZBX_DC_HISTORY *h = (ZBX_DC_HISTORY *)history->values[i]; if (ITEM_VALUE_TYPE_TEXT != h->value_type) continue; zbx_db_insert_add_values(db_insert, h->itemid, h->ts.sec, h->ts.ns, h->value.str); } sql_writer_add_dbinsert(db_insert); }
zbx_log_t *add_log_result(AGENT_RESULT *result, const char *value) { zbx_log_t *log; size_t i; log = zbx_malloc(NULL, sizeof(zbx_log_t)); zbx_log_init(log); log->value = zbx_strdup(log->value, value); for (i = 0; NULL != result->logs && NULL != result->logs[i]; i++) ; result->logs = zbx_realloc(result->logs, sizeof(zbx_log_t *) * (i + 2)); result->logs[i++] = log; result->logs[i] = NULL; result->type |= AR_LOG; return log; }
static void add_user_msg(zbx_uint64_t userid, zbx_uint64_t mediatypeid, ZBX_USER_MSG **user_msg, const char *subject, const char *message, unsigned char source, zbx_uint64_t triggerid) { const char *__function_name = "add_user_msg"; ZBX_USER_MSG *p; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); if (SUCCEED != check_perm2system(userid)) return; if (EVENT_SOURCE_TRIGGERS == source && PERM_READ_ONLY > get_trigger_permission(userid, triggerid)) return; p = *user_msg; while (NULL != p) { if (p->userid == userid && p->mediatypeid == mediatypeid && 0 == strcmp(p->subject, subject) && 0 == strcmp(p->message, message)) break; p = p->next; } if (NULL == p) { p = zbx_malloc(p, sizeof(ZBX_USER_MSG)); p->userid = userid; p->mediatypeid = mediatypeid; p->subject = zbx_strdup(NULL, subject); p->message = zbx_strdup(NULL, message); p->next = *user_msg; *user_msg = p; } zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
static void cache_put_snmp_index(DC_ITEM *item, char *oid, char *value, int index) { const char *__function_name = "cache_put_snmp_index"; int i; zbx_snmp_index_t s; zabbix_log(LOG_LEVEL_DEBUG, "In %s() oid:'%s' value:'%s' index:%d", __function_name, oid, value, index); if (NULL == snmpidx) snmpidx = zbx_malloc(snmpidx, snmpidx_alloc * sizeof(zbx_snmp_index_t)); s.hostid = item->host.hostid; s.port = item->interface.port; s.oid = oid; s.value = value; if (snmpidx_count > (i = get_snmpidx_nearestindex(&s)) && 0 == zbx_snmp_index_compare(&s, &snmpidx[i])) { snmpidx[i].index = index; goto end; } if (snmpidx_count == snmpidx_alloc) { snmpidx_alloc += 16; snmpidx = zbx_realloc(snmpidx, snmpidx_alloc * sizeof(zbx_snmp_index_t)); } memmove(&snmpidx[i + 1], &snmpidx[i], sizeof(zbx_snmp_index_t) * (snmpidx_count - i)); snmpidx[i].hostid = item->host.hostid; snmpidx[i].port = item->interface.port; snmpidx[i].oid = strdup(oid); snmpidx[i].value = strdup(value); snmpidx[i].index = index; snmpidx_count++; end: zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
static int check_service_starttype(SC_HANDLE h_srv, int start_type) { int ret = FAIL; DWORD sz; QUERY_SERVICE_CONFIG *qsc = NULL; if (ZBX_SRV_STARTTYPE_ALL == start_type) return SUCCEED; QueryServiceConfig(h_srv, NULL, 0, &sz); if (ERROR_INSUFFICIENT_BUFFER != GetLastError()) return FAIL; qsc = (QUERY_SERVICE_CONFIG *)zbx_malloc(qsc, sz); if (0 != QueryServiceConfig(h_srv, qsc, sz, &sz)) { switch (start_type) { case ZBX_SRV_STARTTYPE_AUTOMATIC: if (SERVICE_AUTO_START == qsc->dwStartType) ret = SUCCEED; break; case ZBX_SRV_STARTTYPE_MANUAL: if (SERVICE_DEMAND_START == qsc->dwStartType) ret = SUCCEED; break; case ZBX_SRV_STARTTYPE_DISABLED: if (SERVICE_DISABLED == qsc->dwStartType) ret = SUCCEED; break; } } zbx_free(qsc); return ret; }
/****************************************************************************** * * * Function: process_pinger_hosts * * * * Purpose: * * * * Parameters: * * * * Return value: * * * * Author: Alexander Vladishev * * * * Comments: * * * ******************************************************************************/ static void process_pinger_hosts(icmpitem_t *items, int items_count) { const char *__function_name = "process_pinger_hosts"; int i, first_index = 0, ping_result; char error[ITEM_ERROR_LEN_MAX]; static ZBX_FPING_HOST *hosts = NULL; static int hosts_alloc = 4; int hosts_count = 0; zbx_timespec_t ts; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); if (NULL == hosts) hosts = zbx_malloc(hosts, sizeof(ZBX_FPING_HOST) * hosts_alloc); for (i = 0; i < items_count; i++) { add_pinger_host(&hosts, &hosts_alloc, &hosts_count, items[i].addr); if (i == items_count - 1 || items[i].count != items[i + 1].count || items[i].interval != items[i + 1].interval || items[i].size != items[i + 1].size || items[i].timeout != items[i + 1].timeout) { zbx_setproctitle("%s [pinging hosts]", get_process_type_string(process_type)); zbx_timespec(&ts); ping_result = do_ping(hosts, hosts_count, items[i].count, items[i].interval, items[i].size, items[i].timeout, error, sizeof(error)); process_values(items, first_index, i + 1, hosts, hosts_count, &ts, ping_result, error); hosts_count = 0; first_index = i + 1; } } zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
int SYSTEM_USERS_NUM(AGENT_REQUEST *request, AGENT_RESULT *result) { #ifdef _WINDOWS char counter_path[64]; AGENT_REQUEST request_tmp; int ret; zbx_snprintf(counter_path, sizeof(counter_path), "\\%d\\%d", PCI_TERMINAL_SERVICES, PCI_TOTAL_SESSIONS); request_tmp.nparam = 1; request_tmp.params = zbx_malloc(NULL, request_tmp.nparam * sizeof(char *)); request_tmp.params[0] = counter_path; ret = PERF_COUNTER(&request_tmp, result); zbx_free(request_tmp.params); return ret; #else return EXECUTE_INT("who | wc -l", result); #endif }
/****************************************************************************** * * * Function: main_pinger_loop * * * * Purpose: periodically perform ICMP pings * * * * Parameters: * * * * Return value: * * * * Author: Alexei Vladishev * * * * Comments: never returns * * * ******************************************************************************/ ZBX_THREAD_ENTRY(pinger_thread, args) { int nextcheck, sleeptime, items_count = 0, itc; double sec; static icmpitem_t *items = NULL; static int items_alloc = 4; 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); if (NULL == items) items = zbx_malloc(items, sizeof(icmpitem_t) * items_alloc); for (;;) { zbx_setproctitle("%s #%d [getting values]", get_process_type_string(process_type), process_num); sec = zbx_time(); get_pinger_hosts(&items, &items_alloc, &items_count); process_pinger_hosts(items, items_count); sec = zbx_time() - sec; itc = items_count; free_hosts(&items, &items_count); nextcheck = DCconfig_get_poller_nextcheck(ZBX_POLLER_TYPE_PINGER); sleeptime = calculate_sleeptime(nextcheck, POLLER_DELAY); zbx_setproctitle("%s #%d [got %d values in " ZBX_FS_DBL " sec, idle %d sec]", get_process_type_string(process_type), process_num, itc, sec, sleeptime); zbx_sleep_loop(sleeptime); } }
static int check_delayed_start(SC_HANDLE h_srv) { SERVICE_DELAYED_AUTO_START_INFO *sds = NULL; DWORD sz = 0; int ret = FAIL; QueryServiceConfig2(h_srv, SERVICE_CONFIG_DELAYED_AUTO_START_INFO, NULL, 0, &sz); if (ERROR_INSUFFICIENT_BUFFER == GetLastError()) { sds = (SERVICE_DELAYED_AUTO_START_INFO *)zbx_malloc(sds, sz); if (0 != QueryServiceConfig2(h_srv, SERVICE_CONFIG_DELAYED_AUTO_START_INFO, (LPBYTE)sds, sz, &sz) && TRUE == sds->fDelayedAutostart) { ret = SUCCEED; } zbx_free(sds); } return ret; }
/****************************************************************************** * * * Function: save_events * * * * Purpose: flushes the events into a database * * * ******************************************************************************/ static void save_events() { char *sql = NULL; size_t sql_alloc = 2 * ZBX_KIBIBYTE, sql_offset = 0, i; const char *ins_event_sql = "insert into events (eventid,source,object,objectid,clock,ns,value) values "; sql = zbx_malloc(sql, sql_alloc); DBbegin_multiple_update(&sql, &sql_alloc, &sql_offset); #ifdef HAVE_MULTIROW_INSERT zbx_strcpy_alloc(&sql, &sql_alloc, &sql_offset, ins_event_sql); #endif for (i = 0; i < events_num; i++) { if (0 == events[i].eventid) events[i].eventid = DBget_maxid("events"); #ifndef HAVE_MULTIROW_INSERT zbx_strcpy_alloc(&sql, &sql_alloc, &sql_offset, ins_event_sql); #endif zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset, "(" ZBX_FS_UI64 ",%d,%d," ZBX_FS_UI64 ",%d,%d,%d)" ZBX_ROW_DL, events[i].eventid, events[i].source, events[i].object, events[i].objectid, events[i].clock, events[i].ns, events[i].value); } #ifdef HAVE_MULTIROW_INSERT sql_offset--; zbx_strcpy_alloc(&sql, &sql_alloc, &sql_offset, ";\n"); #endif DBend_multiple_update(&sql, &sql_alloc, &sql_offset); DBexecute("%s", sql); zbx_free(sql); }
/****************************************************************************** * * * Function: * * * * Purpose: * * * * Parameters: * * * * Return value: * * * * Comments: * * * ******************************************************************************/ int MAIN_ZABBIX_ENTRY() { zbx_thread_args_t *thread_args; int thread_num = 0; int status; if (NULL == CONFIG_LOG_FILE || '\0' == *CONFIG_LOG_FILE) { zabbix_open_log(LOG_TYPE_SYSLOG, CONFIG_LOG_LEVEL, NULL); } else { zabbix_open_log(LOG_TYPE_FILE, CONFIG_LOG_LEVEL, CONFIG_LOG_FILE); } zabbix_log(LOG_LEVEL_INFORMATION, "Starting Job Arranger monitor. Job Arranger %s (revision %s).", JOBARG_VERSION, JOBARG_REVISION); /* --- START THREADS --- */ threads_num = 1; threads = (ZBX_THREAD_HANDLE *) zbx_calloc(threads, threads_num, sizeof(ZBX_THREAD_HANDLE)); /* start the executive thread */ thread_args = (zbx_thread_args_t *) zbx_malloc(NULL, sizeof(zbx_thread_args_t)); thread_args->thread_num = thread_num; thread_args->args = NULL; threads[thread_num++] = zbx_thread_start(monitor_thread, thread_args); while (-1 == wait(&status)) { if (EINTR != errno) { zabbix_log(LOG_LEVEL_ERR, "failed to wait on child processes: %s", zbx_strerror(errno)); break; } } THIS_SHOULD_NEVER_HAPPEN; zbx_on_exit(); return SUCCEED; }
/****************************************************************************** * * * Function: main_pinger_loop * * * * Purpose: periodically perform ICMP pings * * * * Parameters: * * * * Return value: * * * * Author: Alexei Vladishev * * * * Comments: never returns * * * ******************************************************************************/ void main_pinger_loop() { int nextcheck, sleeptime; double sec; static icmpitem_t *items = NULL; static int items_alloc = 4; int items_count = 0; zabbix_log(LOG_LEVEL_DEBUG, "In main_pinger_loop() process_num:%d", process_num); if (NULL == items) items = zbx_malloc(items, sizeof(icmpitem_t) * items_alloc); zbx_setproctitle("%s [connecting to the database]", get_process_type_string(process_type)); DBconnect(ZBX_DB_CONNECT_NORMAL); for (;;) { zbx_setproctitle("%s [getting values]", get_process_type_string(process_type)); sec = zbx_time(); get_pinger_hosts(&items, &items_alloc, &items_count); process_pinger_hosts(items, items_count); sec = zbx_time() - sec; zabbix_log(LOG_LEVEL_DEBUG, "%s #%d spent " ZBX_FS_DBL " seconds while processing %d items", get_process_type_string(process_type), process_num, sec, items_count); free_hosts(&items, &items_count); nextcheck = DCconfig_get_poller_nextcheck(ZBX_POLLER_TYPE_PINGER); sleeptime = calculate_sleeptime(nextcheck, POLLER_DELAY); zbx_sleep_loop(sleeptime); } }
static int proc_argv(pid_t pid, char ***argv, size_t *argv_alloc, int *argc) { size_t sz; int mib[4]; if (NULL == *argv) { *argv_alloc = ARGS_START_SIZE; *argv = zbx_malloc(*argv, *argv_alloc); } mib[0] = CTL_KERN; mib[1] = KERN_PROC_ARGS; mib[2] = (int)pid; mib[3] = KERN_PROC_ARGV; retry: sz = *argv_alloc; if (0 != sysctl(mib, 4, *argv, &sz, NULL, 0)) { if (errno == ENOMEM) { *argv_alloc *= 2; *argv = zbx_realloc(*argv, *argv_alloc); goto retry; } return FAIL; } mib[3] = KERN_PROC_NARGV; sz = sizeof(int); if (0 != sysctl(mib, 4, argc, &sz, NULL, 0)) return FAIL; return SUCCEED; }
static char *get_commandline(struct kinfo_proc *proc) { int mib[4], i; size_t sz; static char *args = NULL; static int args_alloc = 128; if (NULL == args) args = zbx_malloc(args, args_alloc); mib[0] = CTL_KERN; mib[1] = KERN_PROC; mib[2] = KERN_PROC_ARGS; mib[3] = proc->ZBX_PROC_PID; retry: sz = (size_t)args_alloc; if (-1 == sysctl(mib, 4, args, &sz, NULL, 0)) { if (errno == ENOMEM) { args_alloc *= 2; args = zbx_realloc(args, args_alloc); goto retry; } return NULL; } for (i = 0; i < (int)(sz - 1); i++) if (args[i] == '\0') args[i] = ' '; if (sz == 0) zbx_strlcpy(args, proc->ZBX_PROC_COMM, args_alloc); return args; }
static int zbx_check_user_parameter(const char *param, char *error, int max_error_len) { const char suppressed_chars[] = "\\'\"`*?[]{}~$!&;()<>|#@\n", *c; char *buf = NULL; size_t buf_alloc = 128, buf_offset = 0; if (0 != CONFIG_UNSAFE_USER_PARAMETERS) return SUCCEED; for (c = suppressed_chars; '\0' != *c; c++) { if (NULL == strchr(param, *c)) continue; buf = zbx_malloc(buf, buf_alloc); for (c = suppressed_chars; '\0' != *c; c++) { if (c != suppressed_chars) zbx_strcpy_alloc(&buf, &buf_alloc, &buf_offset, ", "); if (0 != isprint(*c)) zbx_chrcpy_alloc(&buf, &buf_alloc, &buf_offset, *c); else zbx_snprintf_alloc(&buf, &buf_alloc, &buf_offset, "0x%02x", *c); } zbx_snprintf(error, max_error_len, "special characters \"%s\" are not allowed in the parameters", buf); zbx_free(buf); return FAIL; } return SUCCEED; }