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)->log = NULL;					\
90 		(res)->type &= ~AR_LOG;					\
91 	}								\
92 }									\
93 while (0)
94 
95 #define UNSET_MSG_RESULT(res)						\
96 									\
97 do									\
98 {									\
99 	if ((res)->type & AR_MESSAGE)					\
100 	{								\
101 		zbx_free((res)->msg);					\
102 		(res)->type &= ~AR_MESSAGE;				\
103 	}								\
104 }									\
105 while (0)
106 
107 /* AR_META is always excluded */
108 #define UNSET_RESULT_EXCLUDING(res, exc_type) 					\
109 										\
110 do										\
111 {										\
112 	if (!(exc_type & AR_UINT64))	UNSET_UI64_RESULT(res);			\
113 	if (!(exc_type & AR_DOUBLE))	UNSET_DBL_RESULT(res);			\
114 	if (!(exc_type & AR_STRING))	UNSET_STR_RESULT(res);			\
115 	if (!(exc_type & AR_TEXT))	UNSET_TEXT_RESULT(res);			\
116 	if (!(exc_type & AR_LOG))	UNSET_LOG_RESULT(res);			\
117 	if (!(exc_type & AR_MESSAGE))	UNSET_MSG_RESULT(res);			\
118 }										\
119 while (0)
120 
121 /* RETRIEVE RESULT VALUE */
122 
123 #define GET_UI64_RESULT(res)	((zbx_uint64_t *)get_result_value_by_type(res, AR_UINT64))
124 #define GET_DBL_RESULT(res)	((double *)get_result_value_by_type(res, AR_DOUBLE))
125 #define GET_STR_RESULT(res)	((char **)get_result_value_by_type(res, AR_STRING))
126 #define GET_TEXT_RESULT(res)	((char **)get_result_value_by_type(res, AR_TEXT))
127 #define GET_LOG_RESULT(res)	((zbx_log_t *)get_result_value_by_type(res, AR_LOG))
128 #define GET_MSG_RESULT(res)	((char **)get_result_value_by_type(res, AR_MESSAGE))
129 
130 void	*get_result_value_by_type(AGENT_RESULT *result, int require_type);
131 
132 #define ZBX_FLOAT_PRECISION	0.0001
133 
134 extern int	CONFIG_ENABLE_REMOTE_COMMANDS;
135 extern int	CONFIG_LOG_REMOTE_COMMANDS;
136 extern int	CONFIG_UNSAFE_USER_PARAMETERS;
137 
138 /* collector */
139 #define MAX_COLLECTOR_HISTORY	(15 * SEC_PER_MIN + 1)
140 #define ZBX_AVG1		0
141 #define ZBX_AVG5		1
142 #define ZBX_AVG15		2
143 #define ZBX_AVG_COUNT		3
144 
145 #if defined(_WINDOWS)
146 #	define MAX_COLLECTOR_PERIOD	(15 * SEC_PER_MIN)
147 #endif
148 
149 #define ZBX_CPU_STATE_USER	0
150 #define ZBX_CPU_STATE_SYSTEM	1
151 #define ZBX_CPU_STATE_NICE	2
152 #define ZBX_CPU_STATE_IDLE	3
153 #define ZBX_CPU_STATE_INTERRUPT	4
154 #define ZBX_CPU_STATE_IOWAIT	5
155 #define ZBX_CPU_STATE_SOFTIRQ	6
156 #define ZBX_CPU_STATE_STEAL	7
157 #define ZBX_CPU_STATE_GCPU	8
158 #define ZBX_CPU_STATE_GNICE	9
159 #define ZBX_CPU_STATE_COUNT	10
160 
161 #define ZBX_PROC_STAT_ALL	0
162 #define ZBX_PROC_STAT_RUN	1
163 #define ZBX_PROC_STAT_SLEEP	2
164 #define ZBX_PROC_STAT_ZOMB	3
165 #define ZBX_PROC_STAT_DISK	4
166 #define ZBX_PROC_STAT_TRACE	5
167 
168 #define ZBX_DO_SUM		0
169 #define ZBX_DO_MAX		1
170 #define ZBX_DO_MIN		2
171 #define ZBX_DO_AVG		3
172 #define ZBX_DO_ONE		4
173 
174 #define ZBX_DSTAT_TYPE_SECT	0
175 #define ZBX_DSTAT_TYPE_OPER	1
176 #define ZBX_DSTAT_TYPE_BYTE	2
177 #define ZBX_DSTAT_TYPE_SPS	3
178 #define ZBX_DSTAT_TYPE_OPS	4
179 #define ZBX_DSTAT_TYPE_BPS	5
180 
181 /* disk statistics */
182 #define ZBX_DSTAT_R_SECT	0
183 #define ZBX_DSTAT_R_OPER	1
184 #define ZBX_DSTAT_R_BYTE	2
185 #define ZBX_DSTAT_W_SECT	3
186 #define ZBX_DSTAT_W_OPER	4
187 #define ZBX_DSTAT_W_BYTE	5
188 #define ZBX_DSTAT_MAX		6
189 int	get_diskstat(const char *devname, zbx_uint64_t *dstat);
190 
191 /* flags for process */
192 #define PROCESS_LOCAL_COMMAND	0x1
193 #define PROCESS_MODULE_COMMAND	0x2
194 #define PROCESS_WITH_ALIAS	0x4
195 
196 typedef enum
197 {
198 	ZBX_KEY_ACCESS_ALLOW,
199 	ZBX_KEY_ACCESS_DENY
200 }
201 zbx_key_access_rule_type_t;
202 
203 void	init_metrics(void);
204 int	add_metric(ZBX_METRIC *metric, char *error, size_t max_error_len);
205 int	add_metric_local(ZBX_METRIC *metric, char *error, size_t max_error_len);
206 void	free_metrics(void);
207 
208 void	init_key_access_rules(void);
209 void	finalize_key_access_rules_configuration(void);
210 int	add_key_access_rule(const char *parameter, char *pattern, zbx_key_access_rule_type_t type);
211 int	check_key_access_rules(const char *metric);
212 int	check_request_access_rules(AGENT_REQUEST *request);
213 void	free_key_access_rules(void);
214 
215 int	process(const char *in_command, unsigned flags, AGENT_RESULT *result);
216 
217 void	set_user_parameter_dir(const char *path);
218 int	add_user_parameter(const char *itemkey, char *command, char *error, size_t max_error_len);
219 int	add_user_module(const char *key, int (*function)(void));
220 void	test_parameters(void);
221 void	test_parameter(const char *key);
222 
223 void	init_result(AGENT_RESULT *result);
224 void	zbx_log_free(zbx_log_t *log);
225 void	free_result(AGENT_RESULT *result);
226 
227 void	init_request(AGENT_REQUEST *request);
228 void	free_request(AGENT_REQUEST *request);
229 
230 int	parse_item_key(const char *itemkey, AGENT_REQUEST *request);
231 
232 void	unquote_key_param(char *param);
233 int	quote_key_param(char **param, int forced);
234 
235 int	set_result_type(AGENT_RESULT *result, int value_type, char *c);
236 void	set_result_meta(AGENT_RESULT *result, zbx_uint64_t lastlogsize, int mtime);
237 
238 #ifdef HAVE_KSTAT_H
239 zbx_uint64_t	get_kstat_numeric_value(const kstat_named_t *kn);
240 #endif
241 
242 /* external system functions */
243 
244 int	GET_SENSOR(AGENT_REQUEST *request, AGENT_RESULT *result);
245 int	KERNEL_MAXFILES(AGENT_REQUEST *request, AGENT_RESULT *result);
246 int	KERNEL_MAXPROC(AGENT_REQUEST *request, AGENT_RESULT *result);
247 
248 #ifdef ZBX_PROCSTAT_COLLECTOR
249 int	PROC_CPU_UTIL(AGENT_REQUEST *request, AGENT_RESULT *result);
250 #endif
251 
252 int	PROC_MEM(AGENT_REQUEST *request, AGENT_RESULT *result);
253 int	PROC_NUM(AGENT_REQUEST *request, AGENT_RESULT *result);
254 int	NET_IF_IN(AGENT_REQUEST *request, AGENT_RESULT *result);
255 int	NET_IF_OUT(AGENT_REQUEST *request, AGENT_RESULT *result);
256 int	NET_IF_TOTAL(AGENT_REQUEST *request, AGENT_RESULT *result);
257 int	NET_IF_COLLISIONS(AGENT_REQUEST *request, AGENT_RESULT *result);
258 int	NET_IF_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result);
259 int	NET_TCP_LISTEN(AGENT_REQUEST *request, AGENT_RESULT *result);
260 int	NET_UDP_LISTEN(AGENT_REQUEST *request, AGENT_RESULT *result);
261 int	SYSTEM_CPU_SWITCHES(AGENT_REQUEST *request, AGENT_RESULT *result);
262 int	SYSTEM_CPU_INTR(AGENT_REQUEST *request, AGENT_RESULT *result);
263 int	SYSTEM_CPU_LOAD(AGENT_REQUEST *request, AGENT_RESULT *result);
264 int	SYSTEM_CPU_UTIL(AGENT_REQUEST *request, AGENT_RESULT *result);
265 int	SYSTEM_CPU_NUM(AGENT_REQUEST *request, AGENT_RESULT *result);
266 int	SYSTEM_CPU_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result);
267 int	SYSTEM_HOSTNAME(AGENT_REQUEST *request, AGENT_RESULT *result);
268 int	SYSTEM_HW_CHASSIS(AGENT_REQUEST *request, AGENT_RESULT *result);
269 int	SYSTEM_HW_CPU(AGENT_REQUEST *request, AGENT_RESULT *result);
270 int	SYSTEM_HW_DEVICES(AGENT_REQUEST *request, AGENT_RESULT *result);
271 int	SYSTEM_HW_MACADDR(AGENT_REQUEST *request, AGENT_RESULT *result);
272 int	SYSTEM_SW_ARCH(AGENT_REQUEST *request, AGENT_RESULT *result);
273 int	SYSTEM_SW_OS(AGENT_REQUEST *request, AGENT_RESULT *result);
274 int	SYSTEM_SW_PACKAGES(AGENT_REQUEST *request, AGENT_RESULT *result);
275 int	SYSTEM_SWAP_IN(AGENT_REQUEST *request, AGENT_RESULT *result);
276 int	SYSTEM_SWAP_OUT(AGENT_REQUEST *request, AGENT_RESULT *result);
277 int	SYSTEM_SWAP_SIZE(AGENT_REQUEST *request, AGENT_RESULT *result);
278 int	SYSTEM_UPTIME(AGENT_REQUEST *request, AGENT_RESULT *result);
279 int	SYSTEM_UNAME(AGENT_REQUEST *request, AGENT_RESULT *result);
280 int	SYSTEM_BOOTTIME(AGENT_REQUEST *request, AGENT_RESULT *result);
281 int	VFS_DEV_READ(AGENT_REQUEST *request, AGENT_RESULT *result);
282 int	VFS_DEV_WRITE(AGENT_REQUEST *request, AGENT_RESULT *result);
283 int	VFS_DEV_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result);
284 int	VFS_FS_INODE(AGENT_REQUEST *request, AGENT_RESULT *result);
285 int	VFS_FS_SIZE(AGENT_REQUEST *request, AGENT_RESULT *result);
286 int	VFS_FS_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result);
287 int	VFS_FS_GET(AGENT_REQUEST *request, AGENT_RESULT *result);
288 int	VM_MEMORY_SIZE(AGENT_REQUEST *request, AGENT_RESULT *result);
289 
290 #if defined(_WINDOWS) || defined(__MINGW32__)
291 int	USER_PERF_COUNTER(AGENT_REQUEST *request, AGENT_RESULT *result);
292 int	PERF_COUNTER(AGENT_REQUEST *request, AGENT_RESULT *result);
293 int	PERF_COUNTER_EN(AGENT_REQUEST *request, AGENT_RESULT *result);
294 int	PERF_INSTANCE_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result);
295 int	PERF_INSTANCE_DISCOVERY_EN(AGENT_REQUEST *request, AGENT_RESULT *result);
296 int	SERVICE_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result);
297 int	SERVICE_INFO(AGENT_REQUEST *request, AGENT_RESULT *result);
298 int	SERVICE_STATE(AGENT_REQUEST *request, AGENT_RESULT *result);
299 int	SERVICES(AGENT_REQUEST *request, AGENT_RESULT *result);
300 int	PROC_INFO(AGENT_REQUEST *request, AGENT_RESULT *result);
301 int	NET_IF_LIST(AGENT_REQUEST *request, AGENT_RESULT *result);
302 int	WMI_GET(AGENT_REQUEST *request, AGENT_RESULT *result);
303 int	WMI_GETALL(AGENT_REQUEST *request, AGENT_RESULT *result);
304 int	VM_VMEMORY_SIZE(AGENT_REQUEST *request, AGENT_RESULT *result);
305 #endif
306 
307 #ifdef _AIX
308 int	SYSTEM_STAT(AGENT_REQUEST *request, AGENT_RESULT *result);
309 #endif
310 
311 #if defined(_WINDOWS) || defined(__MINGW32__)
312 typedef int (*zbx_metric_func_t)(AGENT_REQUEST *request, AGENT_RESULT *result, HANDLE timeout_event);
313 #else
314 typedef int (*zbx_metric_func_t)(AGENT_REQUEST *request, AGENT_RESULT *result);
315 #endif
316 
317 typedef struct
318 {
319 	const char	*mode;
320 	int		(*function)(const char *devname, AGENT_RESULT *result);
321 }
322 MODE_FUNCTION;
323 
324 typedef struct
325 {
326 	zbx_uint64_t	total;
327 	zbx_uint64_t	not_used;
328 	zbx_uint64_t	used;
329 	double		pfree;
330 	double		pused;
331 }
332 zbx_fs_metrics_t;
333 
334 typedef struct
335 {
336 	char			fsname[MAX_STRING_LEN];
337 	char			fstype[MAX_STRING_LEN];
338 	zbx_fs_metrics_t	bytes;
339 	zbx_fs_metrics_t	inodes;
340 }
341 zbx_mpoint_t;
342 
343 #define ZBX_LLD_MACRO_FSNAME		"{#FSNAME}"
344 #define ZBX_LLD_MACRO_FSTYPE		"{#FSTYPE}"
345 #define ZBX_LLD_MACRO_FSDRIVETYPE	"{#FSDRIVETYPE}"
346 
347 #define ZBX_SYSINFO_TAG_FSNAME			"fsname"
348 #define ZBX_SYSINFO_TAG_FSTYPE			"fstype"
349 #define ZBX_SYSINFO_TAG_FSDRIVETYPE		"fsdrivetype"
350 #define ZBX_SYSINFO_TAG_BYTES			"bytes"
351 #define ZBX_SYSINFO_TAG_INODES			"inodes"
352 #define ZBX_SYSINFO_TAG_TOTAL			"total"
353 #define ZBX_SYSINFO_TAG_FREE			"free"
354 #define ZBX_SYSINFO_TAG_USED			"used"
355 #define ZBX_SYSINFO_TAG_PFREE			"pfree"
356 #define ZBX_SYSINFO_TAG_PUSED			"pused"
357 
358 int	zbx_execute_threaded_metric(zbx_metric_func_t metric_func, AGENT_REQUEST *request, AGENT_RESULT *result);
359 void	zbx_mpoints_free(zbx_mpoint_t *mpoint);
360 
361 /* the fields used by proc queries */
362 #define ZBX_SYSINFO_PROC_NONE		0x0000
363 #define ZBX_SYSINFO_PROC_PID		0x0001
364 #define ZBX_SYSINFO_PROC_NAME		0x0002
365 #define ZBX_SYSINFO_PROC_CMDLINE	0x0004
366 #define ZBX_SYSINFO_PROC_USER		0x0008
367 
368 #if defined(_WINDOWS) || defined(__MINGW32__)
369 #define ZBX_MUTEX_ALL_ALLOW		0
370 #define ZBX_MUTEX_THREAD_DENIED		1
371 #define ZBX_MUTEX_LOGGING_DENIED	2
372 zbx_uint32_t get_thread_global_mutex_flag(void);
373 #endif
374 
375 #ifndef _WINDOWS
376 int	hostname_handle_params(AGENT_REQUEST *request, AGENT_RESULT *result, char *hostname);
377 #endif
378 
379 #endif
380 
381