Code

Updated help file to remove swap -s reference (Sivakumar Nellurandi)
[nagiosplug.git] / plugins / check_swap.c
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         /* if total_swap == 0, let's not divide by 0 */
314         if(total_swap) {
315                 percent_used = 100 * ((double) used_swap) / ((double) total_swap);
316         } else {
317                 percent_used = 0;
318         }
320         result = max_state (result, check_swap (percent_used, free_swap));
321         printf (_("SWAP %s - %d%% free (%.0f MB out of %.0f MB) %s|"),
322                         state_text (result),
323                         (100 - percent_used), free_swap, total_swap, status);
325         puts (perfdata ("swap", (long) free_swap, "MB",
326                         TRUE, (long) max (warn_size/1024, warn_percent/100.0*total_swap),
327                         TRUE, (long) max (crit_size/1024, crit_percent/100.0*total_swap),
328                         TRUE, 0,
329                         TRUE, (long) total_swap));
331         return result;
336 int
337 check_swap (int usp, float free_swap)
339         int result = STATE_UNKNOWN;
340         free_swap = free_swap * 1024;           /* Convert back to bytes as warn and crit specified in bytes */
341         if (usp >= 0 && crit_percent != 0 && usp >= (100.0 - crit_percent))
342                 result = STATE_CRITICAL;
343         else if (crit_size > 0 && free_swap <= crit_size)
344                 result = STATE_CRITICAL;
345         else if (usp >= 0 && warn_percent != 0 && usp >= (100.0 - warn_percent))
346                 result = STATE_WARNING;
347         else if (warn_size > 0 && free_swap <= warn_size)
348                 result = STATE_WARNING;
349         else if (usp >= 0.0)
350                 result = STATE_OK;
351         return result;
356 /* process command-line arguments */
357 int
358 process_arguments (int argc, char **argv)
360         int c = 0;  /* option character */
362         int option = 0;
363         static struct option longopts[] = {
364                 {"warning", required_argument, 0, 'w'},
365                 {"critical", required_argument, 0, 'c'},
366                 {"allswaps", no_argument, 0, 'a'},
367                 {"verbose", no_argument, 0, 'v'},
368                 {"version", no_argument, 0, 'V'},
369                 {"help", no_argument, 0, 'h'},
370                 {0, 0, 0, 0}
371         };
373         if (argc < 2)
374                 return ERROR;
376         while (1) {
377                 c = getopt_long (argc, argv, "+?Vvhac:w:", longopts, &option);
379                 if (c == -1 || c == EOF)
380                         break;
382                 switch (c) {
383                 case 'w':                                                                       /* warning size threshold */
384                         if (is_intnonneg (optarg)) {
385                                 warn_size = (double) atoi (optarg);
386                                 break;
387                         }
388                         else if (strstr (optarg, ",") &&
389                                                          strstr (optarg, "%") &&
390                                                          sscanf (optarg, "%g,%d%%", &warn_size, &warn_percent) == 2) {
391                                 warn_size = floor(warn_size);
392                                 break;
393                         }
394                         else if (strstr (optarg, "%") &&
395                                                          sscanf (optarg, "%d%%", &warn_percent) == 1) {
396                                 break;
397                         }
398                         else {
399                                 usage4 (_("Warning threshold must be integer or percentage!"));
400                         }
401                 case 'c':                                                                       /* critical size threshold */
402                         if (is_intnonneg (optarg)) {
403                                 crit_size = (double) atoi (optarg);
404                                 break;
405                         }
406                         else if (strstr (optarg, ",") &&
407                                                          strstr (optarg, "%") &&
408                                                          sscanf (optarg, "%g,%d%%", &crit_size, &crit_percent) == 2) {
409                                 crit_size = floor(crit_size);
410                                 break;
411                         }
412                         else if (strstr (optarg, "%") &&
413                                                          sscanf (optarg, "%d%%", &crit_percent) == 1) {
414                                 break;
415                         }
416                         else {
417                                 usage4 (_("Critical threshold must be integer or percentage!"));
418                         }
419                 case 'a':                                                                       /* all swap */
420                         allswaps = TRUE;
421                         break;
422                 case 'v':                                                                       /* verbose */
423                         verbose++;
424                         break;
425                 case 'V':                                                                       /* version */
426                         print_revision (progname, revision);
427                         exit (STATE_OK);
428                 case 'h':                                                                       /* help */
429                         print_help ();
430                         exit (STATE_OK);
431                 case '?':                                                                       /* error */
432                         usage2 (_("Unknown argument"), optarg);
433                 }
434         }
436         c = optind;
437         if (c == argc)
438                 return validate_arguments ();
439         if (warn_percent == 0 && is_intnonneg (argv[c]))
440                 warn_percent = atoi (argv[c++]);
442         if (c == argc)
443                 return validate_arguments ();
444         if (crit_percent == 0 && is_intnonneg (argv[c]))
445                 crit_percent = atoi (argv[c++]);
447         if (c == argc)
448                 return validate_arguments ();
449         if (warn_size == 0 && is_intnonneg (argv[c]))
450                 warn_size = (double) atoi (argv[c++]);
452         if (c == argc)
453                 return validate_arguments ();
454         if (crit_size == 0 && is_intnonneg (argv[c]))
455                 crit_size = (double) atoi (argv[c++]);
457         return validate_arguments ();
462 int
463 validate_arguments (void)
465         if (warn_percent == 0 && crit_percent == 0 && warn_size == 0
466                         && crit_size == 0) {
467                 return ERROR;
468         }
469         else if (warn_percent < crit_percent) {
470                 usage4 
471                         (_("Warning percentage should be more than critical percentage"));
472         }
473         else if (warn_size < crit_size) {
474                 usage4
475                         (_("Warning free space should be more than critical free space"));
476         }
477         return OK;
482 void
483 print_help (void)
485         print_revision (progname, revision);
487         printf (_(COPYRIGHT), copyright, email);
489         printf (_("Check swap space on local machine.\n\n"));
491         print_usage ();
493         printf (_(UT_HELP_VRSN));
495         printf (_("\n\
496  -w, --warning=INTEGER\n\
497    Exit with WARNING status if less than INTEGER bytes of swap space are free\n\
498  -w, --warning=PERCENT%%\n\
499    Exit with WARNING status if less than PERCENT of swap space is free\n\
500  -c, --critical=INTEGER\n\
501    Exit with CRITICAL status if less than INTEGER bytes of swap space are free\n\
502  -c, --critical=PERCENT%%\n\
503    Exit with CRITCAL status if less than PERCENT of swap space is free\n\
504  -a, --allswaps\n\
505     Conduct comparisons for all swap partitions, one by one\n\
506  -v, --verbose\n\
507     Verbose output. Up to 3 levels\n"));
509         printf (_("\n\
510 On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s.\n"));
512         printf (_(UT_SUPPORT));
517 void
518 print_usage (void)
520         printf ("\
521 Usage: %s [-av] -w <percent_free>%% -c <percent_free>%%\n\
522        %s [-av] -w <bytes_free> -c <bytes_free>\n", progname, progname);