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
SYSTEM_SWAP_SIZE(AGENT_REQUEST * request,AGENT_RESULT * result)24 int SYSTEM_SWAP_SIZE(AGENT_REQUEST *request, AGENT_RESULT *result)
25 {
26 /*
27 * FreeBSD 7.0 i386
28 */
29 #ifdef XSWDEV_VERSION /* defined in <vm/vm_param.h> */
30 char *swapdev, *mode;
31 int mib[16], *mib_dev;
32 size_t sz, mib_sz;
33 struct xswdev xsw;
34 zbx_uint64_t total = 0, used = 0;
35
36 if (2 < request->nparam)
37 {
38 SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters."));
39 return SYSINFO_RET_FAIL;
40 }
41
42 swapdev = get_rparam(request, 0);
43 mode = get_rparam(request, 1);
44
45 sz = ARRSIZE(mib);
46 if (-1 == sysctlnametomib("vm.swap_info", mib, &sz))
47 {
48 SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain \"vm.swap_info\" system parameter: %s",
49 zbx_strerror(errno)));
50 return SYSINFO_RET_FAIL;
51 }
52
53 mib_sz = sz + 1;
54 mib_dev = &(mib[sz]);
55
56 *mib_dev = 0;
57 sz = sizeof(xsw);
58
59 while (-1 != sysctl(mib, mib_sz, &xsw, &sz, NULL, 0))
60 {
61 if (NULL == swapdev || '\0' == *swapdev || 0 == strcmp(swapdev, "all") /* default parameter */
62 || 0 == strcmp(swapdev, devname(xsw.xsw_dev, S_IFCHR)))
63 {
64 total += (zbx_uint64_t)xsw.xsw_nblks;
65 used += (zbx_uint64_t)xsw.xsw_used;
66 }
67 (*mib_dev)++;
68 }
69
70 if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "free"))
71 {
72 SET_UI64_RESULT(result, (total - used) * getpagesize());
73 }
74 else if (0 == strcmp(mode, "total"))
75 {
76 SET_UI64_RESULT(result, total * getpagesize());
77 }
78 else if (0 == strcmp(mode, "used"))
79 {
80 SET_UI64_RESULT(result, used * getpagesize());
81 }
82 else if (0 == strcmp(mode, "pfree"))
83 {
84 SET_DBL_RESULT(result, 0 != total ? ((double)(total - used) * 100.0 / (double)total) : 100.0);
85 }
86 else if (0 == strcmp(mode, "pused"))
87 {
88 SET_DBL_RESULT(result, 0 != total ? ((double)used * 100.0 / (double)total) : 0.0);
89 }
90 else
91 {
92 SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
93 return SYSINFO_RET_FAIL;
94 }
95
96 return SYSINFO_RET_OK;
97 #else
98 SET_MSG_RESULT(result, zbx_strdup(NULL, "Agent was compiled without support for \"xswdev\" structure."));
99 return SYSINFO_RET_FAIL;
100 #endif
101 }
102