1 /*
2 ** Zabbix
3 ** Copyright (C) 2001-2021 Zabbix SIA
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 **/
19 
20 #include "sysinfo.h"
21 #include "zbxalgo.h"
22 #include "zbxjson.h"
23 #include "cpustat.h"
24 
get_cpu_status_string(int status)25 static const char	*get_cpu_status_string(int status)
26 {
27 	switch (status)
28 	{
29 		case ZBX_CPU_STATUS_ONLINE:
30 			return "online";
31 		case ZBX_CPU_STATUS_OFFLINE:
32 			return "offline";
33 		case ZBX_CPU_STATUS_UNKNOWN:
34 			return "unknown";
35 	}
36 
37 	return NULL;
38 }
39 
SYSTEM_CPU_DISCOVERY(AGENT_REQUEST * request,AGENT_RESULT * result)40 int	SYSTEM_CPU_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result)
41 {
42 	zbx_vector_uint64_pair_t	cpus;
43 	struct zbx_json			json;
44 	int				i, ret = SYSINFO_RET_FAIL;
45 
46 	ZBX_UNUSED(request);
47 
48 	zbx_vector_uint64_pair_create(&cpus);
49 
50 	if (SUCCEED != get_cpus(&cpus))
51 	{
52 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Collector is not started."));
53 		goto out;
54 	}
55 
56 	zbx_json_init(&json, ZBX_JSON_STAT_BUF_LEN);
57 	zbx_json_addarray(&json, ZBX_PROTO_TAG_DATA);
58 
59 	for (i = 0; i < cpus.values_num; i++)
60 	{
61 		zbx_json_addobject(&json, NULL);
62 
63 		zbx_json_adduint64(&json, "{#CPU.NUMBER}", cpus.values[i].first);
64 		zbx_json_addstring(&json, "{#CPU.STATUS}", get_cpu_status_string((int)cpus.values[i].second),
65 				ZBX_JSON_TYPE_STRING);
66 
67 		zbx_json_close(&json);
68 	}
69 
70 	zbx_json_close(&json);
71 	SET_STR_RESULT(result, zbx_strdup(result->str, json.buffer));
72 
73 	zbx_json_free(&json);
74 
75 	ret = SYSINFO_RET_OK;
76 out:
77 	zbx_vector_uint64_pair_destroy(&cpus);
78 
79 	return ret;
80 }
81