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
24 #ifdef HAVE_LIBPERFSTAT
25
26 static perfstat_memory_total_t m;
27
28 #define ZBX_PERFSTAT_PAGE_SHIFT 12 /* 4 KB */
29
30 #define ZBX_PERFSTAT_MEMORY_TOTAL() \
31 \
32 if (-1 == perfstat_memory_total(NULL, &m, sizeof(m), 1)) \
33 { \
34 SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", \
35 zbx_strerror(errno))); \
36 return SYSINFO_RET_FAIL; \
37 }
38
VM_MEMORY_TOTAL(AGENT_RESULT * result)39 static int VM_MEMORY_TOTAL(AGENT_RESULT *result)
40 {
41 ZBX_PERFSTAT_MEMORY_TOTAL();
42
43 SET_UI64_RESULT(result, m.real_total << ZBX_PERFSTAT_PAGE_SHIFT); /* total real memory in pages */
44
45 return SYSINFO_RET_OK;
46 }
47
VM_MEMORY_PINNED(AGENT_RESULT * result)48 static int VM_MEMORY_PINNED(AGENT_RESULT *result)
49 {
50 ZBX_PERFSTAT_MEMORY_TOTAL();
51
52 SET_UI64_RESULT(result, m.real_pinned << ZBX_PERFSTAT_PAGE_SHIFT); /* real memory which is pinned in pages */
53
54 return SYSINFO_RET_OK;
55 }
56
VM_MEMORY_FREE(AGENT_RESULT * result)57 static int VM_MEMORY_FREE(AGENT_RESULT *result)
58 {
59 ZBX_PERFSTAT_MEMORY_TOTAL();
60
61 SET_UI64_RESULT(result, m.real_free << ZBX_PERFSTAT_PAGE_SHIFT); /* free real memory in pages */
62
63 return SYSINFO_RET_OK;
64 }
65
VM_MEMORY_USED(AGENT_RESULT * result)66 static int VM_MEMORY_USED(AGENT_RESULT *result)
67 {
68 ZBX_PERFSTAT_MEMORY_TOTAL();
69
70 SET_UI64_RESULT(result, m.real_inuse << ZBX_PERFSTAT_PAGE_SHIFT); /* real memory which is in use in pages */
71
72 return SYSINFO_RET_OK;
73 }
74
VM_MEMORY_PUSED(AGENT_RESULT * result)75 static int VM_MEMORY_PUSED(AGENT_RESULT *result)
76 {
77 ZBX_PERFSTAT_MEMORY_TOTAL();
78
79 if (0 == m.real_total)
80 {
81 SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot calculate percentage because total is zero."));
82 return SYSINFO_RET_FAIL;
83 }
84
85 SET_DBL_RESULT(result, m.real_inuse / (double)m.real_total * 100);
86
87 return SYSINFO_RET_OK;
88 }
89
VM_MEMORY_AVAILABLE(AGENT_RESULT * result)90 static int VM_MEMORY_AVAILABLE(AGENT_RESULT *result)
91 {
92 ZBX_PERFSTAT_MEMORY_TOTAL();
93
94 SET_UI64_RESULT(result, (m.real_free + m.numperm) << ZBX_PERFSTAT_PAGE_SHIFT);
95
96 return SYSINFO_RET_OK;
97 }
98
VM_MEMORY_PAVAILABLE(AGENT_RESULT * result)99 static int VM_MEMORY_PAVAILABLE(AGENT_RESULT *result)
100 {
101 ZBX_PERFSTAT_MEMORY_TOTAL();
102
103 if (0 == m.real_total)
104 {
105 SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot calculate percentage because total is zero."));
106 return SYSINFO_RET_FAIL;
107 }
108
109 SET_DBL_RESULT(result, (m.real_free + m.numperm) / (double)m.real_total * 100);
110
111 return SYSINFO_RET_OK;
112 }
113
VM_MEMORY_CACHED(AGENT_RESULT * result)114 static int VM_MEMORY_CACHED(AGENT_RESULT *result)
115 {
116 ZBX_PERFSTAT_MEMORY_TOTAL();
117
118 SET_UI64_RESULT(result, m.numperm << ZBX_PERFSTAT_PAGE_SHIFT); /* number of pages used for files */
119
120 return SYSINFO_RET_OK;
121 }
122
123 #endif
124
VM_MEMORY_SIZE(AGENT_REQUEST * request,AGENT_RESULT * result)125 int VM_MEMORY_SIZE(AGENT_REQUEST *request, AGENT_RESULT *result)
126 {
127 #ifdef HAVE_LIBPERFSTAT
128 int ret;
129 char *mode;
130
131 if (1 < request->nparam)
132 {
133 SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters."));
134 return SYSINFO_RET_FAIL;
135 }
136
137 mode = get_rparam(request, 0);
138
139 if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "total"))
140 ret = VM_MEMORY_TOTAL(result);
141 else if (0 == strcmp(mode, "pinned"))
142 ret = VM_MEMORY_PINNED(result);
143 else if (0 == strcmp(mode, "free"))
144 ret = VM_MEMORY_FREE(result);
145 else if (0 == strcmp(mode, "used"))
146 ret = VM_MEMORY_USED(result);
147 else if (0 == strcmp(mode, "pused"))
148 ret = VM_MEMORY_PUSED(result);
149 else if (0 == strcmp(mode, "available"))
150 ret = VM_MEMORY_AVAILABLE(result);
151 else if (0 == strcmp(mode, "pavailable"))
152 ret = VM_MEMORY_PAVAILABLE(result);
153 else if (0 == strcmp(mode, "cached"))
154 ret = VM_MEMORY_CACHED(result);
155 else
156 {
157 SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter."));
158 return SYSINFO_RET_FAIL;
159 }
160
161 return ret;
162 #else
163 SET_MSG_RESULT(result, zbx_strdup(NULL, "Agent was compiled without support for Perfstat API."));
164 return SYSINFO_RET_FAIL;
165 #endif
166 }
167