diff options
Diffstat (limited to 'src/libs/zbxsysinfo/linux/cpu.c')
-rw-r--r-- | src/libs/zbxsysinfo/linux/cpu.c | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/src/libs/zbxsysinfo/linux/cpu.c b/src/libs/zbxsysinfo/linux/cpu.c index be90bb51fd9..5abeb9d7c4b 100644 --- a/src/libs/zbxsysinfo/linux/cpu.c +++ b/src/libs/zbxsysinfo/linux/cpu.c @@ -20,6 +20,47 @@ #include "common.h" #include "sysinfo.h" #include "stats.h" +#include "log.h" +#include "zbxalgo.h" +#include "zbxjson.h" + +int SYSTEM_CPU_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result) +{ + int i; + zbx_vector_uint64_t cpus; + struct zbx_json json; + + zbx_vector_uint64_create(&cpus); + + if (SUCCEED != get_cpu_statuses(&cpus)) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Collector is not started.")); + zbx_vector_uint64_destroy(&cpus); + return SYSINFO_RET_FAIL; + } + + zbx_json_init(&json, ZBX_JSON_STAT_BUF_LEN); + zbx_json_addarray(&json, ZBX_PROTO_TAG_DATA); + + for (i = 0; i < cpus.values_num; i++) + { + zbx_json_addobject(&json, NULL); + + zbx_json_adduint64(&json, "{#CPU.NUMBER}", i); + zbx_json_addstring(&json, "{#CPU.STATUS}", (SYSINFO_RET_OK == cpus.values[i] ? + "online" : "offline"), ZBX_JSON_TYPE_STRING); + + zbx_json_close(&json); + } + + zbx_json_close(&json); + SET_STR_RESULT(result, zbx_strdup(result->str, json.buffer)); + + zbx_json_free(&json); + zbx_vector_uint64_destroy(&cpus); + + return SYSINFO_RET_OK; +} int SYSTEM_CPU_NUM(AGENT_REQUEST *request, AGENT_RESULT *result) { @@ -28,7 +69,10 @@ int SYSTEM_CPU_NUM(AGENT_REQUEST *request, AGENT_RESULT *result) long ncpu; if (1 < request->nparam) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters.")); return SYSINFO_RET_FAIL; + } type = get_rparam(request, 0); @@ -37,10 +81,16 @@ int SYSTEM_CPU_NUM(AGENT_REQUEST *request, AGENT_RESULT *result) else if (0 == strcmp(type, "max")) name = _SC_NPROCESSORS_CONF; else + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter.")); return SYSINFO_RET_FAIL; + } if (-1 == (ncpu = sysconf(name))) + { + SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain number of CPUs: %s", zbx_strerror(errno))); return SYSINFO_RET_FAIL; + } SET_UI64_RESULT(result, ncpu); @@ -53,14 +103,22 @@ int SYSTEM_CPU_UTIL(AGENT_REQUEST *request, AGENT_RESULT *result) int cpu_num, state, mode; if (3 < request->nparam) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters.")); return SYSINFO_RET_FAIL; + } tmp = get_rparam(request, 0); if (NULL == tmp || '\0' == *tmp || 0 == strcmp(tmp, "all")) + { cpu_num = 0; + } else if (SUCCEED != is_uint31_1(tmp, &cpu_num)) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter.")); return SYSINFO_RET_FAIL; + } else cpu_num++; @@ -83,7 +141,10 @@ int SYSTEM_CPU_UTIL(AGENT_REQUEST *request, AGENT_RESULT *result) else if (0 == strcmp(tmp, "steal")) state = ZBX_CPU_STATE_STEAL; else + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter.")); return SYSINFO_RET_FAIL; + } tmp = get_rparam(request, 2); @@ -94,7 +155,10 @@ int SYSTEM_CPU_UTIL(AGENT_REQUEST *request, AGENT_RESULT *result) else if (0 == strcmp(tmp, "avg15")) mode = ZBX_AVG15; else + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter.")); return SYSINFO_RET_FAIL; + } return get_cpustat(result, cpu_num, state, mode); } @@ -106,14 +170,22 @@ int SYSTEM_CPU_LOAD(AGENT_REQUEST *request, AGENT_RESULT *result) double load[ZBX_AVG_COUNT], value; if (2 < request->nparam) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters.")); return SYSINFO_RET_FAIL; + } tmp = get_rparam(request, 0); if (NULL == tmp || '\0' == *tmp || 0 == strcmp(tmp, "all")) + { per_cpu = 0; + } else if (0 != strcmp(tmp, "percpu")) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter.")); return SYSINFO_RET_FAIL; + } tmp = get_rparam(request, 1); @@ -124,17 +196,27 @@ int SYSTEM_CPU_LOAD(AGENT_REQUEST *request, AGENT_RESULT *result) else if (0 == strcmp(tmp, "avg15")) mode = ZBX_AVG15; else + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter.")); return SYSINFO_RET_FAIL; + } if (mode >= getloadavg(load, 3)) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot obtain load average.")); return SYSINFO_RET_FAIL; + } value = load[mode]; if (1 == per_cpu) { if (0 >= (cpu_num = sysconf(_SC_NPROCESSORS_ONLN))) + { + SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain number of CPUs: %s", + zbx_strerror(errno))); return SYSINFO_RET_FAIL; + } value /= cpu_num; } @@ -151,7 +233,10 @@ int SYSTEM_CPU_SWITCHES(AGENT_REQUEST *request, AGENT_RESULT *result) FILE *f; if (NULL == (f = fopen("/proc/stat", "r"))) + { + SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot open /proc/stat: %s", zbx_strerror(errno))); return SYSINFO_RET_FAIL; + } while (NULL != fgets(line, sizeof(line), f)) { @@ -167,6 +252,9 @@ int SYSTEM_CPU_SWITCHES(AGENT_REQUEST *request, AGENT_RESULT *result) } zbx_fclose(f); + if (SYSINFO_RET_FAIL == ret) + SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot find a line with \"ctxt\" in /proc/stat.")); + return ret; } @@ -178,7 +266,10 @@ int SYSTEM_CPU_INTR(AGENT_REQUEST *request, AGENT_RESULT *result) FILE *f; if (NULL == (f = fopen("/proc/stat", "r"))) + { + SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot open /proc/stat: %s", zbx_strerror(errno))); return SYSINFO_RET_FAIL; + } while (NULL != fgets(line, sizeof(line), f)) { @@ -194,5 +285,8 @@ int SYSTEM_CPU_INTR(AGENT_REQUEST *request, AGENT_RESULT *result) } zbx_fclose(f); + if (SYSINFO_RET_FAIL == ret) + SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot find a line with \"intr\" in /proc/stat.")); + return ret; } |