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 "common.h"
21 #include "sysinfo.h"
22 #include "stats.h"
23 #include "log.h"
24 
SYSTEM_CPU_NUM(AGENT_REQUEST * request,AGENT_RESULT * result)25 int	SYSTEM_CPU_NUM(AGENT_REQUEST *request, AGENT_RESULT *result)
26 {
27 	char			*type;
28 	struct pst_dynamic	dyn;
29 
30 	if (1 < request->nparam)
31 	{
32 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters."));
33 		return SYSINFO_RET_FAIL;
34 	}
35 
36 	type = get_rparam(request, 0);
37 
38 	/* only "online" (default) for parameter "type" is supported */
39 	if (NULL != type && '\0' != *type && 0 != strcmp(type, "online"))
40 	{
41 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter."));
42 		return SYSINFO_RET_FAIL;
43 	}
44 
45 	if (-1 == pstat_getdynamic(&dyn, sizeof(dyn), 1, 0))
46 	{
47 		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno)));
48 		return SYSINFO_RET_FAIL;
49 	}
50 
51 	SET_UI64_RESULT(result, dyn.psd_proc_cnt);
52 
53 	return SYSINFO_RET_OK;
54 }
55 
SYSTEM_CPU_UTIL(AGENT_REQUEST * request,AGENT_RESULT * result)56 int	SYSTEM_CPU_UTIL(AGENT_REQUEST *request, AGENT_RESULT *result)
57 {
58 	char	*tmp;
59 	int	cpu_num, state, mode;
60 
61 	if (3 < request->nparam)
62 	{
63 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters."));
64 		return SYSINFO_RET_FAIL;
65 	}
66 
67 	tmp = get_rparam(request, 0);
68 
69 	if (NULL == tmp || '\0' == *tmp || 0 == strcmp(tmp, "all"))
70 		cpu_num = ZBX_CPUNUM_ALL;
71 	else if (SUCCEED != is_uint31_1(tmp, &cpu_num))
72 	{
73 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter."));
74 		return SYSINFO_RET_FAIL;
75 	}
76 
77 	tmp = get_rparam(request, 1);
78 
79 	if (NULL == tmp || '\0' == *tmp || 0 == strcmp(tmp, "user"))
80 		state = ZBX_CPU_STATE_USER;
81 	else if (0 == strcmp(tmp, "nice"))
82 		state = ZBX_CPU_STATE_NICE;
83 	else if (0 == strcmp(tmp, "system"))
84 		state = ZBX_CPU_STATE_SYSTEM;
85 	else if (0 == strcmp(tmp, "idle"))
86 		state = ZBX_CPU_STATE_IDLE;
87 	else
88 	{
89 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
90 		return SYSINFO_RET_FAIL;
91 	}
92 
93 	tmp = get_rparam(request, 2);
94 
95 	if (NULL == tmp || '\0' == *tmp || 0 == strcmp(tmp, "avg1"))
96 		mode = ZBX_AVG1;
97 	else if (0 == strcmp(tmp, "avg5"))
98 		mode = ZBX_AVG5;
99 	else if (0 == strcmp(tmp, "avg15"))
100 		mode = ZBX_AVG15;
101 	else
102 	{
103 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));
104 		return SYSINFO_RET_FAIL;
105 	}
106 
107 	return get_cpustat(result, cpu_num, state, mode);
108 }
109 
SYSTEM_CPU_LOAD(AGENT_REQUEST * request,AGENT_RESULT * result)110 int	SYSTEM_CPU_LOAD(AGENT_REQUEST *request, AGENT_RESULT *result)
111 {
112 	char			*tmp;
113 	struct pst_dynamic	dyn;
114 	double			value;
115 	int			per_cpu = 1;
116 
117 	if (2 < request->nparam)
118 	{
119 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters."));
120 		return SYSINFO_RET_FAIL;
121 	}
122 
123 	tmp = get_rparam(request, 0);
124 
125 	if (NULL == tmp || '\0' == *tmp || 0 == strcmp(tmp, "all"))
126 		per_cpu = 0;
127 	else if (0 != strcmp(tmp, "percpu"))
128 	{
129 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter."));
130 		return SYSINFO_RET_FAIL;
131 	}
132 
133 	if (-1 == pstat_getdynamic(&dyn, sizeof(dyn), 1, 0))
134 	{
135 		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno)));
136 		return SYSINFO_RET_FAIL;
137 	}
138 
139 	tmp = get_rparam(request, 1);
140 
141 	if (NULL == tmp || '\0' == *tmp || 0 == strcmp(tmp, "avg1"))
142 		value = dyn.psd_avg_1_min;
143 	else if (0 == strcmp(tmp, "avg5"))
144 		value = dyn.psd_avg_5_min;
145 	else if (0 == strcmp(tmp, "avg15"))
146 		value = dyn.psd_avg_15_min;
147 	else
148 	{
149 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
150 		return SYSINFO_RET_FAIL;
151 	}
152 
153 	if (1 == per_cpu)
154 	{
155 		if (0 >= dyn.psd_proc_cnt)
156 		{
157 			SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot obtain number of CPUs."));
158 			return SYSINFO_RET_FAIL;
159 		}
160 		value /= dyn.psd_proc_cnt;
161 	}
162 
163 	SET_DBL_RESULT(result, value);
164 
165 	return SYSINFO_RET_OK;
166 }
167