fe792e87b3ece6947841d308896415ea0a9facbb
1 /******************************************************************************
2 *
3 * Program: Swap space plugin for Nagios
4 * License: GPL
5 *
6 * License Information:
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 2 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, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 * Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net)
23 *
24 * $Id$
25 *
26 *****************************************************************************/
28 #include "common.h"
29 #include "popen.h"
30 #include "utils.h"
32 const char *progname = "check_swap";
33 const char *revision = "$Revision$";
34 const char *copyright = "2000-2003";
35 const char *email = "nagiosplug-devel@lists.sourceforge.net";
37 int check_swap (int usp, long unsigned int free_swap);
38 int process_arguments (int argc, char **argv);
39 int validate_arguments (void);
40 void print_usage (void);
41 void print_help (void);
43 int warn_percent = 0;
44 int crit_percent = 0;
45 unsigned long long warn_size = 0;
46 unsigned long long crit_size = 0;
47 int verbose;
48 int allswaps;
50 int
51 main (int argc, char **argv)
52 {
53 int percent_used, percent;
54 unsigned long long total_swap = 0, used_swap = 0, free_swap = 0;
55 unsigned long long dsktotal, dskused, dskfree, tmp;
56 int result = STATE_OK;
57 char input_buffer[MAX_INPUT_BUFFER];
58 char *perf;
59 #ifdef HAVE_PROC_MEMINFO
60 FILE *fp;
61 #else
62 # ifdef HAVE_SWAP
63 int conv_factor; /* Convert to MBs */
64 char *temp_buffer;
65 char *swap_command;
66 char *swap_format;
67 # endif
68 #endif
69 char str[32];
70 char *status;
72 setlocale (LC_ALL, "");
73 bindtextdomain (PACKAGE, LOCALEDIR);
74 textdomain (PACKAGE);
76 status = strdup("");
77 perf = strdup("");
79 if (process_arguments (argc, argv) != OK)
80 usage (_("Invalid command arguments supplied\n"));
82 #ifdef HAVE_PROC_MEMINFO
83 fp = fopen (PROC_MEMINFO, "r");
84 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
85 if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %llu %llu %llu", &dsktotal, &dskused, &dskfree) == 3) {
86 dsktotal = dsktotal / 1048576;
87 dskused = dskused / 1048576;
88 dskfree = dskfree / 1048576;
89 total_swap += dsktotal;
90 used_swap += dskused;
91 free_swap += dskfree;
92 if (allswaps) {
93 percent = 100 * (((double) dskused) / ((double) dsktotal));
94 result = max_state (result, check_swap (percent, dskfree));
95 if (verbose)
96 asprintf (&status, "%s [%llu (%d%%)]", status, dskfree, 100 - percent);
97 }
98 }
99 else if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%[TotalFre]%*[:] %llu %*[k]%*[B]", str, &tmp)) {
100 if (strcmp ("Total", str) == 0) {
101 dsktotal = tmp / 1024;
102 }
103 else if (strcmp ("Free", str) == 0) {
104 dskfree = tmp / 1024;
105 }
106 }
107 }
108 fclose(fp);
109 dskused = dsktotal - dskfree;
110 total_swap = dsktotal;
111 used_swap = dskused;
112 free_swap = dskfree;
113 #else
114 # ifdef HAVE_SWAP
115 asprintf(&swap_command, "%s", SWAP_COMMAND);
116 asprintf(&swap_format, "%s", SWAP_FORMAT);
117 conv_factor = SWAP_CONVERSION;
119 /* These override the command used if a summary (and thus ! allswaps) is required */
120 /* The summary flag returns more accurate information about swap usage on these OSes */
121 # ifdef _AIX
122 if (!allswaps) {
123 asprintf(&swap_command, "%s", "/usr/sbin/lsps -s");
124 asprintf(&swap_format, "%s", "%d%*s %d");
125 conv_factor = 1;
126 }
127 # else
128 # ifdef sun
129 if (!allswaps) {
130 asprintf(&swap_command, "%s", "/usr/sbin/swap -s");
131 asprintf(&swap_format, "%s", "%*s %*dk %*s %*s + %*dk %*s = %dk %*s %dk %*s");
132 conv_factor = 2048;
133 }
134 # endif
135 # endif
137 if (verbose >= 2)
138 printf (_("Command: %s\n"), swap_command);
139 if (verbose >= 3)
140 printf (_("Format: %s\n"), swap_format);
142 child_process = spopen (swap_command);
143 if (child_process == NULL) {
144 printf (_("Could not open pipe: %s\n"), swap_command);
145 return STATE_UNKNOWN;
146 }
148 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
149 if (child_stderr == NULL)
150 printf (_("Could not open stderr for %s\n"), swap_command);
152 sprintf (str, "%s", "");
153 /* read 1st line */
154 fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
155 if (strcmp (swap_format, "") == 0) {
156 temp_buffer = strtok (input_buffer, " \n");
157 while (temp_buffer) {
158 if (strstr (temp_buffer, "blocks"))
159 sprintf (str, "%s %s", str, "%f");
160 else if (strstr (temp_buffer, "dskfree"))
161 sprintf (str, "%s %s", str, "%f");
162 else
163 sprintf (str, "%s %s", str, "%*s");
164 temp_buffer = strtok (NULL, " \n");
165 }
166 }
168 /* If different swap command is used for summary switch, need to read format differently */
169 # ifdef _AIX
170 if (!allswaps) {
171 fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process); /* Ignore first line */
172 sscanf (input_buffer, swap_format, &total_swap, &used_swap);
173 free_swap = total_swap * (100 - used_swap) /100;
174 used_swap = total_swap - free_swap;
175 if (verbose >= 3)
176 printf (_("total=%d, used=%d, free=%d\n"), total_swap, used_swap, free_swap);
177 } else {
178 # else
179 # ifdef sun
180 if (!allswaps) {
181 sscanf (input_buffer, swap_format, &used_swap, &free_swap);
182 used_swap = used_swap / 1024;
183 free_swap = free_swap / 1024;
184 total_swap = used_swap + free_swap;
185 } else {
186 # endif
187 # endif
188 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
189 sscanf (input_buffer, swap_format, &dsktotal, &dskfree);
191 dsktotal = dsktotal / conv_factor;
192 /* AIX lists percent used, so this converts to dskfree in MBs */
193 # ifdef _AIX
194 dskfree = dsktotal * (100 - dskfree) / 100;
195 # else
196 dskfree = dskfree / conv_factor;
197 # endif
198 if (verbose >= 3)
199 printf (_("total=%d, free=%d\n"), dsktotal, dskfree);
201 dskused = dsktotal - dskfree;
202 total_swap += dsktotal;
203 used_swap += dskused;
204 free_swap += dskfree;
205 if (allswaps) {
206 percent = 100 * (((double) dskused) / ((double) dsktotal));
207 result = max_state (result, check_swap (percent, dskfree));
208 if (verbose)
209 asprintf (&status, "%s [%llu (%d%%)]", status, dskfree, 100 - percent);
210 }
211 }
212 # ifdef _AIX
213 }
214 # else
215 # ifdef sun
216 }
217 # endif
218 # endif
220 /* If we get anything on STDERR, at least set warning */
221 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
222 result = max_state (result, STATE_WARNING);
224 /* close stderr */
225 (void) fclose (child_stderr);
227 /* close the pipe */
228 if (spclose (child_process))
229 result = max_state (result, STATE_WARNING);
230 # endif /* HAVE_SWAP */
231 #endif /* HAVE_PROC_MEMINFO */
233 percent_used = 100 * ((double) used_swap) / ((double) total_swap);
234 result = max_state (result, check_swap (percent_used, free_swap));
235 asprintf (&status, _(" %d%% free (%llu MB out of %llu MB)%s"),
236 (100 - percent_used), free_swap, total_swap, status);
238 asprintf (&perf, "%s", perfdata ("swap", (long) free_swap, "MB",
239 TRUE, (long) max (warn_size/1024, warn_percent/100.0*total_swap),
240 TRUE, (long) max (crit_size/1024, crit_percent/100.0*total_swap),
241 TRUE, 0,
242 TRUE, (long) total_swap));
243 printf ("SWAP %s:%s |%s\n", state_text (result), status, perf);
244 return result;
245 }
249 \f
250 int
251 check_swap (int usp, long unsigned int free_swap)
252 {
253 int result = STATE_UNKNOWN;
254 free_swap = free_swap * 1024; /* Convert back to bytes as warn and crit specified in bytes */
255 if (usp >= 0 && crit_percent != 0 && usp >= (100.0 - crit_percent))
256 result = STATE_CRITICAL;
257 else if (crit_size > 0 && free_swap <= crit_size)
258 result = STATE_CRITICAL;
259 else if (usp >= 0 && warn_percent != 0 && usp >= (100.0 - warn_percent))
260 result = STATE_WARNING;
261 else if (warn_size > 0 && free_swap <= warn_size)
262 result = STATE_WARNING;
263 else if (usp >= 0.0)
264 result = STATE_OK;
265 return result;
266 }
269 /* process command-line arguments */
270 int
271 process_arguments (int argc, char **argv)
272 {
273 int c = 0; /* option character */
275 int option = 0;
276 static struct option longopts[] = {
277 {"warning", required_argument, 0, 'w'},
278 {"critical", required_argument, 0, 'c'},
279 {"allswaps", no_argument, 0, 'a'},
280 {"verbose", no_argument, 0, 'v'},
281 {"version", no_argument, 0, 'V'},
282 {"help", no_argument, 0, 'h'},
283 {0, 0, 0, 0}
284 };
286 if (argc < 2)
287 return ERROR;
289 while (1) {
290 c = getopt_long (argc, argv, "+?Vvhac:w:", longopts, &option);
292 if (c == -1 || c == EOF)
293 break;
295 switch (c) {
296 case 'w': /* warning size threshold */
297 if (is_intnonneg (optarg)) {
298 warn_size = atoi (optarg);
299 break;
300 }
301 else if (strstr (optarg, ",") &&
302 strstr (optarg, "%") &&
303 sscanf (optarg, "%llu,%d%%", &warn_size, &warn_percent) == 2) {
304 break;
305 }
306 else if (strstr (optarg, "%") &&
307 sscanf (optarg, "%d%%", &warn_percent) == 1) {
308 break;
309 }
310 else {
311 usage (_("Warning threshold must be integer or percentage!\n"));
312 }
313 case 'c': /* critical size threshold */
314 if (is_intnonneg (optarg)) {
315 crit_size = atoi (optarg);
316 break;
317 }
318 else if (strstr (optarg, ",") &&
319 strstr (optarg, "%") &&
320 sscanf (optarg, "%llu,%d%%", &crit_size, &crit_percent) == 2) {
321 break;
322 }
323 else if (strstr (optarg, "%") &&
324 sscanf (optarg, "%d%%", &crit_percent) == 1) {
325 break;
326 }
327 else {
328 usage (_("Critical threshold must be integer or percentage!\n"));
329 }
330 case 'a': /* all swap */
331 allswaps = TRUE;
332 break;
333 case 'v': /* verbose */
334 verbose++;
335 break;
336 case 'V': /* version */
337 print_revision (progname, revision);
338 exit (STATE_OK);
339 case 'h': /* help */
340 print_help ();
341 exit (STATE_OK);
342 case '?': /* error */
343 usage (_("Invalid argument\n"));
344 }
345 }
347 c = optind;
348 if (c == argc)
349 return validate_arguments ();
350 if (warn_percent == 0 && is_intnonneg (argv[c]))
351 warn_percent = atoi (argv[c++]);
353 if (c == argc)
354 return validate_arguments ();
355 if (crit_percent == 0 && is_intnonneg (argv[c]))
356 crit_percent = atoi (argv[c++]);
358 if (c == argc)
359 return validate_arguments ();
360 if (warn_size == 0 && is_intnonneg (argv[c]))
361 warn_size = atoi (argv[c++]);
363 if (c == argc)
364 return validate_arguments ();
365 if (crit_size == 0 && is_intnonneg (argv[c]))
366 crit_size = atoi (argv[c++]);
368 return validate_arguments ();
369 }
375 int
376 validate_arguments (void)
377 {
378 if (warn_percent == 0 && crit_percent == 0 && warn_size == 0
379 && crit_size == 0) {
380 return ERROR;
381 }
382 else if (warn_percent < crit_percent) {
383 usage
384 (_("Warning percentage should be more than critical percentage\n"));
385 }
386 else if (warn_size < crit_size) {
387 usage
388 (_("Warning free space should be more than critical free space\n"));
389 }
390 return OK;
391 }
397 \f
398 void
399 print_help (void)
400 {
401 print_revision (progname, revision);
403 printf (_(COPYRIGHT), copyright, email);
405 printf (_("Check swap space on local server.\n\n"));
407 print_usage ();
409 printf (_(UT_HELP_VRSN));
411 printf (_("\n\
412 -w, --warning=INTEGER\n\
413 Exit with WARNING status if less than INTEGER bytes of swap space are free\n\
414 -w, --warning=PERCENT%%\n\
415 Exit with WARNING status if less than PERCENT of swap space is free\n\
416 -c, --critical=INTEGER\n\
417 Exit with CRITICAL status if less than INTEGER bytes of swap space are free\n\
418 -c, --critical=PERCENT%%\n\
419 Exit with CRITCAL status if less than PERCENT of swap space is free\n\
420 -a, --allswaps\n\
421 Conduct comparisons for all swap partitions, one by one\n\
422 -v, --verbose\n\
423 Verbose output. Up to 3 levels\n"));
425 printf (_("\n\
426 On Solaris, if -a specified, uses swap -l, otherwise uses swap -s.\n\
427 Will be discrepencies because swap -s counts allocated swap and includes\n\
428 real memory\n"));
429 printf (_("\n\
430 On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s.\n"));
432 printf (_(UT_SUPPORT));
433 }
438 void
439 print_usage (void)
440 {
441 printf (_("Usage:\n\
442 %s [-av] -w <percent_free>%% -c <percent_free>%%\n\
443 %s [-av] -w <bytes_free> -c <bytes_free>\n\
444 %s (-h | --help) for detailed help\n\
445 %s (-V | --version) for version information\n"),
446 progname, progname, progname, progname);
447 }