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 float warn_size = 0;
46 float 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, *tmp_status;
84 setlocale (LC_ALL, "");
85 bindtextdomain (PACKAGE, LOCALEDIR);
86 textdomain (PACKAGE);
88 status = strdup ("");
89 tmp_status = strdup ("");
90 perf = strdup ("");
92 if (process_arguments (argc, argv) == ERROR)
93 usage4 (_("Could not parse arguments"));
95 #ifdef HAVE_PROC_MEMINFO
96 fp = fopen (PROC_MEMINFO, "r");
97 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
98 if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %f %f %f", &dsktotal, &dskused, &dskfree) == 3) {
99 dsktotal = dsktotal / 1048576;
100 dskused = dskused / 1048576;
101 dskfree = dskfree / 1048576;
102 total_swap += dsktotal;
103 used_swap += dskused;
104 free_swap += dskfree;
105 if (allswaps) {
106 if (dsktotal == 0)
107 percent=100.0;
108 else
109 percent = 100 * (((double) dskused) / ((double) dsktotal));
110 result = max_state (result, check_swap (percent, dskfree));
111 if (verbose)
112 asprintf (&status, "%s [%.0f (%d%%)]", status, dskfree, 100 - percent);
113 }
114 }
115 else if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%[TotalFre]%*[:] %f %*[k]%*[B]", str, &tmp)) {
116 if (strcmp ("Total", str) == 0) {
117 dsktotal = tmp / 1024;
118 }
119 else if (strcmp ("Free", str) == 0) {
120 dskfree = tmp / 1024;
121 }
122 }
123 }
124 fclose(fp);
125 dskused = dsktotal - dskfree;
126 total_swap = dsktotal;
127 used_swap = dskused;
128 free_swap = dskfree;
129 #else
130 # ifdef HAVE_SWAP
131 asprintf(&swap_command, "%s", SWAP_COMMAND);
132 asprintf(&swap_format, "%s", SWAP_FORMAT);
134 /* These override the command used if a summary (and thus ! allswaps) is required */
135 /* The summary flag returns more accurate information about swap usage on these OSes */
136 # ifdef _AIX
137 if (!allswaps) {
138 asprintf(&swap_command, "%s", "/usr/sbin/lsps -s");
139 asprintf(&swap_format, "%s", "%d%*s %d");
140 conv_factor = 1;
141 }
142 # endif
144 if (verbose >= 2)
145 printf (_("Command: %s\n"), swap_command);
146 if (verbose >= 3)
147 printf (_("Format: %s\n"), swap_format);
149 child_process = spopen (swap_command);
150 if (child_process == NULL) {
151 printf (_("Could not open pipe: %s\n"), swap_command);
152 return STATE_UNKNOWN;
153 }
155 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
156 if (child_stderr == NULL)
157 printf (_("Could not open stderr for %s\n"), swap_command);
159 sprintf (str, "%s", "");
160 /* read 1st line */
161 fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
162 if (strcmp (swap_format, "") == 0) {
163 temp_buffer = strtok (input_buffer, " \n");
164 while (temp_buffer) {
165 if (strstr (temp_buffer, "blocks"))
166 sprintf (str, "%s %s", str, "%f");
167 else if (strstr (temp_buffer, "dskfree"))
168 sprintf (str, "%s %s", str, "%f");
169 else
170 sprintf (str, "%s %s", str, "%*s");
171 temp_buffer = strtok (NULL, " \n");
172 }
173 }
175 /* If different swap command is used for summary switch, need to read format differently */
176 # ifdef _AIX
177 if (!allswaps) {
178 fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process); /* Ignore first line */
179 sscanf (input_buffer, swap_format, &total_swap, &used_swap);
180 free_swap = total_swap * (100 - used_swap) /100;
181 used_swap = total_swap - free_swap;
182 if (verbose >= 3)
183 printf (_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap, used_swap, free_swap);
184 } else {
185 # endif
186 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
187 sscanf (input_buffer, swap_format, &dsktotal, &dskfree);
189 dsktotal = dsktotal / conv_factor;
190 /* AIX lists percent used, so this converts to dskfree in MBs */
191 # ifdef _AIX
192 dskfree = dsktotal * (100 - dskfree) / 100;
193 # else
194 dskfree = dskfree / conv_factor;
195 # endif
196 if (verbose >= 3)
197 printf (_("total=%.0f, free=%.0f\n"), dsktotal, dskfree);
199 dskused = dsktotal - dskfree;
200 total_swap += dsktotal;
201 used_swap += dskused;
202 free_swap += dskfree;
203 if (allswaps) {
204 percent = 100 * (((double) dskused) / ((double) dsktotal));
205 result = max_state (result, check_swap (percent, dskfree));
206 if (verbose)
207 asprintf (&status, "%s [%.0f (%d%%)]", status, dskfree, 100 - percent);
208 }
209 }
210 # ifdef _AIX
211 }
212 # endif
214 /* If we get anything on STDERR, at least set warning */
215 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
216 result = max_state (result, STATE_WARNING);
218 /* close stderr */
219 (void) fclose (child_stderr);
221 /* close the pipe */
222 if (spclose (child_process))
223 result = max_state (result, STATE_WARNING);
224 # else
225 # ifdef CHECK_SWAP_SWAPCTL_SVR4
227 /* get the number of active swap devices */
228 nswaps=swapctl(SC_GETNSWP, NULL);
230 /* initialize swap table + entries */
231 tbl=(swaptbl_t*)malloc(sizeof(swaptbl_t)+(sizeof(swapent_t)*nswaps));
232 memset(tbl, 0, sizeof(swaptbl_t)+(sizeof(swapent_t)*nswaps));
233 tbl->swt_n=nswaps;
234 for(i=0;i<nswaps;i++){
235 ent=&tbl->swt_ent[i];
236 ent->ste_path=(char*)malloc(sizeof(char)*MAXPATHLEN);
237 }
239 /* and now, tally 'em up */
240 swapctl_res=swapctl(SC_LIST, tbl);
241 if(swapctl_res < 0){
242 perror(_("swapctl failed: "));
243 result = STATE_WARNING;
244 }
246 for(i=0;i<nswaps;i++){
247 dsktotal = (float) tbl->swt_ent[i].ste_pages / SWAP_CONVERSION;
248 dskfree = (float) tbl->swt_ent[i].ste_free / SWAP_CONVERSION;
249 dskused = ( dsktotal - dskfree );
251 if (verbose >= 3)
252 printf ("dsktotal=%.0f dskfree=%.0f dskused=%.0f\n", dsktotal, dskfree, dskused);
254 if(allswaps && dsktotal > 0){
255 percent = 100 * (((double) dskused) / ((double) dsktotal));
256 result = max_state (result, check_swap (percent, dskfree));
257 if (verbose) {
258 asprintf (&status, "%s [%.0f (%d%%)]", status, dskfree, 100 - percent);
259 }
260 }
262 total_swap += dsktotal;
263 free_swap += dskfree;
264 used_swap += dskused;
265 }
267 /* and clean up after ourselves */
268 for(i=0;i<nswaps;i++){
269 free(tbl->swt_ent[i].ste_path);
270 }
271 free(tbl);
272 # else
273 # ifdef CHECK_SWAP_SWAPCTL_BSD
275 /* get the number of active swap devices */
276 nswaps=swapctl(SWAP_NSWAP, NULL, 0);
278 /* initialize swap table + entries */
279 ent=(struct swapent*)malloc(sizeof(struct swapent)*nswaps);
281 /* and now, tally 'em up */
282 swapctl_res=swapctl(SWAP_STATS, ent, nswaps);
283 if(swapctl_res < 0){
284 perror(_("swapctl failed: "));
285 result = STATE_WARNING;
286 }
288 for(i=0;i<nswaps;i++){
289 dsktotal = (float) ent->se_nblks / conv_factor;
290 dskused = (float) ent->se_inuse / conv_factor;
291 dskfree = ( dsktotal - dskused );
293 if(allswaps && dsktotal > 0){
294 percent = 100 * (((double) dskused) / ((double) dsktotal));
295 result = max_state (result, check_swap (percent, dskfree));
296 if (verbose) {
297 asprintf (&status, "%s [%.0f (%d%%)]", status, dskfree, 100 - percent);
298 }
299 }
301 total_swap += dsktotal;
302 free_swap += dskfree;
303 used_swap += dskused;
304 }
306 /* and clean up after ourselves */
307 free(ent);
309 # endif /* CHECK_SWAP_SWAPCTL_BSD */
310 # endif /* CHECK_SWAP_SWAPCTL_SVR4 */
311 # endif /* HAVE_SWAP */
312 #endif /* HAVE_PROC_MEMINFO */
314 percent_used = 100 * ((double) used_swap) / ((double) total_swap);
315 result = max_state (result, check_swap (percent_used, free_swap));
316 /* broken into two steps because of funkiness with builtin asprintf */
317 asprintf (&tmp_status, _(" %d%% free (%.0f MB out of %.0f MB)"),
318 (100 - percent_used), free_swap, total_swap);
319 asprintf (&status, "%s%s", tmp_status, status);
321 asprintf (&perf, "%s", perfdata ("swap", (long) free_swap, "MB",
322 TRUE, (long) max (warn_size/1024, warn_percent/100.0*total_swap),
323 TRUE, (long) max (crit_size/1024, crit_percent/100.0*total_swap),
324 TRUE, 0,
325 TRUE, (long) total_swap));
326 printf ("SWAP %s:%s |%s\n", state_text (result), status, perf);
327 return result;
328 }
332 int
333 check_swap (int usp, float free_swap)
334 {
335 int result = STATE_UNKNOWN;
336 free_swap = free_swap * 1024; /* Convert back to bytes as warn and crit specified in bytes */
337 if (usp >= 0 && crit_percent != 0 && usp >= (100.0 - crit_percent))
338 result = STATE_CRITICAL;
339 else if (crit_size > 0 && free_swap <= crit_size)
340 result = STATE_CRITICAL;
341 else if (usp >= 0 && warn_percent != 0 && usp >= (100.0 - warn_percent))
342 result = STATE_WARNING;
343 else if (warn_size > 0 && free_swap <= warn_size)
344 result = STATE_WARNING;
345 else if (usp >= 0.0)
346 result = STATE_OK;
347 return result;
348 }
352 /* process command-line arguments */
353 int
354 process_arguments (int argc, char **argv)
355 {
356 int c = 0; /* option character */
358 int option = 0;
359 static struct option longopts[] = {
360 {"warning", required_argument, 0, 'w'},
361 {"critical", required_argument, 0, 'c'},
362 {"allswaps", no_argument, 0, 'a'},
363 {"verbose", no_argument, 0, 'v'},
364 {"version", no_argument, 0, 'V'},
365 {"help", no_argument, 0, 'h'},
366 {0, 0, 0, 0}
367 };
369 if (argc < 2)
370 return ERROR;
372 while (1) {
373 c = getopt_long (argc, argv, "+?Vvhac:w:", longopts, &option);
375 if (c == -1 || c == EOF)
376 break;
378 switch (c) {
379 case 'w': /* warning size threshold */
380 if (is_intnonneg (optarg)) {
381 warn_size = (float) atoi (optarg);
382 break;
383 }
384 else if (strstr (optarg, ",") &&
385 strstr (optarg, "%") &&
386 sscanf (optarg, "%.0f,%d%%", &warn_size, &warn_percent) == 2) {
387 break;
388 }
389 else if (strstr (optarg, "%") &&
390 sscanf (optarg, "%d%%", &warn_percent) == 1) {
391 break;
392 }
393 else {
394 usage4 (_("Warning threshold must be integer or percentage!"));
395 }
396 case 'c': /* critical size threshold */
397 if (is_intnonneg (optarg)) {
398 crit_size = (float) atoi (optarg);
399 break;
400 }
401 else if (strstr (optarg, ",") &&
402 strstr (optarg, "%") &&
403 sscanf (optarg, "%.0f,%d%%", &crit_size, &crit_percent) == 2) {
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 = (float) atoi (argv[c++]);
446 if (c == argc)
447 return validate_arguments ();
448 if (crit_size == 0 && is_intnonneg (argv[c]))
449 crit_size = 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 }