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 "log.h"
23 
KERNEL_MAXPROC(AGENT_REQUEST * request,AGENT_RESULT * result)24 int	KERNEL_MAXPROC(AGENT_REQUEST *request, AGENT_RESULT *result)
25 {
26 	int		ret = SYSINFO_RET_FAIL;
27 	kstat_ctl_t	*kc;
28 	kstat_t		*kt;
29 	struct var	*v;
30 
31 	if (NULL == (kc = kstat_open()))
32 	{
33 		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot open kernel statistics facility: %s",
34 				zbx_strerror(errno)));
35 		return SYSINFO_RET_FAIL;
36 	}
37 
38 	if (NULL == (kt = kstat_lookup(kc, "unix", 0, "var")))
39 	{
40 		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot look up in kernel statistics facility: %s",
41 				zbx_strerror(errno)));
42 		goto clean;
43 	}
44 
45 	if (KSTAT_TYPE_RAW != kt->ks_type)
46 	{
47 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Information looked up in kernel statistics facility"
48 				" is of the wrong type."));
49 		goto clean;
50 	}
51 
52 	if (-1 == kstat_read(kc, kt, NULL))
53 	{
54 		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot read from kernel statistics facility: %s",
55 				zbx_strerror(errno)));
56 		goto clean;
57 	}
58 
59 	v = (struct var *)kt->ks_data;
60 
61 	/* int	v_proc;	    Max processes system wide */
62 	SET_UI64_RESULT(result, v->v_proc);
63 	ret = SYSINFO_RET_OK;
64 clean:
65 	kstat_close(kc);
66 
67 	return ret;
68 }
69