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 #ifndef ZABBIX_SYSINFO_H
21 #define ZABBIX_SYSINFO_H
22 
23 #include "common.h"
24 #include "module.h"
25 
26 /* CHECK RESULT */
27 
28 #define ISSET_UI64(res)	((res)->type & AR_UINT64)
29 #define ISSET_DBL(res)	((res)->type & AR_DOUBLE)
30 #define ISSET_STR(res)	((res)->type & AR_STRING)
31 #define ISSET_TEXT(res)	((res)->type & AR_TEXT)
32 #define ISSET_LOG(res)	((res)->type & AR_LOG)
33 #define ISSET_MSG(res)	((res)->type & AR_MESSAGE)
34 #define ISSET_META(res)	((res)->type & AR_META)
35 
36 #define ISSET_VALUE(res)	((res)->type & (AR_UINT64 | AR_DOUBLE | AR_STRING | AR_TEXT | AR_LOG))
37 
38 /* UNSET RESULT */
39 
40 #define UNSET_UI64_RESULT(res)						\
41 									\
42 do									\
43 {									\
44 	(res)->type &= ~AR_UINT64;					\
45 	(res)->ui64 = (zbx_uint64_t)0;					\
46 }									\
47 while (0)
48 
49 #define UNSET_DBL_RESULT(res)						\
50 									\
51 do									\
52 {									\
53 	(res)->type &= ~AR_DOUBLE;					\
54 	(res)->dbl = (double)0;						\
55 }									\
56 while (0)
57 
58 #define UNSET_STR_RESULT(res)						\
59 									\
60 do									\
61 {									\
62 	if ((res)->type & AR_STRING)					\
63 	{								\
64 		zbx_free((res)->str);					\
65 		(res)->type &= ~AR_STRING;				\
66 	}								\
67 }									\
68 while (0)
69 
70 #define UNSET_TEXT_RESULT(res)						\
71 									\
72 do									\
73 {									\
74 	if ((res)->type & AR_TEXT)					\
75 	{								\
76 		zbx_free((res)->text);					\
77 		(res)->type &= ~AR_TEXT;				\
78 	}								\
79 }									\
80 while (0)
81 
82 #define UNSET_LOG_RESULT(res)						\
83 									\
84 do									\
85 {									\
86 	if ((res)->type & AR_LOG)					\
87 	{								\
88 		zbx_log_free((res)->log);				\
89 		(res)->type &= ~AR_LOG;					\
90 	}								\
91 }									\
92 while (0)
93 
94 #define UNSET_MSG_RESULT(res)						\
95 									\
96 do									\
97 {									\
98 	if ((res)->type & AR_MESSAGE)					\
99 	{								\
100 		zbx_free((res)->msg);					\
101 		(res)->type &= ~AR_MESSAGE;				\
102 	}								\
103 }									\
104 while (0)
105 
106 #define UNSET_RESULT_EXCLUDING(res, exc_type) 					\
107 										\
108 do										\
109 {										\
110 	if (!(exc_type & AR_UINT64))	UNSET_UI64_RESULT(res);			\
111 	if (!(exc_type & AR_DOUBLE))	UNSET_DBL_RESULT(res);			\
112 	if (!(exc_type & AR_STRING))	UNSET_STR_RESULT(res);			\
113 	if (!(exc_type & AR_TEXT))	UNSET_TEXT_RESULT(res);			\
114 	if (!(exc_type & AR_LOG))	UNSET_LOG_RESULT(res);			\
115 	if (!(exc_type & AR_MESSAGE))	UNSET_MSG_RESULT(res);			\
116 }										\
117 while (0)
118 
119 /* RETRIEVE RESULT VALUE */
120 
121 #define GET_UI64_RESULT(res)	((zbx_uint64_t *)get_result_value_by_type(res, AR_UINT64))
122 #define GET_DBL_RESULT(res)	((double *)get_result_value_by_type(res, AR_DOUBLE))
123 #define GET_STR_RESULT(res)	((char **)get_result_value_by_type(res, AR_STRING))
124 #define GET_TEXT_RESULT(res)	((char **)get_result_value_by_type(res, AR_TEXT))
125 #define GET_LOG_RESULT(res)	((zbx_log_t *)get_result_value_by_type(res, AR_LOG))
126 #define GET_MSG_RESULT(res)	((char **)get_result_value_by_type(res, AR_MESSAGE))
127 
128 void    *get_result_value_by_type(AGENT_RESULT *result, int require_type);
129 
130 #define ZBX_FLOAT_PRECISION	0.0001
131 
132 extern int	CONFIG_ENABLE_REMOTE_COMMANDS;
133 extern int	CONFIG_LOG_REMOTE_COMMANDS;
134 extern int	CONFIG_UNSAFE_USER_PARAMETERS;
135 
136 /* collector */
137 #define MAX_COLLECTOR_HISTORY	(15 * SEC_PER_MIN + 1)
138 #define ZBX_AVG1		0
139 #define ZBX_AVG5		1
140 #define ZBX_AVG15		2
141 #define ZBX_AVG_COUNT		3
142 
143 #ifdef _WINDOWS
144 #	define MAX_COLLECTOR_PERIOD	(15 * SEC_PER_MIN)
145 #endif
146 
147 #define ZBX_CPU_STATE_USER	0
148 #define ZBX_CPU_STATE_SYSTEM	1
149 #define ZBX_CPU_STATE_NICE	2
150 #define ZBX_CPU_STATE_IDLE	3
151 #define ZBX_CPU_STATE_INTERRUPT	4
152 #define ZBX_CPU_STATE_IOWAIT	5
153 #define ZBX_CPU_STATE_SOFTIRQ	6
154 #define ZBX_CPU_STATE_STEAL	7
155 #define ZBX_CPU_STATE_GCPU	8
156 #define ZBX_CPU_STATE_GNICE	9
157 #define ZBX_CPU_STATE_COUNT	10
158 
159 #define ZBX_PROC_STAT_ALL	0
160 #define ZBX_PROC_STAT_RUN	1
161 #define ZBX_PROC_STAT_SLEEP	2
162 #define ZBX_PROC_STAT_ZOMB	3
163 
164 #define ZBX_DO_SUM		0
165 #define ZBX_DO_MAX		1
166 #define ZBX_DO_MIN		2
167 #define ZBX_DO_AVG		3
168 #define ZBX_DO_ONE		4
169 
170 #define ZBX_DSTAT_TYPE_SECT	0
171 #define ZBX_DSTAT_TYPE_OPER	1
172 #define ZBX_DSTAT_TYPE_BYTE	2
173 #define ZBX_DSTAT_TYPE_SPS	3
174 #define ZBX_DSTAT_TYPE_OPS	4
175 #define ZBX_DSTAT_TYPE_BPS	5
176 
177 /* disk statistics */
178 #define ZBX_DSTAT_R_SECT	0
179 #define ZBX_DSTAT_R_OPER	1
180 #define ZBX_DSTAT_R_BYTE	2
181 #define ZBX_DSTAT_W_SECT	3
182 #define ZBX_DSTAT_W_OPER	4
183 #define ZBX_DSTAT_W_BYTE	5
184 #define ZBX_DSTAT_MAX		6
185 int	get_diskstat(const char *devname, zbx_uint64_t *dstat);
186 
187 /* flags for process */
188 #define PROCESS_LOCAL_COMMAND	0x1
189 #define PROCESS_MODULE_COMMAND	0x2
190 #define PROCESS_WITH_ALIAS	0x4
191 
192 void	init_metrics();
193 int	add_metric(ZBX_METRIC *metric, char *error, size_t max_error_len);
194 void	free_metrics();
195 
196 int	process(const char *in_command, unsigned flags, AGENT_RESULT *result);
197 
198 int	add_user_parameter(const char *key, char *command, char *error, size_t max_error_len);
199 int	add_user_module(const char *key, int (*function)());
200 void	test_parameters();
201 void	test_parameter(const char *key);
202 
203 void	init_result(AGENT_RESULT *result);
204 void	zbx_log_free(zbx_log_t *log);
205 void	free_result(AGENT_RESULT *result);
206 
207 void	init_request(AGENT_REQUEST *request);
208 void	free_request(AGENT_REQUEST *request);
209 
210 int	parse_item_key(const char *itemkey, AGENT_REQUEST *request);
211 
212 void	unquote_key_param(char *param);
213 int	quote_key_param(char **param, int forced);
214 
215 int	set_result_type(AGENT_RESULT *result, int value_type, int data_type, char *c);
216 void	set_result_meta(AGENT_RESULT *result, zbx_uint64_t lastlogsize, int mtime);
217 
218 #ifdef HAVE_KSTAT_H
219 zbx_uint64_t	get_kstat_numeric_value(const kstat_named_t *kn);
220 #endif
221 
222 /* external system functions */
223 
224 int	GET_SENSOR(AGENT_REQUEST *request, AGENT_RESULT *result);
225 int	KERNEL_MAXFILES(AGENT_REQUEST *request, AGENT_RESULT *result);
226 int	KERNEL_MAXPROC(AGENT_REQUEST *request, AGENT_RESULT *result);
227 
228 #ifdef ZBX_PROCSTAT_COLLECTOR
229 int	PROC_CPU_UTIL(AGENT_REQUEST *request, AGENT_RESULT *result);
230 #endif
231 
232 int	PROC_MEM(AGENT_REQUEST *request, AGENT_RESULT *result);
233 int	PROC_NUM(AGENT_REQUEST *request, AGENT_RESULT *result);
234 int	NET_IF_IN(AGENT_REQUEST *request, AGENT_RESULT *result);
235 int	NET_IF_OUT(AGENT_REQUEST *request, AGENT_RESULT *result);
236 int	NET_IF_TOTAL(AGENT_REQUEST *request, AGENT_RESULT *result);
237 int	NET_IF_COLLISIONS(AGENT_REQUEST *request, AGENT_RESULT *result);
238 int	NET_IF_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result);
239 int	NET_TCP_LISTEN(AGENT_REQUEST *request, AGENT_RESULT *result);
240 int	NET_UDP_LISTEN(AGENT_REQUEST *request, AGENT_RESULT *result);
241 int	SYSTEM_CPU_SWITCHES(AGENT_REQUEST *request, AGENT_RESULT *result);
242 int	SYSTEM_CPU_INTR(AGENT_REQUEST *request, AGENT_RESULT *result);
243 int	SYSTEM_CPU_LOAD(AGENT_REQUEST *request, AGENT_RESULT *result);
244 int	SYSTEM_CPU_UTIL(AGENT_REQUEST *request, AGENT_RESULT *result);
245 int	SYSTEM_CPU_NUM(AGENT_REQUEST *request, AGENT_RESULT *result);
246 int	SYSTEM_CPU_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result);
247 int	SYSTEM_HOSTNAME(AGENT_REQUEST *request, AGENT_RESULT *result);
248 int	SYSTEM_HW_CHASSIS(AGENT_REQUEST *request, AGENT_RESULT *result);
249 int	SYSTEM_HW_CPU(AGENT_REQUEST *request, AGENT_RESULT *result);
250 int	SYSTEM_HW_DEVICES(AGENT_REQUEST *request, AGENT_RESULT *result);
251 int	SYSTEM_HW_MACADDR(AGENT_REQUEST *request, AGENT_RESULT *result);
252 int	SYSTEM_SW_ARCH(AGENT_REQUEST *request, AGENT_RESULT *result);
253 int	SYSTEM_SW_OS(AGENT_REQUEST *request, AGENT_RESULT *result);
254 int	SYSTEM_SW_PACKAGES(AGENT_REQUEST *request, AGENT_RESULT *result);
255 int	SYSTEM_SWAP_IN(AGENT_REQUEST *request, AGENT_RESULT *result);
256 int	SYSTEM_SWAP_OUT(AGENT_REQUEST *request, AGENT_RESULT *result);
257 int	SYSTEM_SWAP_SIZE(AGENT_REQUEST *request, AGENT_RESULT *result);
258 int	SYSTEM_UPTIME(AGENT_REQUEST *request, AGENT_RESULT *result);
259 int	SYSTEM_UNAME(AGENT_REQUEST *request, AGENT_RESULT *result);
260 int	SYSTEM_BOOTTIME(AGENT_REQUEST *request, AGENT_RESULT *result);
261 int	VFS_DEV_READ(AGENT_REQUEST *request, AGENT_RESULT *result);
262 int	VFS_DEV_WRITE(AGENT_REQUEST *request, AGENT_RESULT *result);
263 int	VFS_FS_INODE(AGENT_REQUEST *request, AGENT_RESULT *result);
264 int	VFS_FS_SIZE(AGENT_REQUEST *request, AGENT_RESULT *result);
265 int	VFS_FS_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result);
266 int	VM_MEMORY_SIZE(AGENT_REQUEST *request, AGENT_RESULT *result);
267 
268 #ifdef _WINDOWS
269 int	USER_PERF_COUNTER(AGENT_REQUEST *request, AGENT_RESULT *result);
270 int	PERF_COUNTER(AGENT_REQUEST *request, AGENT_RESULT *result);
271 int	SERVICE_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result);
272 int	SERVICE_INFO(AGENT_REQUEST *request, AGENT_RESULT *result);
273 int	SERVICE_STATE(AGENT_REQUEST *request, AGENT_RESULT *result);
274 int	SERVICES(AGENT_REQUEST *request, AGENT_RESULT *result);
275 int	PROC_INFO(AGENT_REQUEST *request, AGENT_RESULT *result);
276 int	NET_IF_LIST(AGENT_REQUEST *request, AGENT_RESULT *result);
277 int	WMI_GET(AGENT_REQUEST *request, AGENT_RESULT *result);
278 int	VM_VMEMORY_SIZE(AGENT_REQUEST *request, AGENT_RESULT *result);
279 #endif
280 
281 #ifdef _AIX
282 int	SYSTEM_STAT(AGENT_REQUEST *request, AGENT_RESULT *result);
283 #endif
284 
285 typedef int (*zbx_metric_func_t)(AGENT_REQUEST *request, AGENT_RESULT *result);
286 
287 typedef struct
288 {
289 	const char	*mode;
290 	int		(*function)();
291 }
292 MODE_FUNCTION;
293 
294 int	zbx_execute_threaded_metric(zbx_metric_func_t metric_func, AGENT_REQUEST *request, AGENT_RESULT *result);
295 
296 /* the fields used by proc queries */
297 #define ZBX_SYSINFO_PROC_NONE		0x0000
298 #define ZBX_SYSINFO_PROC_PID		0x0001
299 #define ZBX_SYSINFO_PROC_NAME		0x0002
300 #define ZBX_SYSINFO_PROC_CMDLINE	0x0004
301 #define ZBX_SYSINFO_PROC_USER		0x0008
302 
303 #ifdef _WINDOWS
304 #define ZBX_MUTEX_ALL_ALLOW		0
305 #define ZBX_MUTEX_THREAD_DENIED		1
306 #define ZBX_MUTEX_LOGGING_DENIED	2
307 zbx_uint32_t get_thread_global_mutex_flag();
308 #endif
309 
310 #endif
311