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 const char *progname = "check_swap";
29 const char *revision = "$Revision$";
30 const char *copyright = "2000-2004";
31 const char *email = "nagiosplug-devel@lists.sourceforge.net";
33 #include "common.h"
34 #include "popen.h"
35 #include "utils.h"
37 int check_swap (int usp, float 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 double warn_size = 0;
46 double crit_size = 0;
47 int verbose;
48 int allswaps;
50 int
51 main (int argc, char **argv)
52 {
53 int percent_used, percent;
54 float total_swap = 0, used_swap = 0, free_swap = 0;
55 float dsktotal = 0, dskused = 0, dskfree = 0, tmp = 0;
56 int result = STATE_UNKNOWN;
57 char input_buffer[MAX_INPUT_BUFFER];
58 char *perf;
59 #ifdef HAVE_PROC_MEMINFO
60 FILE *fp;
61 #else
62 int conv_factor = SWAP_CONVERSION;
63 # ifdef HAVE_SWAP
64 char *temp_buffer;
65 char *swap_command;
66 char *swap_format;
67 # else
68 # ifdef HAVE_DECL_SWAPCTL
69 int i=0, nswaps=0, swapctl_res=0;
70 # ifdef CHECK_SWAP_SWAPCTL_SVR4
71 swaptbl_t *tbl=NULL;
72 swapent_t *ent=NULL;
73 # else
74 # ifdef CHECK_SWAP_SWAPCTL_BSD
75 struct swapent *ent;
76 # endif /* CHECK_SWAP_SWAPCTL_BSD */
77 # endif /* CHECK_SWAP_SWAPCTL_SVR4 */
78 # endif /* HAVE_DECL_SWAPCTL */
79 # endif
80 #endif
81 char str[32];
82 char *status;
84 setlocale (LC_ALL, "");
85 bindtextdomain (PACKAGE, LOCALEDIR);
86 textdomain (PACKAGE);
88 status = strdup ("");
89 perf = strdup ("");
91 if (process_arguments (argc, argv) == ERROR)
92 usage4 (_("Could not parse arguments"));
94 #ifdef HAVE_PROC_MEMINFO
95 fp = fopen (PROC_MEMINFO, "r");
96 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
97 if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %f %f %f", &dsktotal, &dskused, &dskfree) == 3) {
98 dsktotal = dsktotal / 1048576;
99 dskused = dskused / 1048576;
100 dskfree = dskfree / 1048576;
101 total_swap += dsktotal;
102 used_swap += dskused;
103 free_swap += dskfree;
104 if (allswaps) {
105 if (dsktotal == 0)
106 percent=100.0;
107 else
108 percent = 100 * (((double) dskused) / ((double) dsktotal));
109 result = max_state (result, check_swap (percent, dskfree));
110 if (verbose)
111 asprintf (&status, "%s [%.0f (%d%%)]", status, dskfree, 100 - percent);
112 }
113 }
114 else if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%[TotalFre]%*[:] %f %*[k]%*[B]", str, &tmp)) {
115 if (strcmp ("Total", str) == 0) {
116 dsktotal = tmp / 1024;
117 }
118 else if (strcmp ("Free", str) == 0) {
119 dskfree = tmp / 1024;
120 }
121 }
122 }
123 fclose(fp);
124 dskused = dsktotal - dskfree;
125 total_swap = dsktotal;
126 used_swap = dskused;
127 free_swap = dskfree;
128 #else
129 # ifdef HAVE_SWAP
130 asprintf(&swap_command, "%s", SWAP_COMMAND);
131 asprintf(&swap_format, "%s", SWAP_FORMAT);
133 /* These override the command used if a summary (and thus ! allswaps) is required */
134 /* The summary flag returns more accurate information about swap usage on these OSes */
135 # ifdef _AIX
136 if (!allswaps) {
137 asprintf(&swap_command, "%s", "/usr/sbin/lsps -s");
138 asprintf(&swap_format, "%s", "%f%*s %f");
139 conv_factor = 1;
140 }
141 # endif
143 if (verbose >= 2)
144 printf (_("Command: %s\n"), swap_command);
145 if (verbose >= 3)
146 printf (_("Format: %s\n"), swap_format);
148 child_process = spopen (swap_command);
149 if (child_process == NULL) {
150 printf (_("Could not open pipe: %s\n"), swap_command);
151 return STATE_UNKNOWN;
152 }
154 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
155 if (child_stderr == NULL)
156 printf (_("Could not open stderr for %s\n"), swap_command);
158 sprintf (str, "%s", "");
159 /* read 1st line */
160 fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
161 if (strcmp (swap_format, "") == 0) {
162 temp_buffer = strtok (input_buffer, " \n");
163 while (temp_buffer) {
164 if (strstr (temp_buffer, "blocks"))
165 sprintf (str, "%s %s", str, "%f");
166 else if (strstr (temp_buffer, "dskfree"))
167 sprintf (str, "%s %s", str, "%f");
168 else
169 sprintf (str, "%s %s", str, "%*s");
170 temp_buffer = strtok (NULL, " \n");
171 }
172 }
174 /* If different swap command is used for summary switch, need to read format differently */
175 # ifdef _AIX
176 if (!allswaps) {
177 fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process); /* Ignore first line */
178 sscanf (input_buffer, swap_format, &total_swap, &used_swap);
179 free_swap = total_swap * (100 - used_swap) /100;
180 used_swap = total_swap - free_swap;
181 if (verbose >= 3)
182 printf (_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap, used_swap, free_swap);
183 } else {
184 # endif
185 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
186 sscanf (input_buffer, swap_format, &dsktotal, &dskfree);
188 dsktotal = dsktotal / conv_factor;
189 /* AIX lists percent used, so this converts to dskfree in MBs */
190 # ifdef _AIX
191 dskfree = dsktotal * (100 - dskfree) / 100;
192 # else
193 dskfree = dskfree / conv_factor;
194 # endif
195 if (verbose >= 3)
196 printf (_("total=%.0f, free=%.0f\n"), dsktotal, dskfree);
198 dskused = dsktotal - dskfree;
199 total_swap += dsktotal;
200 used_swap += dskused;
201 free_swap += dskfree;
202 if (allswaps) {
203 percent = 100 * (((double) dskused) / ((double) dsktotal));
204 result = max_state (result, check_swap (percent, dskfree));
205 if (verbose)
206 asprintf (&status, "%s [%.0f (%d%%)]", status, dskfree, 100 - percent);
207 }
208 }
209 # ifdef _AIX
210 }
211 # endif
213 /* If we get anything on STDERR, at least set warning */
214 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
215 result = max_state (result, STATE_WARNING);
217 /* close stderr */
218 (void) fclose (child_stderr);
220 /* close the pipe */
221 if (spclose (child_process))
222 result = max_state (result, STATE_WARNING);
223 # else
224 # ifdef CHECK_SWAP_SWAPCTL_SVR4
226 /* get the number of active swap devices */
227 nswaps=swapctl(SC_GETNSWP, NULL);
229 /* initialize swap table + entries */
230 tbl=(swaptbl_t*)malloc(sizeof(swaptbl_t)+(sizeof(swapent_t)*nswaps));
231 memset(tbl, 0, sizeof(swaptbl_t)+(sizeof(swapent_t)*nswaps));
232 tbl->swt_n=nswaps;
233 for(i=0;i<nswaps;i++){
234 ent=&tbl->swt_ent[i];
235 ent->ste_path=(char*)malloc(sizeof(char)*MAXPATHLEN);
236 }
238 /* and now, tally 'em up */
239 swapctl_res=swapctl(SC_LIST, tbl);
240 if(swapctl_res < 0){
241 perror(_("swapctl failed: "));
242 result = STATE_WARNING;
243 }
245 for(i=0;i<nswaps;i++){
246 dsktotal = (float) tbl->swt_ent[i].ste_pages / SWAP_CONVERSION;
247 dskfree = (float) tbl->swt_ent[i].ste_free / SWAP_CONVERSION;
248 dskused = ( dsktotal - dskfree );
250 if (verbose >= 3)
251 printf ("dsktotal=%.0f dskfree=%.0f dskused=%.0f\n", dsktotal, dskfree, dskused);
253 if(allswaps && dsktotal > 0){
254 percent = 100 * (((double) dskused) / ((double) dsktotal));
255 result = max_state (result, check_swap (percent, dskfree));
256 if (verbose) {
257 asprintf (&status, "%s [%.0f (%d%%)]", status, dskfree, 100 - percent);
258 }
259 }
261 total_swap += dsktotal;
262 free_swap += dskfree;
263 used_swap += dskused;
264 }
266 /* and clean up after ourselves */
267 for(i=0;i<nswaps;i++){
268 free(tbl->swt_ent[i].ste_path);
269 }
270 free(tbl);
271 # else
272 # ifdef CHECK_SWAP_SWAPCTL_BSD
274 /* get the number of active swap devices */
275 nswaps=swapctl(SWAP_NSWAP, NULL, 0);
277 /* initialize swap table + entries */
278 ent=(struct swapent*)malloc(sizeof(struct swapent)*nswaps);
280 /* and now, tally 'em up */
281 swapctl_res=swapctl(SWAP_STATS, ent, nswaps);
282 if(swapctl_res < 0){
283 perror(_("swapctl failed: "));
284 result = STATE_WARNING;
285 }
287 for(i=0;i<nswaps;i++){
288 dsktotal = (float) ent->se_nblks / conv_factor;
289 dskused = (float) ent->se_inuse / conv_factor;
290 dskfree = ( dsktotal - dskused );
292 if(allswaps && dsktotal > 0){
293 percent = 100 * (((double) dskused) / ((double) dsktotal));
294 result = max_state (result, check_swap (percent, dskfree));
295 if (verbose) {
296 asprintf (&status, "%s [%.0f (%d%%)]", status, dskfree, 100 - percent);
297 }
298 }
300 total_swap += dsktotal;
301 free_swap += dskfree;
302 used_swap += dskused;
303 }
305 /* and clean up after ourselves */
306 free(ent);
308 # endif /* CHECK_SWAP_SWAPCTL_BSD */
309 # endif /* CHECK_SWAP_SWAPCTL_SVR4 */
310 # endif /* HAVE_SWAP */
311 #endif /* HAVE_PROC_MEMINFO */
313 percent_used = 100 * ((double) used_swap) / ((double) total_swap);
314 result = max_state (result, check_swap (percent_used, free_swap));
315 printf (_("SWAP %s - %d%% free (%.0f MB out of %.0f MB) %s|"),
316 state_text (result),
317 (100 - percent_used), free_swap, total_swap, status);
319 puts (perfdata ("swap", (long) free_swap, "MB",
320 TRUE, (long) max (warn_size/1024, warn_percent/100.0*total_swap),
321 TRUE, (long) max (crit_size/1024, crit_percent/100.0*total_swap),
322 TRUE, 0,
323 TRUE, (long) total_swap));
325 return result;
326 }
330 int
331 check_swap (int usp, float free_swap)
332 {
333 int result = STATE_UNKNOWN;
334 free_swap = free_swap * 1024; /* Convert back to bytes as warn and crit specified in bytes */
335 if (usp >= 0 && crit_percent != 0 && usp >= (100.0 - crit_percent))
336 result = STATE_CRITICAL;
337 else if (crit_size > 0 && free_swap <= crit_size)
338 result = STATE_CRITICAL;
339 else if (usp >= 0 && warn_percent != 0 && usp >= (100.0 - warn_percent))
340 result = STATE_WARNING;
341 else if (warn_size > 0 && free_swap <= warn_size)
342 result = STATE_WARNING;
343 else if (usp >= 0.0)
344 result = STATE_OK;
345 return result;
346 }
350 /* process command-line arguments */
351 int
352 process_arguments (int argc, char **argv)
353 {
354 int c = 0; /* option character */
356 int option = 0;
357 static struct option longopts[] = {
358 {"warning", required_argument, 0, 'w'},
359 {"critical", required_argument, 0, 'c'},
360 {"allswaps", no_argument, 0, 'a'},
361 {"verbose", no_argument, 0, 'v'},
362 {"version", no_argument, 0, 'V'},
363 {"help", no_argument, 0, 'h'},
364 {0, 0, 0, 0}
365 };
367 if (argc < 2)
368 return ERROR;
370 while (1) {
371 c = getopt_long (argc, argv, "+?Vvhac:w:", longopts, &option);
373 if (c == -1 || c == EOF)
374 break;
376 switch (c) {
377 case 'w': /* warning size threshold */
378 if (is_intnonneg (optarg)) {
379 warn_size = (double) atoi (optarg);
380 break;
381 }
382 else if (strstr (optarg, ",") &&
383 strstr (optarg, "%") &&
384 sscanf (optarg, "%g,%d%%", &warn_size, &warn_percent) == 2) {
385 warn_size = floor(warn_size);
386 break;
387 }
388 else if (strstr (optarg, "%") &&
389 sscanf (optarg, "%d%%", &warn_percent) == 1) {
390 break;
391 }
392 else {
393 usage4 (_("Warning threshold must be integer or percentage!"));
394 }
395 case 'c': /* critical size threshold */
396 if (is_intnonneg (optarg)) {
397 crit_size = (double) atoi (optarg);
398 break;
399 }
400 else if (strstr (optarg, ",") &&
401 strstr (optarg, "%") &&
402 sscanf (optarg, "%g,%d%%", &crit_size, &crit_percent) == 2) {
403 crit_size = floor(crit_size);
404 break;
405 }
406 else if (strstr (optarg, "%") &&
407 sscanf (optarg, "%d%%", &crit_percent) == 1) {
408 break;
409 }
410 else {
411 usage4 (_("Critical threshold must be integer or percentage!"));
412 }
413 case 'a': /* all swap */
414 allswaps = TRUE;
415 break;
416 case 'v': /* verbose */
417 verbose++;
418 break;
419 case 'V': /* version */
420 print_revision (progname, revision);
421 exit (STATE_OK);
422 case 'h': /* help */
423 print_help ();
424 exit (STATE_OK);
425 case '?': /* error */
426 usage2 (_("Unknown argument"), optarg);
427 }
428 }
430 c = optind;
431 if (c == argc)
432 return validate_arguments ();
433 if (warn_percent == 0 && is_intnonneg (argv[c]))
434 warn_percent = atoi (argv[c++]);
436 if (c == argc)
437 return validate_arguments ();
438 if (crit_percent == 0 && is_intnonneg (argv[c]))
439 crit_percent = atoi (argv[c++]);
441 if (c == argc)
442 return validate_arguments ();
443 if (warn_size == 0 && is_intnonneg (argv[c]))
444 warn_size = (double) atoi (argv[c++]);
446 if (c == argc)
447 return validate_arguments ();
448 if (crit_size == 0 && is_intnonneg (argv[c]))
449 crit_size = (double) atoi (argv[c++]);
451 return validate_arguments ();
452 }
456 int
457 validate_arguments (void)
458 {
459 if (warn_percent == 0 && crit_percent == 0 && warn_size == 0
460 && crit_size == 0) {
461 return ERROR;
462 }
463 else if (warn_percent < crit_percent) {
464 usage4
465 (_("Warning percentage should be more than critical percentage"));
466 }
467 else if (warn_size < crit_size) {
468 usage4
469 (_("Warning free space should be more than critical free space"));
470 }
471 return OK;
472 }
476 void
477 print_help (void)
478 {
479 print_revision (progname, revision);
481 printf (_(COPYRIGHT), copyright, email);
483 printf (_("Check swap space on local machine.\n\n"));
485 print_usage ();
487 printf (_(UT_HELP_VRSN));
489 printf (_("\n\
490 -w, --warning=INTEGER\n\
491 Exit with WARNING status if less than INTEGER bytes of swap space are free\n\
492 -w, --warning=PERCENT%%\n\
493 Exit with WARNING status if less than PERCENT of swap space is free\n\
494 -c, --critical=INTEGER\n\
495 Exit with CRITICAL status if less than INTEGER bytes of swap space are free\n\
496 -c, --critical=PERCENT%%\n\
497 Exit with CRITCAL status if less than PERCENT of swap space is free\n\
498 -a, --allswaps\n\
499 Conduct comparisons for all swap partitions, one by one\n\
500 -v, --verbose\n\
501 Verbose output. Up to 3 levels\n"));
503 printf (_("\n\
504 On Solaris, if -a specified, uses swap -l, otherwise uses swap -s.\n\
505 Will be discrepencies because swap -s counts allocated swap and includes\n\
506 real memory\n"));
507 printf (_("\n\
508 On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s.\n"));
510 printf (_(UT_SUPPORT));
511 }
515 void
516 print_usage (void)
517 {
518 printf ("\
519 Usage: %s [-av] -w <percent_free>%% -c <percent_free>%%\n\
520 %s [-av] -w <bytes_free> -c <bytes_free>\n", progname, progname);
521 }