Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/plugins/check_memory.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2014-2024 Davide Madrisan <[email protected]>
5 *
6 * A Nagios plugin to check system memory usage on Linux.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 * This software takes some ideas and code from procps-3.2.8 (free).
22 */
23
24 #include <getopt.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 #include "common.h"
31 #include "logging.h"
32 #include "meminfo.h"
33 #include "messages.h"
34 #include "perfdata.h"
35 #include "progname.h"
36 #include "progversion.h"
37 #include "string-macros.h"
38 #include "system.h"
39 #include "thresholds.h"
40 #include "units.h"
41 #include "vminfo.h"
42 #include "xalloc.h"
43 #include "xasprintf.h"
44
45 static const char *program_copyright =
46 "Copyright (C) 2014-2024 Davide Madrisan <" PACKAGE_BUGREPORT ">\n";
47
48 static struct option const longopts[] = {
49 {(char *) "available", no_argument, NULL, 'a'},
50 {(char *) "caches", no_argument, NULL, 'C'},
51 {(char *) "vmstats", no_argument, NULL, 's'},
52 {(char *) "critical", required_argument, NULL, 'c'},
53 {(char *) "warning", required_argument, NULL, 'w'},
54 {(char *) "byte", no_argument, NULL, 'b'},
55 {(char *) "kilobyte", no_argument, NULL, 'k'},
56 {(char *) "megabyte", no_argument, NULL, 'm'},
57 {(char *) "gigabyte", no_argument, NULL, 'g'},
58 {(char *) "units", required_argument, 0, 'u'},
59 {(char *) "help", no_argument, NULL, GETOPT_HELP_CHAR},
60 {(char *) "version", no_argument, NULL, GETOPT_VERSION_CHAR},
61 {NULL, 0, NULL, 0}
62 };
63
64 static _Noreturn void
65 usage (FILE * out)
66 {
67 fprintf (out, "%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
68 fputs ("This plugin checks the system memory utilization.\n", out);
69 fputs (program_copyright, out);
70 fputs (USAGE_HEADER, out);
71 fprintf (out, " %s [-a] [-b,-k,-m,-g] [-s] [-u UNIT] -w PERC -c PERC\n",
72 program_name);
73 fputs (USAGE_OPTIONS, out);
74 fputs (" -a, --available display the free/available memory\n",
75 out);
76 fputs (" -b,-k,-m,-g "
77 "show output in bytes, KB (the default), MB, or GB\n", out);
78 fputs (" -s, --vmstats display the virtual memory perfdata\n", out);
79 fputs (" -u, --units show output in the selected unit (default: KB),\n",
80 out);
81 fputs (" choose bytes, B, kB, MB, GB, KiB, MiB, GiB\n",
82 out);
83 fputs (" -w, --warning PERCENT warning threshold\n", out);
84 fputs (" -c, --critical PERCENT critical threshold\n", out);
85 fputs (USAGE_HELP, out);
86 fputs (USAGE_VERSION, out);
87 fputs (USAGE_NOTE_1, out);
88 fputs (" The option '-a|--available' gives an estimation of the "
89 "available memory\n"
90 " for starting new applications without swapping.\n", out);
91 fputs (" It requires at least a kernel 3.14, which provides this "
92 "information in\n"
93 " /proc/meminfo (see the parameter 'MemAvailable').\n", out);
94 fputs (" A MemAvailable fall-back code is implemented for "
95 "kernels 2.6.27 and above.\n", out);
96 fputs (" For older kernels 'MemFree' is returned instead.\n", out);
97 fputs (USAGE_NOTE_2, out);
98 fputs (" kB/MB/GB are still calculated as their respective binary units "
99 "due to\n", out);
100 fputs (" backward compatibility issues.\n", out);
101 fputs (USAGE_EXAMPLES, out);
102 fprintf (out, " %s --available -w 20%%: -c 10%%:\n", program_name);
103 fprintf (out, " %s --available --units MiB -w 20%%: -c 10%%:\n", program_name);
104 fprintf (out, " %s --vmstats -w 80%% -c90%%\n", program_name);
105
106 exit (out == stderr ? STATE_UNKNOWN : STATE_OK);
107 }
108
109 static _Noreturn void
110 print_version (void)
111 {
112 printf ("%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
113 fputs (program_copyright, stdout);
114 fputs (GPLv3_DISCLAIMER, stdout);
115
116 exit (STATE_OK);
117 }
118
119 #ifndef NPL_TESTING
120 int
121 main (int argc, char **argv)
122 {
123 bool vmem_perfdata = false;
124 int c, status, err;
125 int shift = k_shift;
126 char *critical = NULL, *warning = NULL;
127 char *units = NULL;
128 char *status_msg, *perfdata_mem_msg,
129 *perfdata_vmem_msg = "",
130 *perfdata_memavailable_msg = "",
131 *perfdata_memused_msg = "";
132 float mem_percent = 0;
133 thresholds *my_threshold = NULL;
134
135 struct proc_sysmem *sysmem = NULL;
136 unsigned long kb_mem_main_available;
137 unsigned long kb_mem_main_buffers;
138 unsigned long kb_mem_main_cached;
139 unsigned long kb_mem_main_free;
140 unsigned long kb_mem_main_shared;
141 unsigned long kb_mem_main_total;
142 unsigned long kb_mem_main_used;
143 unsigned long kb_mem_active;
144 unsigned long kb_mem_anon_pages;
145 unsigned long kb_mem_committed_as;
146 unsigned long kb_mem_dirty;
147 unsigned long kb_mem_inactive;
148
149 struct proc_vmem *vmem = NULL;
150 unsigned long nr_vmem_pgpgin[2];
151 unsigned long nr_vmem_pgpgout[2];
152 unsigned long nr_vmem_pgmajfault[2];
153
154 /* by default we display the memory used */
155 unsigned long *kb_mem_monitored = &kb_mem_main_used;
156
157 set_program_name (argv[0]);
158
159 while ((c = getopt_long (argc, argv,
160 "aMSCsc:w:bkmgu:" GETOPT_HELP_VERSION_STRING,
161 longopts, NULL)) != -1)
162 {
163 switch (c)
164 {
165 default:
166 usage (stderr);
167 case 'a':
168 kb_mem_monitored = &kb_mem_main_available;
169 break;
170 case 'C':
171 /* does nothing, exists for compatibility */
172 break;
173 case 's':
174 vmem_perfdata = true;
175 break;
176 case 'c':
177 critical = optarg;
178 break;
179 case 'w':
180 warning = optarg;
181 break;
182 case 'b': shift = b_shift; units = xstrdup ("B"); break;
183 case 'k': shift = k_shift; units = xstrdup ("kB"); break;
184 case 'm': shift = m_shift; units = xstrdup ("MB"); break;
185 case 'g': shift = g_shift; units = xstrdup ("GB"); break;
186 case 'u':
187 if (units)
188 free (units);
189
190 if (STREQ (optarg, "B") || STREQ (optarg, "bytes"))
191 shift = b_shift;
192 else if (STREQ (optarg, "kB") || STREQ (optarg, "KiB"))
193 shift = k_shift;
194 else if (STREQ (optarg, "MB") || STREQ (optarg, "MiB"))
195 shift = m_shift;
196 else if (STREQ (optarg, "GB") || STREQ (optarg, "GiB"))
197 shift = g_shift;
198 else
199 plugin_error (STATE_UNKNOWN, 0, "unit type %s not known", optarg);
200
201 units = xstrdup (optarg);
202 break;
203
204 case_GETOPT_HELP_CHAR
205 case_GETOPT_VERSION_CHAR
206
207 }
208 }
209
210 if (!thresholds_expressed_as_percentages (warning, critical))
211 usage (stderr);
212
213 status = set_thresholds (&my_threshold, warning, critical);
214 if (status == NP_RANGE_UNPARSEABLE)
215 usage (stderr);
216
217 /* output in kilobytes by default */
218 if (NULL == units)
219 units = xstrdup ("kB");
220
221 err = proc_sysmem_new (&sysmem);
222 if (err < 0)
223 plugin_error (STATE_UNKNOWN, err, "memory exhausted");
224
225 proc_sysmem_read (sysmem);
226
227 kb_mem_active = proc_sysmem_get_active (sysmem);
228 kb_mem_anon_pages = proc_sysmem_get_anon_pages (sysmem);
229 kb_mem_committed_as = proc_sysmem_get_committed_as (sysmem);
230 kb_mem_dirty = proc_sysmem_get_dirty (sysmem);
231 kb_mem_inactive = proc_sysmem_get_inactive (sysmem);
232 kb_mem_main_available = proc_sysmem_get_main_available (sysmem);
233 kb_mem_main_buffers = proc_sysmem_get_main_buffers (sysmem);
234 kb_mem_main_cached = proc_sysmem_get_main_cached (sysmem);
235 kb_mem_main_free = proc_sysmem_get_main_free (sysmem);
236 kb_mem_main_shared = proc_sysmem_get_main_shared (sysmem);
237 kb_mem_main_total = proc_sysmem_get_main_total (sysmem);
238 kb_mem_main_used = proc_sysmem_get_main_used (sysmem);
239
240 if (vmem_perfdata)
241 {
242 unsigned long dpgpgin, dpgpgout, dpgmajfault;
243
244 err = proc_vmem_new (&vmem);
245 if (err < 0)
246 plugin_error (STATE_UNKNOWN, err, "memory exhausted");
247
248 proc_vmem_read (vmem);
249 nr_vmem_pgpgin[0] = proc_vmem_get_pgpgin (vmem);
250 nr_vmem_pgpgout[0] = proc_vmem_get_pgpgout (vmem);
251 nr_vmem_pgmajfault[0] = proc_vmem_get_pgmajfault (vmem);
252
253 sleep (1);
254
255 proc_vmem_read (vmem);
256 nr_vmem_pgpgin[1] = proc_vmem_get_pgpgin (vmem);
257 nr_vmem_pgpgout[1] = proc_vmem_get_pgpgout (vmem);
258 nr_vmem_pgmajfault[1] = proc_vmem_get_pgmajfault (vmem);
259
260 dpgpgin = nr_vmem_pgpgin[1] - nr_vmem_pgpgin[0];
261 dpgpgout = nr_vmem_pgpgout[1] - nr_vmem_pgpgout[0];
262 dpgmajfault = nr_vmem_pgmajfault[1] - nr_vmem_pgmajfault[0];
263
264 perfdata_vmem_msg =
265 xasprintf (" vmem_pageins/s=%lu vmem_pageouts/s=%lu "
266 "vmem_pgmajfault/s=%lu", dpgpgin, dpgpgout, dpgmajfault);
267 }
268
269 /* Note: we should perhaps implement the following tests instead:
270 * 1. The Main Memory Test
271 * (MemFree + Buffers + Cached) / MemTotal < threshold
272 * 2. The Page Fault Test
273 * Major pagefaults > threshold
274 *
275 * See. http://doc.qt.digia.com/qtextended4.4/syscust-oom.html */
276
277 if (kb_mem_main_total != 0)
278 mem_percent = ((*kb_mem_monitored) * 100.0 / kb_mem_main_total);
279 status = get_status (mem_percent, my_threshold);
280
281 char *mem_monitored_warning = NULL,
282 *mem_monitored_critical = NULL;
283 unsigned long long warning_limit, critical_limit;
284
285 if (0 == (get_perfdata_limit_converted
286 (my_threshold->warning, kb_mem_main_total, shift, &warning_limit, true)))
287 mem_monitored_warning = xasprintf ("%llu", warning_limit);
288 if (0 == (get_perfdata_limit_converted
289 (my_threshold->critical, kb_mem_main_total, shift,
290 &critical_limit, true)))
291 mem_monitored_critical = xasprintf ("%llu", critical_limit);
292
293 /* performance data format:
294 * 'label'=value[UOM];[warn];[crit];[min];[max] */
295 bool add_perfdata = (kb_mem_monitored == &kb_mem_main_available);
296 perfdata_memavailable_msg =
297 xasprintf ("mem_available=%llu%s;%s;%s;0;%llu",
298 UNIT_STR (kb_mem_main_available),
299 (add_perfdata
300 && mem_monitored_warning) ? mem_monitored_warning : "",
301 (add_perfdata
302 && mem_monitored_critical) ? mem_monitored_critical : "",
303 UNIT_CONVERT (kb_mem_main_total, shift));
304
305 add_perfdata = (kb_mem_monitored == &kb_mem_main_used);
306 perfdata_memused_msg =
307 xasprintf ("mem_used=%llu%s;%s;%s;0;%llu",
308 UNIT_STR (kb_mem_main_used),
309 (add_perfdata
310 && mem_monitored_warning) ? mem_monitored_warning : "",
311 (add_perfdata &&
312 mem_monitored_critical) ? mem_monitored_critical : "",
313 UNIT_CONVERT (kb_mem_main_total, shift));
314
315 status_msg =
316 xasprintf ("%s: %.2f%% (%llu %s) %s",
317 state_text (status), mem_percent, UNIT_STR (*kb_mem_monitored),
318 (kb_mem_monitored == &kb_mem_main_available) ?
319 "available" : "used");
320
321 free (my_threshold);
322
323 perfdata_mem_msg =
324 xasprintf ("mem_total=%llu%s %s mem_free=%llu%s "
325 "mem_shared=%llu%s mem_buffers=%llu%s mem_cached=%llu%s %s "
326 "mem_active=%llu%s mem_anonpages=%llu%s mem_committed=%llu%s "
327 "mem_dirty=%llu%s mem_inactive=%llu%s"
328 , UNIT_STR (kb_mem_main_total)
329 , perfdata_memused_msg
330 , UNIT_STR (kb_mem_main_free)
331 , UNIT_STR (kb_mem_main_shared)
332 , UNIT_STR (kb_mem_main_buffers)
333 , UNIT_STR (kb_mem_main_cached)
334 , perfdata_memavailable_msg
335 , UNIT_STR (kb_mem_active)
336 , UNIT_STR (kb_mem_anon_pages)
337 , UNIT_STR (kb_mem_committed_as)
338 , UNIT_STR (kb_mem_dirty)
339 , UNIT_STR (kb_mem_inactive));
340
341 printf ("%s %s | %s%s\n", program_name_short, status_msg,
342 perfdata_mem_msg, perfdata_vmem_msg);
343
344 proc_sysmem_unref (sysmem);
345 proc_vmem_unref (vmem);
346
347 return status;
348 }
349 #endif /* NPL_TESTING */