/****************************************************************************** * * * Function: collector_thread * * * * Purpose: Collect system information * * * * Author: Eugene Grigorjev * * * ******************************************************************************/ ZBX_THREAD_ENTRY(collector_thread, args) { assert(args); zabbix_log(LOG_LEVEL_WARNING, "agent #%d started [collector]", ((zbx_thread_args_t *)args)->thread_num); zbx_free(args); if (SUCCEED != init_cpu_collector(&(collector->cpus))) free_cpu_collector(&(collector->cpus)); while (ZBX_IS_RUNNING()) { zbx_setproctitle("collector [processing data]"); #ifdef _WINDOWS collect_perfstat(); #else if (0 != CPU_COLLECTOR_STARTED(collector)) collect_cpustat(&(collector->cpus)); if (0 != DISKDEVICE_COLLECTOR_STARTED(collector)) collect_stats_diskdevices(); #endif #ifdef _AIX if (1 == collector->vmstat.enabled) collect_vmstat_data(&collector->vmstat); #endif zbx_setproctitle("collector [sleeping for 1 seconds]"); zbx_sleep(1); } if (CPU_COLLECTOR_STARTED(collector)) free_cpu_collector(&(collector->cpus)); #ifdef _WINDOWS free_perf_collector(); /* cpu_collector must be freed before perf_collector is freed */ #endif zabbix_log(LOG_LEVEL_INFORMATION, "zabbix_agentd collector stopped"); ZBX_DO_EXIT(); zbx_thread_exit(0); }
int SYSTEM_CPU_UTIL(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { char tmp[32]; int cpu_num; if (!CPU_COLLECTOR_STARTED(collector)) { SET_MSG_RESULT(result, strdup("Collector is not started!")); return SYSINFO_RET_OK; } if (num_param(param) > 3) return SYSINFO_RET_FAIL; if (0 != get_param(param, 1, tmp, sizeof(tmp))) *tmp = '\0'; if ('\0' == *tmp || 0 == strcmp(tmp, "all")) /* default parameter */ cpu_num = 0; else { cpu_num = atoi(tmp) + 1; if (cpu_num < 1 || cpu_num > collector->cpus.count) return SYSINFO_RET_FAIL; } if (0 != get_param(param, 2, tmp, sizeof(tmp))) *tmp = '\0'; /* only 'system' parameter supported */ if ('\0' != *tmp && 0 != strcmp(tmp, "system")) /* default parameter */ return SYSINFO_RET_FAIL; if (0 != get_param(param, 3, tmp, sizeof(tmp))) *tmp = '\0'; if ('\0' == *tmp || 0 == strcmp(tmp, "avg1")) /* default parameter */ SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].util1); else if (0 == strcmp(tmp, "avg5")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].util5); else if (0 == strcmp(tmp, "avg15")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].util15); else return SYSINFO_RET_FAIL; return SYSINFO_RET_OK; }
int SYSTEM_CPU_UTIL(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { char cpuname[MAX_STRING_LEN]; char type[MAX_STRING_LEN]; char mode[MAX_STRING_LEN]; int cpu_num = 0; assert(result); init_result(result); if(num_param(param) > 3) { return SYSINFO_RET_FAIL; } if(get_param(param, 1, cpuname, sizeof(cpuname)) != 0) { cpuname[0] = '\0'; } if(cpuname[0] == '\0') { /* default parameter */ zbx_snprintf(cpuname, sizeof(cpuname), "all"); } if(get_param(param, 2, type, sizeof(type)) != 0) { type[0] = '\0'; } if(type[0] == '\0') { /* default parameter */ zbx_snprintf(type, sizeof(type), "user"); } if(get_param(param, 3, mode, sizeof(mode)) != 0) { mode[0] = '\0'; } if(mode[0] == '\0') { /* default parameter */ zbx_snprintf(mode, sizeof(mode), "avg1"); } if ( !CPU_COLLECTOR_STARTED(collector) ) { SET_MSG_RESULT(result, strdup("Collector is not started!")); return SYSINFO_RET_OK; } if(strcmp(cpuname,"all") == 0) { cpu_num = 0; } else { cpu_num = atoi(cpuname)+1; if ((cpu_num < 1) || (cpu_num > collector->cpus.count)) return SYSINFO_RET_FAIL; } if( 0 == strcmp(type,"idle")) { if( 0 == strcmp(mode,"avg1")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].idle1) else if( 0 == strcmp(mode,"avg5")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].idle5) else if( 0 == strcmp(mode,"avg15")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].idle15) else return SYSINFO_RET_FAIL; } else if( 0 == strcmp(type,"nice"))
int SYSTEM_CPU_LOAD(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { char cpuname[10], mode[10]; if(num_param(param) > 2) { return SYSINFO_RET_FAIL; } if(get_param(param, 1, cpuname, sizeof(cpuname)) != 0) { cpuname[0] = '\0'; } if(cpuname[0] == '\0') { /* default parameter */ zbx_snprintf(cpuname, sizeof(cpuname), "all"); } if(get_param(param, 2, mode, sizeof(mode)) != 0) { mode[0] = '\0'; } if(mode[0] == '\0') { /* default parameter */ zbx_snprintf(mode, sizeof(mode), "avg1"); } if(strcmp(cpuname,"all") != 0) { return SYSINFO_RET_FAIL; } if ( !CPU_COLLECTOR_STARTED(collector) ) { SET_MSG_RESULT(result, strdup("Collector is not started!")); return SYSINFO_RET_OK; } if(strcmp(mode,"avg1") == 0) { SET_DBL_RESULT(result, collector->cpus.load1); } else if(strcmp(mode,"avg5") == 0) { SET_DBL_RESULT(result, collector->cpus.load5); } else if(strcmp(mode,"avg15") == 0) { SET_DBL_RESULT(result, collector->cpus.load15); } else { return SYSINFO_RET_FAIL; } return SYSINFO_RET_OK; }
int get_cpustat(AGENT_RESULT *result, int cpu_num, int state, int mode) { int i, time, idx_curr, idx_base; zbx_uint64_t counter, total = 0; ZBX_SINGLE_CPU_STAT_DATA *cpu; if (0 > state || state >= ZBX_CPU_STATE_COUNT) return SYSINFO_RET_FAIL; switch (mode) { case ZBX_AVG1: time = SEC_PER_MIN; break; case ZBX_AVG5: time = 5 * SEC_PER_MIN; break; case ZBX_AVG15: time = 15 * SEC_PER_MIN; break; default: return SYSINFO_RET_FAIL; } if (!CPU_COLLECTOR_STARTED(collector)) { SET_MSG_RESULT(result, strdup("Collector is not started!")); return SYSINFO_RET_FAIL; } if (NULL == (cpu = get_cpustat_by_num(&collector->cpus, cpu_num))) return SYSINFO_RET_FAIL; if (0 == cpu->h_count) { SET_DBL_RESULT(result, 0); return SYSINFO_RET_OK; } LOCK_CPUSTATS; if (MAX_COLLECTOR_HISTORY <= (idx_curr = (cpu->h_first + cpu->h_count - 1))) idx_curr -= MAX_COLLECTOR_HISTORY; if (SYSINFO_RET_FAIL == cpu->h_status[idx_curr]) { UNLOCK_CPUSTATS; return SYSINFO_RET_FAIL; } if (1 == cpu->h_count) { for (i = 0; i < ZBX_CPU_STATE_COUNT; i++) total += cpu->h_counter[i][idx_curr]; counter = cpu->h_counter[state][idx_curr]; } else { if (0 > (idx_base = idx_curr - MIN(cpu->h_count - 1, time))) idx_base += MAX_COLLECTOR_HISTORY; while (SYSINFO_RET_OK != cpu->h_status[idx_base]) if (MAX_COLLECTOR_HISTORY == ++idx_base) idx_base -= MAX_COLLECTOR_HISTORY; for (i = 0; i < ZBX_CPU_STATE_COUNT; i++) total += cpu->h_counter[i][idx_curr] - cpu->h_counter[i][idx_base]; counter = cpu->h_counter[state][idx_curr] - cpu->h_counter[state][idx_base]; } UNLOCK_CPUSTATS; SET_DBL_RESULT(result, 0 == total ? 0 : 100. * (double)counter / (double)total); return SYSINFO_RET_OK; }
int SYSTEM_CPU_UTIL(AGENT_REQUEST *request, AGENT_RESULT *result) { char *tmp, *error = NULL; int cpu_num, ret = FAIL; double value; if (0 == CPU_COLLECTOR_STARTED(collector)) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Collector is not started.")); return SYSINFO_RET_FAIL; } if (3 < request->nparam) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters.")); return SYSINFO_RET_FAIL; } if (NULL == (tmp = get_rparam(request, 0)) || '\0' == *tmp || 0 == strcmp(tmp, "all")) cpu_num = 0; else if (SUCCEED != is_uint_range(tmp, &cpu_num, 0, collector->cpus.count - 1)) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter.")); return SYSINFO_RET_FAIL; } else cpu_num++; /* only "system" (default) for parameter "type" is supported */ if (NULL != (tmp = get_rparam(request, 1)) && '\0' != *tmp && 0 != strcmp(tmp, "system")) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter.")); return SYSINFO_RET_FAIL; } if (NULL == (tmp = get_rparam(request, 2)) || '\0' == *tmp || 0 == strcmp(tmp, "avg1")) { ret = get_perf_counter_value(collector->cpus.cpu_counter[cpu_num], 1 * SEC_PER_MIN, &value, &error); } else if (0 == strcmp(tmp, "avg5")) { ret = get_perf_counter_value(collector->cpus.cpu_counter[cpu_num], 5 * SEC_PER_MIN, &value, &error); } else if (0 == strcmp(tmp, "avg15")) { ret = get_perf_counter_value(collector->cpus.cpu_counter[cpu_num], 15 * SEC_PER_MIN, &value, &error); } else { SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter.")); return SYSINFO_RET_FAIL; } if (SUCCEED == ret) { SET_DBL_RESULT(result, value); return SYSINFO_RET_OK; } SET_MSG_RESULT(result, NULL != error ? error : zbx_strdup(NULL, "Cannot obtain performance information from collector.")); return SYSINFO_RET_FAIL; }
int SYSTEM_CPU_LOAD(AGENT_REQUEST *request, AGENT_RESULT *result) { char *tmp, *error = NULL; double value; int cpu_num, ret = FAIL; if (0 == CPU_COLLECTOR_STARTED(collector)) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Collector is not started.")); return SYSINFO_RET_FAIL; } if (2 < request->nparam) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters.")); return SYSINFO_RET_FAIL; } if (NULL == (tmp = get_rparam(request, 0)) || '\0' == *tmp || 0 == strcmp(tmp, "all")) { cpu_num = 1; } else if (0 == strcmp(tmp, "percpu")) { if (0 >= (cpu_num = get_cpu_num())) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot obtain number of CPUs.")); return SYSINFO_RET_FAIL; } } else { SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter.")); return SYSINFO_RET_FAIL; } if (NULL == (tmp = get_rparam(request, 1)) || '\0' == *tmp || 0 == strcmp(tmp, "avg1")) { ret = get_perf_counter_value(collector->cpus.queue_counter, 1 * SEC_PER_MIN, &value, &error); } else if (0 == strcmp(tmp, "avg5")) { ret = get_perf_counter_value(collector->cpus.queue_counter, 5 * SEC_PER_MIN, &value, &error); } else if (0 == strcmp(tmp, "avg15")) { ret = get_perf_counter_value(collector->cpus.queue_counter, 15 * SEC_PER_MIN, &value, &error); } else { SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter.")); return SYSINFO_RET_FAIL; } if (SUCCEED == ret) { SET_DBL_RESULT(result, value / cpu_num); return SYSINFO_RET_OK; } SET_MSG_RESULT(result, NULL != error ? error : zbx_strdup(NULL, "Cannot obtain performance information from collector.")); return SYSINFO_RET_FAIL; }
int SYSTEM_CPU_UTIL(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { char tmp[32], type[32]; int cpu_num, mode; assert(result); init_result(result); if (!CPU_COLLECTOR_STARTED(collector)) { SET_MSG_RESULT(result, strdup("Collector is not started!")); return SYSINFO_RET_OK; } if (num_param(param) > 3) return SYSINFO_RET_FAIL; if (0 != get_param(param, 1, tmp, sizeof(tmp))) *tmp = '\0'; if ('\0' == *tmp || 0 == strcmp(tmp, "all")) /* default parameter */ cpu_num = 0; else { cpu_num = atoi(tmp); if (cpu_num < 1 || cpu_num > collector->cpus.count) return SYSINFO_RET_FAIL; } if (0 != get_param(param, 2, type, sizeof(type))) *type = '\0'; if (0 != get_param(param, 3, tmp, sizeof(tmp))) *tmp = '\0'; if ('\0' == *tmp || 0 == strcmp(tmp, "avg1")) /* default parameter */ mode = ZBX_AVG1; else if (0 == strcmp(tmp, "avg5")) mode = ZBX_AVG5; else if (0 == strcmp(tmp, "avg15")) mode = ZBX_AVG15; else return SYSINFO_RET_FAIL; if ('\0' == *type || 0 == strcmp(type, "user")) /* default parameter */ SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].user[mode]) else if (0 == strcmp(type, "nice")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].nice[mode]) else if (0 == strcmp(type, "system")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].system[mode]) else if (0 == strcmp(type, "idle")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].idle[mode]) else if (0 == strcmp(type, "iowait")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].iowait[mode]) else if (0 == strcmp(type, "interrupt")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].interrupt[mode]) else if (0 == strcmp(type, "softirq")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].softirq[mode]) else if (0 == strcmp(type, "steal")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].steal[mode]) else return SYSINFO_RET_FAIL; return SYSINFO_RET_OK; }