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 = 0, dskused = 0, dskfree = 0, tmp = 0;
56 int result = STATE_OK;
57 char input_buffer[MAX_INPUT_BUFFER];
58 char *perf;
59 int conv_factor = SWAP_CONVERSION;
60 #ifdef HAVE_PROC_MEMINFO
61 FILE *fp;
62 #else
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) != OK)
93 usage (_("Invalid command arguments supplied\n"));
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]%*[:] %llu %llu %llu", &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 [%llu (%d%%)]", status, dskfree, 100 - percent);
113 }
114 }
115 else if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%[TotalFre]%*[:] %llu %*[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=%d, used=%d, free=%d\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=%d, free=%d\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 [%llu (%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 = tbl->swt_ent[i].ste_pages / SWAP_CONVERSION;
248 dskfree = tbl->swt_ent[i].ste_free / SWAP_CONVERSION;
249 dskused = ( dsktotal - dskfree );
251 if(allswaps && dsktotal > 0){
252 percent = 100 * (((double) dskused) / ((double) dsktotal));
253 result = max_state (result, check_swap (percent, dskfree));
254 if (verbose) {
255 asprintf (&status, "%s [%d (%d%%)]", status, (int)dskfree, 100 - percent);
256 }
257 }
259 total_swap += dsktotal;
260 free_swap += dskfree;
261 used_swap += dskused;
262 }
264 /* and clean up after ourselves */
265 for(i=0;i<nswaps;i++){
266 free(tbl->swt_ent[i].ste_path);
267 }
268 free(tbl);
269 # else
270 # ifdef CHECK_SWAP_SWAPCTL_BSD
272 /* get the number of active swap devices */
273 nswaps=swapctl(SWAP_NSWAP, NULL, 0);
275 /* initialize swap table + entries */
276 ent=(struct swapent*)malloc(sizeof(struct swapent)*nswaps);
278 /* and now, tally 'em up */
279 swapctl_res=swapctl(SWAP_STATS, ent, nswaps);
280 if(swapctl_res < 0){
281 perror("swapctl failed: ");
282 result = STATE_WARNING;
283 }
285 for(i=0;i<nswaps;i++){
286 dsktotal = ent->se_nblks / conv_factor;
287 dskused = ent->se_inuse / conv_factor;
288 dskfree = ( dsktotal - dskused );
290 if(allswaps && dsktotal > 0){
291 percent = 100 * (((double) dskused) / ((double) dsktotal));
292 result = max_state (result, check_swap (percent, dskfree));
293 if (verbose) {
294 asprintf (&status, "%s [%d (%d%%)]", status, (int)dskfree, 100 - percent);
295 }
296 }
298 total_swap += dsktotal;
299 free_swap += dskfree;
300 used_swap += dskused;
301 }
303 /* and clean up after ourselves */
304 free(ent);
306 # endif /* CHECK_SWAP_SWAPCTL_BSD */
307 # endif /* CHECK_SWAP_SWAPCTL_SVR4 */
308 # endif /* HAVE_SWAP */
309 #endif /* HAVE_PROC_MEMINFO */
311 percent_used = 100 * ((double) used_swap) / ((double) total_swap);
312 result = max_state (result, check_swap (percent_used, free_swap));
313 /* broken into two steps because of funkiness with builtin asprintf */
314 asprintf (&tmp_status, _(" %d%% free (%llu MB out of %llu MB)"),
315 (100 - percent_used), free_swap, total_swap);
316 asprintf (&status, "%s%s", tmp_status, status);
318 asprintf (&perf, "%s", perfdata ("swap", (long) free_swap, "MB",
319 TRUE, (long) max (warn_size/1024, warn_percent/100.0*total_swap),
320 TRUE, (long) max (crit_size/1024, crit_percent/100.0*total_swap),
321 TRUE, 0,
322 TRUE, (long) total_swap));
323 printf ("SWAP %s:%s |%s\n", state_text (result), status, perf);
324 return result;
325 }
329 \f
330 int
331 check_swap (int usp, long unsigned int 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 }
349 /* process command-line arguments */
350 int
351 process_arguments (int argc, char **argv)
352 {
353 int c = 0; /* option character */
355 int option = 0;
356 static struct option longopts[] = {
357 {"warning", required_argument, 0, 'w'},
358 {"critical", required_argument, 0, 'c'},
359 {"allswaps", no_argument, 0, 'a'},
360 {"verbose", no_argument, 0, 'v'},
361 {"version", no_argument, 0, 'V'},
362 {"help", no_argument, 0, 'h'},
363 {0, 0, 0, 0}
364 };
366 if (argc < 2)
367 return ERROR;
369 while (1) {
370 c = getopt_long (argc, argv, "+?Vvhac:w:", longopts, &option);
372 if (c == -1 || c == EOF)
373 break;
375 switch (c) {
376 case 'w': /* warning size threshold */
377 if (is_intnonneg (optarg)) {
378 warn_size = atoi (optarg);
379 break;
380 }
381 else if (strstr (optarg, ",") &&
382 strstr (optarg, "%") &&
383 sscanf (optarg, "%llu,%d%%", &warn_size, &warn_percent) == 2) {
384 break;
385 }
386 else if (strstr (optarg, "%") &&
387 sscanf (optarg, "%d%%", &warn_percent) == 1) {
388 break;
389 }
390 else {
391 usage (_("Warning threshold must be integer or percentage!\n"));
392 }
393 case 'c': /* critical size threshold */
394 if (is_intnonneg (optarg)) {
395 crit_size = atoi (optarg);
396 break;
397 }
398 else if (strstr (optarg, ",") &&
399 strstr (optarg, "%") &&
400 sscanf (optarg, "%llu,%d%%", &crit_size, &crit_percent) == 2) {
401 break;
402 }
403 else if (strstr (optarg, "%") &&
404 sscanf (optarg, "%d%%", &crit_percent) == 1) {
405 break;
406 }
407 else {
408 usage (_("Critical threshold must be integer or percentage!\n"));
409 }
410 case 'a': /* all swap */
411 allswaps = TRUE;
412 break;
413 case 'v': /* verbose */
414 verbose++;
415 break;
416 case 'V': /* version */
417 print_revision (progname, revision);
418 exit (STATE_OK);
419 case 'h': /* help */
420 print_help ();
421 exit (STATE_OK);
422 case '?': /* error */
423 usage (_("Invalid argument\n"));
424 }
425 }
427 c = optind;
428 if (c == argc)
429 return validate_arguments ();
430 if (warn_percent == 0 && is_intnonneg (argv[c]))
431 warn_percent = atoi (argv[c++]);
433 if (c == argc)
434 return validate_arguments ();
435 if (crit_percent == 0 && is_intnonneg (argv[c]))
436 crit_percent = atoi (argv[c++]);
438 if (c == argc)
439 return validate_arguments ();
440 if (warn_size == 0 && is_intnonneg (argv[c]))
441 warn_size = atoi (argv[c++]);
443 if (c == argc)
444 return validate_arguments ();
445 if (crit_size == 0 && is_intnonneg (argv[c]))
446 crit_size = atoi (argv[c++]);
448 return validate_arguments ();
449 }
455 int
456 validate_arguments (void)
457 {
458 if (warn_percent == 0 && crit_percent == 0 && warn_size == 0
459 && crit_size == 0) {
460 return ERROR;
461 }
462 else if (warn_percent < crit_percent) {
463 usage
464 (_("Warning percentage should be more than critical percentage\n"));
465 }
466 else if (warn_size < crit_size) {
467 usage
468 (_("Warning free space should be more than critical free space\n"));
469 }
470 return OK;
471 }
477 \f
478 void
479 print_help (void)
480 {
481 print_revision (progname, revision);
483 printf (_(COPYRIGHT), copyright, email);
485 printf (_("Check swap space on local server.\n\n"));
487 print_usage ();
489 printf (_(UT_HELP_VRSN));
491 printf (_("\n\
492 -w, --warning=INTEGER\n\
493 Exit with WARNING status if less than INTEGER bytes of swap space are free\n\
494 -w, --warning=PERCENT%%\n\
495 Exit with WARNING status if less than PERCENT of swap space is free\n\
496 -c, --critical=INTEGER\n\
497 Exit with CRITICAL status if less than INTEGER bytes of swap space are free\n\
498 -c, --critical=PERCENT%%\n\
499 Exit with CRITCAL status if less than PERCENT of swap space is free\n\
500 -a, --allswaps\n\
501 Conduct comparisons for all swap partitions, one by one\n\
502 -v, --verbose\n\
503 Verbose output. Up to 3 levels\n"));
505 printf (_("\n\
506 On Solaris, if -a specified, uses swap -l, otherwise uses swap -s.\n\
507 Will be discrepencies because swap -s counts allocated swap and includes\n\
508 real memory\n"));
509 printf (_("\n\
510 On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s.\n"));
512 printf (_(UT_SUPPORT));
513 }
518 void
519 print_usage (void)
520 {
521 printf (_("Usage:\n\
522 %s [-av] -w <percent_free>%% -c <percent_free>%%\n\
523 %s [-av] -w <bytes_free> -c <bytes_free>\n\
524 %s (-h | --help) for detailed help\n\
525 %s (-V | --version) for version information\n"),
526 progname, progname, progname, progname);
527 }