int check_counter_path(char *counterPath) { const char *__function_name = "check_counter_path"; PDH_COUNTER_PATH_ELEMENTS *cpe = NULL; PDH_STATUS status; int is_numeric, ret = FAIL; DWORD dwSize = 0; wchar_t *wcounterPath; wcounterPath = zbx_utf8_to_unicode(counterPath); status = PdhParseCounterPath(wcounterPath, NULL, &dwSize, 0); if (PDH_MORE_DATA == status || ERROR_SUCCESS == status) { cpe = (PDH_COUNTER_PATH_ELEMENTS *)zbx_malloc(cpe, dwSize); } else { zabbix_log(LOG_LEVEL_ERR, "cannot get required buffer size for counter path '%s': %s", counterPath, strerror_from_module(status, L"PDH.DLL")); goto clean; } if (ERROR_SUCCESS != (status = PdhParseCounterPath(wcounterPath, cpe, &dwSize, 0))) { zabbix_log(LOG_LEVEL_ERR, "cannot parse counter path '%s': %s", counterPath, strerror_from_module(status, L"PDH.DLL")); goto clean; } is_numeric = (SUCCEED == _wis_uint(cpe->szObjectName) ? 0x01 : 0); is_numeric |= (SUCCEED == _wis_uint(cpe->szCounterName) ? 0x02 : 0); if (0 != is_numeric) { if (0x01 & is_numeric) cpe->szObjectName = get_counter_name(_wtoi(cpe->szObjectName)); if (0x02 & is_numeric) cpe->szCounterName = get_counter_name(_wtoi(cpe->szCounterName)); if (ERROR_SUCCESS != zbx_PdhMakeCounterPath(__function_name, cpe, counterPath)) goto clean; zabbix_log(LOG_LEVEL_DEBUG, "counter path converted to '%s'", counterPath); } ret = SUCCEED; clean: zbx_free(cpe); zbx_free(wcounterPath); return ret; }
int init_cpu_collector(ZBX_CPUS_STAT_DATA *pcpus) { const char *__function_name = "init_cpu_collector"; int cpu_num, ret = FAIL; #ifdef _WINDOWS TCHAR cpu[8]; char counterPath[PDH_MAX_COUNTER_PATH]; PDH_COUNTER_PATH_ELEMENTS cpe; #else #ifdef HAVE_KSTAT_H kstat_ctl_t *kc; kstat_t *k, *kd; #endif #endif zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); #ifdef _WINDOWS cpe.szMachineName = NULL; cpe.szObjectName = get_counter_name(PCI_PROCESSOR); cpe.szInstanceName = cpu; cpe.szParentInstance = NULL; cpe.dwInstanceIndex = -1; cpe.szCounterName = get_counter_name(PCI_PROCESSOR_TIME); for (cpu_num = 0; cpu_num <= pcpus->count; cpu_num++) { if (0 == cpu_num) zbx_wsnprintf(cpu, sizeof(cpu) / sizeof(TCHAR), TEXT("_Total")); else _itow_s(cpu_num - 1, cpu, sizeof(cpu) / sizeof(TCHAR), 10); if (ERROR_SUCCESS != zbx_PdhMakeCounterPath(__function_name, &cpe, counterPath)) goto clean; if (NULL == (pcpus->cpu_counter[cpu_num] = add_perf_counter(NULL, counterPath, MAX_CPU_HISTORY))) goto clean; } cpe.szObjectName = get_counter_name(PCI_SYSTEM); cpe.szInstanceName = NULL; cpe.szCounterName = get_counter_name(PCI_PROCESSOR_QUEUE_LENGTH); if (ERROR_SUCCESS != zbx_PdhMakeCounterPath(__function_name, &cpe, counterPath)) goto clean; if (NULL == (pcpus->queue_counter = add_perf_counter(NULL, counterPath, MAX_CPU_HISTORY))) goto clean; ret = SUCCEED; clean: #else /* not _WINDOWS */ if (ZBX_MUTEX_ERROR == zbx_mutex_create_force(&cpustats_lock, ZBX_MUTEX_CPUSTATS)) { zbx_error("unable to create mutex for cpu collector"); exit(FAIL); } for (cpu_num = 0; cpu_num <= pcpus->count; cpu_num++) pcpus->cpu[cpu_num].cpu_num = cpu_num; #ifdef HAVE_KSTAT_H /* Solaris */ if (NULL != (kc = kstat_open())) { if (NULL != (k = kstat_lookup(kc, "unix", 0, "kstat_headers")) && -1 != kstat_read(kc, k, NULL)) { int i; for (i = 0, cpu_num = 1; i < k->ks_ndata; i++) { kd = (kstat_t *)k->ks_data; if (0 == strcmp(kd[i].ks_module, "cpu_info")) pcpus->cpu[cpu_num++].cpu_num = kd[i].ks_instance + 1; } } kstat_close(kc); } #endif /* HAVE_KSTAT_H */ ret = SUCCEED; #endif /* _WINDOWS */ zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; }
int init_cpu_collector(ZBX_CPUS_STAT_DATA *pcpus) { const char *__function_name = "init_cpu_collector"; int cpu_num, ret = FAIL; #ifdef _WINDOWS TCHAR cpu[8]; char counterPath[PDH_MAX_COUNTER_PATH]; PDH_COUNTER_PATH_ELEMENTS cpe; #endif zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); #ifdef _WINDOWS cpe.szMachineName = NULL; cpe.szObjectName = get_counter_name(PCI_PROCESSOR); cpe.szInstanceName = cpu; cpe.szParentInstance = NULL; cpe.dwInstanceIndex = -1; cpe.szCounterName = get_counter_name(PCI_PROCESSOR_TIME); for (cpu_num = 0; cpu_num <= pcpus->count; cpu_num++) { if (0 == cpu_num) zbx_wsnprintf(cpu, sizeof(cpu) / sizeof(TCHAR), TEXT("_Total")); else _itow_s(cpu_num - 1, cpu, sizeof(cpu) / sizeof(TCHAR), 10); if (ERROR_SUCCESS != zbx_PdhMakeCounterPath(__function_name, &cpe, counterPath)) goto clean; if (NULL == (pcpus->cpu_counter[cpu_num] = add_perf_counter(NULL, counterPath, MAX_CPU_HISTORY))) goto clean; } cpe.szObjectName = get_counter_name(PCI_SYSTEM); cpe.szInstanceName = NULL; cpe.szCounterName = get_counter_name(PCI_PROCESSOR_QUEUE_LENGTH); if (ERROR_SUCCESS != zbx_PdhMakeCounterPath(__function_name, &cpe, counterPath)) goto clean; if (NULL == (pcpus->queue_counter = add_perf_counter(NULL, counterPath, MAX_CPU_HISTORY))) goto clean; ret = SUCCEED; clean: #else /* not _WINDOWS */ if (ZBX_MUTEX_ERROR == zbx_mutex_create_force(&cpustats_lock, ZBX_MUTEX_CPUSTATS)) { zbx_error("unable to create mutex for cpu collector"); exit(EXIT_FAILURE); } #ifndef HAVE_KSTAT_H for (cpu_num = 0; cpu_num <= pcpus->count; cpu_num++) pcpus->cpu[cpu_num].cpu_num = cpu_num; #else /* Solaris */ /* CPU instance numbers on Solaris can be non-contiguous, we don't know them yet */ pcpus->cpu[0].cpu_num = 0; for (cpu_num = 1; cpu_num <= pcpus->count; cpu_num++) pcpus->cpu[cpu_num].cpu_num = -1; if (NULL == (kc = kstat_open())) { zbx_error("kstat_open() failed"); exit(EXIT_FAILURE); } kc_id = kc->kc_chain_id; if (NULL == ksp) ksp = zbx_malloc(ksp, sizeof(kstat_t *) * pcpus->count); if (SUCCEED != refresh_kstat(pcpus)) { zbx_error("kstat_chain_update() failed"); exit(EXIT_FAILURE); } #endif /* HAVE_KSTAT_H */ ret = SUCCEED; #endif /* _WINDOWS */ zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; }