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 "../common/common.h"
23
VM_MEMORY_TOTAL(AGENT_RESULT * result)24 static int VM_MEMORY_TOTAL(AGENT_RESULT *result)
25 {
26 return EXECUTE_INT("vmstat -s | awk 'BEGIN{pages=0}{gsub(\"[()]\",\"\");if($4==\"pagesize\")pgsize=($6);if(($2==\"inactive\"||$2==\"active\"||$2==\"wired\")&&$3==\"pages\")pages+=$1}END{printf (pages*pgsize)}'", result);
27 }
28
VM_MEMORY_FREE(AGENT_RESULT * result)29 static int VM_MEMORY_FREE(AGENT_RESULT *result)
30 {
31 return EXECUTE_INT("vmstat -s | awk '{gsub(\"[()]\",\"\");if($4==\"pagesize\")pgsize=($6);if($2==\"free\"&&$3==\"pages\")pages=($1)}END{printf (pages*pgsize)}'", result);
32 }
33
VM_MEMORY_USED(AGENT_RESULT * result)34 static int VM_MEMORY_USED(AGENT_RESULT *result)
35 {
36 int ret = SYSINFO_RET_FAIL;
37 AGENT_RESULT result_tmp;
38 zbx_uint64_t free, total;
39
40 init_result(&result_tmp);
41
42 if (SYSINFO_RET_OK != VM_MEMORY_FREE(&result_tmp))
43 {
44 SET_MSG_RESULT(result, zbx_strdup(NULL, result_tmp.msg));
45 goto clean;
46 }
47
48 free = result_tmp.ui64;
49
50 if (SYSINFO_RET_OK != VM_MEMORY_TOTAL(&result_tmp))
51 {
52 SET_MSG_RESULT(result, zbx_strdup(NULL, result_tmp.msg));
53 goto clean;
54 }
55
56 total = result_tmp.ui64;
57
58 SET_UI64_RESULT(result, total - free);
59
60 ret = SYSINFO_RET_OK;
61 clean:
62 free_result(&result_tmp);
63
64 return ret;
65 }
66
VM_MEMORY_PUSED(AGENT_RESULT * result)67 static int VM_MEMORY_PUSED(AGENT_RESULT *result)
68 {
69 int ret = SYSINFO_RET_FAIL;
70 AGENT_RESULT result_tmp;
71 zbx_uint64_t free, total;
72
73 init_result(&result_tmp);
74
75 if (SYSINFO_RET_OK != VM_MEMORY_FREE(&result_tmp))
76 {
77 SET_MSG_RESULT(result, zbx_strdup(NULL, result_tmp.msg));
78 goto clean;
79 }
80
81 free = result_tmp.ui64;
82
83 if (SYSINFO_RET_OK != VM_MEMORY_TOTAL(&result_tmp))
84 {
85 SET_MSG_RESULT(result, zbx_strdup(NULL, result_tmp.msg));
86 goto clean;
87 }
88
89 total = result_tmp.ui64;
90
91 if (0 == total)
92 {
93 SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot calculate percentage because total is zero."));
94 goto clean;
95 }
96
97 SET_UI64_RESULT(result, (total - free) / (double)total * 100);
98
99 ret = SYSINFO_RET_OK;
100 clean:
101 free_result(&result_tmp);
102
103 return ret;
104 }
105
VM_MEMORY_AVAILABLE(AGENT_RESULT * result)106 static int VM_MEMORY_AVAILABLE(AGENT_RESULT *result)
107 {
108 return VM_MEMORY_FREE(result);
109 }
110
VM_MEMORY_PAVAILABLE(AGENT_RESULT * result)111 static int VM_MEMORY_PAVAILABLE(AGENT_RESULT *result)
112 {
113 int ret = SYSINFO_RET_FAIL;
114 AGENT_RESULT result_tmp;
115 zbx_uint64_t free, total;
116
117 init_result(&result_tmp);
118
119 if (SYSINFO_RET_OK != VM_MEMORY_FREE(&result_tmp))
120 {
121 SET_MSG_RESULT(result, zbx_strdup(NULL, result_tmp.msg));
122 goto clean;
123 }
124
125 free = result_tmp.ui64;
126
127 if (SYSINFO_RET_OK != VM_MEMORY_TOTAL(&result_tmp))
128 {
129 SET_MSG_RESULT(result, zbx_strdup(NULL, result_tmp.msg));
130 goto clean;
131 }
132
133 total = result_tmp.ui64;
134
135 if (0 == total)
136 {
137 SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot calculate percentage because total is zero."));
138 goto clean;
139 }
140
141 SET_UI64_RESULT(result, free / (double)total * 100);
142
143 ret = SYSINFO_RET_OK;
144 clean:
145 free_result(&result_tmp);
146
147 return ret;
148 }
149
VM_MEMORY_SIZE(AGENT_REQUEST * request,AGENT_RESULT * result)150 int VM_MEMORY_SIZE(AGENT_REQUEST *request, AGENT_RESULT *result)
151 {
152 char *mode;
153 int ret = SYSINFO_RET_FAIL;
154
155 if (1 < request->nparam)
156 {
157 SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters."));
158 return SYSINFO_RET_FAIL;
159 }
160
161 mode = get_rparam(request, 0);
162
163 if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "total"))
164 ret = VM_MEMORY_TOTAL(result);
165 else if (0 == strcmp(mode, "free"))
166 ret = VM_MEMORY_FREE(result);
167 else if (0 == strcmp(mode, "used"))
168 ret = VM_MEMORY_USED(result);
169 else if (0 == strcmp(mode, "pused"))
170 ret = VM_MEMORY_PUSED(result);
171 else if (0 == strcmp(mode, "available"))
172 ret = VM_MEMORY_AVAILABLE(result);
173 else if (0 == strcmp(mode, "pavailable"))
174 ret = VM_MEMORY_PAVAILABLE(result);
175 else
176 {
177 SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter."));
178 return SYSINFO_RET_FAIL;
179 }
180
181 return ret;
182 }
183