Debian Codegraph @

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

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2014,2015 Davide Madrisan <[email protected]>
5 *
6 * A Nagios plugin to check the swap usage on unix.
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 is based on the source code of the tool "free" (procps 3.2.8)
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 "messages.h"
32 #include "meminfo.h"
33 #include "progname.h"
34 #include "progversion.h"
35 #include "thresholds.h"
36 #include "units.h"
37 #include "vminfo.h"
38 #include "xalloc.h"
39 #include "xasprintf.h"
40
41 static const char *program_copyright =
42 "Copyright (C) 2014,2015 Davide Madrisan <" PACKAGE_BUGREPORT ">\n";
43
44 static struct option const longopts[] = {
45 {(char *) "vmstats", no_argument, NULL, 's'},
46 {(char *) "critical", required_argument, NULL, 'c'},
47 {(char *) "warning", required_argument, NULL, 'w'},
48 {(char *) "byte", no_argument, NULL, 'b'},
49 {(char *) "kilobyte", no_argument, NULL, 'k'},
50 {(char *) "megabyte", no_argument, NULL, 'm'},
51 {(char *) "gigabyte", no_argument, NULL, 'g'},
52 {(char *) "help", no_argument, NULL, GETOPT_HELP_CHAR},
53 {(char *) "version", no_argument, NULL, GETOPT_VERSION_CHAR},
54 {NULL, 0, NULL, 0}
55 };
56
57 static _Noreturn void
58 usage (FILE * out)
59 {
60 fprintf (out, "%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
61 fputs ("This plugin checks the swap utilization.\n", out);
62 fputs (program_copyright, out);
63 fputs (USAGE_HEADER, out);
64 fprintf (out, " %s [-b,-k,-m,-g] [-s] -w PERC -c PERC\n", program_name);
65 fputs (USAGE_OPTIONS, out);
66 fputs (" -b,-k,-m,-g "
67 "show output in bytes, KB (the default), MB, or GB\n", out);
68 fputs (" -s, --vmstats display the virtual memory perfdata\n", out);
69 fputs (" -w, --warning PERCENT warning threshold\n", out);
70 fputs (" -c, --critical PERCENT critical threshold\n", out);
71 fputs (USAGE_HELP, out);
72 fputs (USAGE_VERSION, out);
73 fputs (USAGE_EXAMPLES, out);
74 fprintf (out, " %s --vmstats -w 30%% -c 50%%\n", program_name);
75
76 exit (out == stderr ? STATE_UNKNOWN : STATE_OK);
77 }
78
79 static _Noreturn void
80 print_version (void)
81 {
82 printf ("%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
83 fputs (program_copyright, stdout);
84 fputs (GPLv3_DISCLAIMER, stdout);
85
86 exit (STATE_OK);
87 }
88
89 int
90 main (int argc, char **argv)
91 {
92 bool vmem_perfdata = false;
93 int c, status, err;
94 int shift = k_shift;
95 char *critical = NULL, *warning = NULL;
96 char *units = NULL;
97 char *status_msg;
98 char *perfdata_swap_msg, *perfdata_vmem_msg = NULL;
99 float percent_used = 0;
100 thresholds *my_threshold = NULL;
101
102 struct proc_sysmem *sysmem = NULL;
103 unsigned long kb_swap_cached;
104 unsigned long kb_swap_free;
105 unsigned long kb_swap_total;
106 unsigned long kb_swap_used;
107
108 struct proc_vmem *vmem = NULL;
109 unsigned long kb_swap_pageins[2];
110 unsigned long kb_swap_pageouts[2];
111
112 set_program_name (argv[0]);
113
114 while ((c = getopt_long (argc, argv, "sc:w:bkmg" GETOPT_HELP_VERSION_STRING,
115 longopts, NULL)) != -1)
116 {
117 switch (c)
118 {
119 default:
120 usage (stderr);
121 case 's':
122 vmem_perfdata = true;
123 break;
124 case 'c':
125 critical = optarg;
126 break;
127 case 'w':
128 warning = optarg;
129 break;
130 case 'b': shift = b_shift; units = xstrdup ("B"); break;
131 case 'k': shift = k_shift; units = xstrdup ("kB"); break;
132 case 'm': shift = m_shift; units = xstrdup ("MB"); break;
133 case 'g': shift = g_shift; units = xstrdup ("GB"); break;
134
135 case_GETOPT_HELP_CHAR
136 case_GETOPT_VERSION_CHAR
137
138 }
139 }
140
141 if (!thresholds_expressed_as_percentages (warning, critical))
142 usage (stderr);
143
144 status = set_thresholds (&my_threshold, warning, critical);
145 if (status == NP_RANGE_UNPARSEABLE)
146 usage (stderr);
147
148 /* output in kilobytes by default */
149 if (units == NULL)
150 units = xstrdup ("kB");
151
152 err = proc_sysmem_new (&sysmem);
153 if (err < 0)
154 plugin_error (STATE_UNKNOWN, err, "memory exhausted");
155
156 proc_sysmem_read (sysmem);
157
158 kb_swap_cached = proc_sysmem_get_swap_cached (sysmem);
159 kb_swap_free = proc_sysmem_get_swap_free (sysmem);
160 kb_swap_total = proc_sysmem_get_swap_total (sysmem);
161 kb_swap_used = proc_sysmem_get_swap_used (sysmem);
162
163 if (vmem_perfdata)
164 {
165 unsigned long dpswpin, dpswpout;
166
167 err = proc_vmem_new (&vmem);
168 if (err < 0)
169 plugin_error (STATE_UNKNOWN, err, "memory exhausted");
170
171 proc_vmem_read (vmem);
172 kb_swap_pageins[0] = proc_vmem_get_pswpin (vmem);
173 kb_swap_pageouts[0] = proc_vmem_get_pswpout (vmem);
174
175 sleep (1);
176
177 proc_vmem_read (vmem);
178 kb_swap_pageins[1] = proc_vmem_get_pswpin (vmem);
179 kb_swap_pageouts[1] = proc_vmem_get_pswpout (vmem);
180
181 dpswpin = kb_swap_pageins[1] - kb_swap_pageins[0];
182 dpswpout = kb_swap_pageouts[1] - kb_swap_pageouts[0];
183
184 perfdata_vmem_msg =
185 xasprintf (", swap_pageins/s=%lu swap_pageouts/s=%lu",
186 dpswpin, dpswpout);
187 }
188
189 if (kb_swap_total != 0)
190 percent_used = (kb_swap_used * 100.0 / kb_swap_total);
191
192 status = get_status (percent_used, my_threshold);
193 free (my_threshold);
194
195 status_msg = xasprintf ("%s: %.2f%% (%llu %s) used", state_text (status),
196 percent_used, UNIT_STR (kb_swap_used));
197
198 perfdata_swap_msg =
199 xasprintf ("swap_total=%llu%s swap_used=%llu%s swap_free=%llu%s "
200 /* The amount of swap, in kB, used as cache memory */
201 "swap_cached=%llu%s",
202 UNIT_STR (kb_swap_total), UNIT_STR (kb_swap_used),
203 UNIT_STR (kb_swap_free), UNIT_STR (kb_swap_cached));
204
205 printf ("%s %s | %s%s\n", program_name_short, status_msg, perfdata_swap_msg,
206 vmem_perfdata ? perfdata_vmem_msg : "");
207
208 proc_vmem_unref (vmem);
209 proc_sysmem_unref (sysmem);
210
211 return status;
212 }