Code

fix div by zero error when swaptotal is zero (Flo Gleixner)
[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 #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                                 if (dsktotal == 0)
94                                         percent=100.0;
95                                 else
96                                         percent = 100 * (((double) dskused) / ((double) dsktotal));
97                                 result = max_state (result, check_swap (percent, dskfree));
98                                 if (verbose)
99                                         asprintf (&status, "%s [%llu (%d%%)]", status, dskfree, 100 - percent);
100                         }
101                 }
102                 else if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%[TotalFre]%*[:] %llu %*[k]%*[B]", str, &tmp)) {
103                         if (strcmp ("Total", str) == 0) {
104                                 dsktotal = tmp / 1024;
105                         }
106                         else if (strcmp ("Free", str) == 0) {
107                                 dskfree = tmp / 1024;
108                         }
109                 }
110         }
111         fclose(fp);
112         dskused = dsktotal - dskfree;
113         total_swap = dsktotal;
114         used_swap = dskused;
115         free_swap = dskfree;
116 #else
117 # ifdef HAVE_SWAP
118         asprintf(&swap_command, "%s", SWAP_COMMAND);
119         asprintf(&swap_format, "%s", SWAP_FORMAT);
120         conv_factor = SWAP_CONVERSION;
122 /* These override the command used if a summary (and thus ! allswaps) is required */
123 /* The summary flag returns more accurate information about swap usage on these OSes */
124 #  ifdef _AIX
125         if (!allswaps) {
126                 asprintf(&swap_command, "%s", "/usr/sbin/lsps -s");
127                 asprintf(&swap_format, "%s", "%d%*s %d");
128                 conv_factor = 1;
129         }
130 #  else
131 #   ifdef sun
132         if (!allswaps) {
133                 asprintf(&swap_command, "%s", "/usr/sbin/swap -s");
134                 asprintf(&swap_format, "%s", "%*s %*dk %*s %*s + %*dk %*s = %dk %*s %dk %*s");
135                 conv_factor = 2048;
136         }
137 #   endif
138 #  endif
140         if (verbose >= 2)
141                 printf (_("Command: %s\n"), swap_command);
142         if (verbose >= 3)
143                 printf (_("Format: %s\n"), swap_format);
145         child_process = spopen (swap_command);
146         if (child_process == NULL) {
147                 printf (_("Could not open pipe: %s\n"), swap_command);
148                 return STATE_UNKNOWN;
149         }
151         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
152         if (child_stderr == NULL)
153                 printf (_("Could not open stderr for %s\n"), swap_command);
155         sprintf (str, "%s", "");
156         /* read 1st line */
157         fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
158         if (strcmp (swap_format, "") == 0) {
159                 temp_buffer = strtok (input_buffer, " \n");
160                 while (temp_buffer) {
161                         if (strstr (temp_buffer, "blocks"))
162                                 sprintf (str, "%s %s", str, "%f");
163                         else if (strstr (temp_buffer, "dskfree"))
164                                 sprintf (str, "%s %s", str, "%f");
165                         else
166                                 sprintf (str, "%s %s", str, "%*s");
167                         temp_buffer = strtok (NULL, " \n");
168                 }
169         }
171 /* If different swap command is used for summary switch, need to read format differently */
172 #  ifdef _AIX
173         if (!allswaps) {
174                 fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process);       /* Ignore first line */
175                 sscanf (input_buffer, swap_format, &total_swap, &used_swap);
176                 free_swap = total_swap * (100 - used_swap) /100;
177                 used_swap = total_swap - free_swap;
178                 if (verbose >= 3)
179                         printf (_("total=%d, used=%d, free=%d\n"), total_swap, used_swap, free_swap);
180         } else {
181 #  else
182 #   ifdef sun
183         if (!allswaps) {
184                 sscanf (input_buffer, swap_format, &used_swap, &free_swap);
185                 used_swap = used_swap / 1024;
186                 free_swap = free_swap / 1024;
187                 total_swap = used_swap + free_swap;
188         } else {
189 #   endif
190 #  endif
191                 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
192                         sscanf (input_buffer, swap_format, &dsktotal, &dskfree);
194                         dsktotal = dsktotal / conv_factor;
195                         /* AIX lists percent used, so this converts to dskfree in MBs */
196 #  ifdef _AIX
197                         dskfree = dsktotal * (100 - dskfree) / 100;
198 #  else
199                         dskfree = dskfree / conv_factor;
200 #  endif
201                         if (verbose >= 3)
202                                 printf (_("total=%d, free=%d\n"), dsktotal, dskfree);
204                         dskused = dsktotal - dskfree;
205                         total_swap += dsktotal;
206                         used_swap += dskused;
207                         free_swap += dskfree;
208                         if (allswaps) {
209                                 percent = 100 * (((double) dskused) / ((double) dsktotal));
210                                 result = max_state (result, check_swap (percent, dskfree));
211                                 if (verbose)
212                                         asprintf (&status, "%s [%llu (%d%%)]", status, dskfree, 100 - percent);
213                         }
214                 }
215 #  ifdef _AIX
216         }
217 #  else
218 #   ifdef sun
219         }
220 #   endif
221 #  endif
223         /* If we get anything on STDERR, at least set warning */
224         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
225                 result = max_state (result, STATE_WARNING);
227         /* close stderr */
228         (void) fclose (child_stderr);
230         /* close the pipe */
231         if (spclose (child_process))
232                 result = max_state (result, STATE_WARNING);
233 # endif /* HAVE_SWAP */
234 #endif /* HAVE_PROC_MEMINFO */
236         percent_used = 100 * ((double) used_swap) / ((double) total_swap);
237         result = max_state (result, check_swap (percent_used, free_swap));
238         asprintf (&status, _(" %d%% free (%llu MB out of %llu MB)%s"),
239                                                 (100 - percent_used), free_swap, total_swap, status);
241         asprintf (&perf, "%s", perfdata ("swap", (long) free_swap, "MB",
242                 TRUE, (long) max (warn_size/1024, warn_percent/100.0*total_swap),
243                 TRUE, (long) max (crit_size/1024, crit_percent/100.0*total_swap),
244                 TRUE, 0,
245                 TRUE, (long) total_swap));
246         printf ("SWAP %s:%s |%s\n", state_text (result), status, perf);
247         return result;
252 \f
253 int
254 check_swap (int usp, long unsigned int free_swap)
256         int result = STATE_UNKNOWN;
257         free_swap = free_swap * 1024;           /* Convert back to bytes as warn and crit specified in bytes */
258         if (usp >= 0 && crit_percent != 0 && usp >= (100.0 - crit_percent))
259                 result = STATE_CRITICAL;
260         else if (crit_size > 0 && free_swap <= crit_size)
261                 result = STATE_CRITICAL;
262         else if (usp >= 0 && warn_percent != 0 && usp >= (100.0 - warn_percent))
263                 result = STATE_WARNING;
264         else if (warn_size > 0 && free_swap <= warn_size)
265                 result = STATE_WARNING;
266         else if (usp >= 0.0)
267                 result = STATE_OK;
268         return result;
272 /* process command-line arguments */
273 int
274 process_arguments (int argc, char **argv)
276         int c = 0;  /* option character */
278         int option = 0;
279         static struct option longopts[] = {
280                 {"warning", required_argument, 0, 'w'},
281                 {"critical", required_argument, 0, 'c'},
282                 {"allswaps", no_argument, 0, 'a'},
283                 {"verbose", no_argument, 0, 'v'},
284                 {"version", no_argument, 0, 'V'},
285                 {"help", no_argument, 0, 'h'},
286                 {0, 0, 0, 0}
287         };
289         if (argc < 2)
290                 return ERROR;
292         while (1) {
293                 c = getopt_long (argc, argv, "+?Vvhac:w:", longopts, &option);
295                 if (c == -1 || c == EOF)
296                         break;
298                 switch (c) {
299                 case 'w':                                                                       /* warning size threshold */
300                         if (is_intnonneg (optarg)) {
301                                 warn_size = atoi (optarg);
302                                 break;
303                         }
304                         else if (strstr (optarg, ",") &&
305                                                          strstr (optarg, "%") &&
306                                                          sscanf (optarg, "%llu,%d%%", &warn_size, &warn_percent) == 2) {
307                                 break;
308                         }
309                         else if (strstr (optarg, "%") &&
310                                                          sscanf (optarg, "%d%%", &warn_percent) == 1) {
311                                 break;
312                         }
313                         else {
314                                 usage (_("Warning threshold must be integer or percentage!\n"));
315                         }
316                 case 'c':                                                                       /* critical size threshold */
317                         if (is_intnonneg (optarg)) {
318                                 crit_size = atoi (optarg);
319                                 break;
320                         }
321                         else if (strstr (optarg, ",") &&
322                                                          strstr (optarg, "%") &&
323                                                          sscanf (optarg, "%llu,%d%%", &crit_size, &crit_percent) == 2) {
324                                 break;
325                         }
326                         else if (strstr (optarg, "%") &&
327                                                          sscanf (optarg, "%d%%", &crit_percent) == 1) {
328                                 break;
329                         }
330                         else {
331                                 usage (_("Critical threshold must be integer or percentage!\n"));
332                         }
333                 case 'a':                                                                       /* all swap */
334                         allswaps = TRUE;
335                         break;
336                 case 'v':                                                                       /* verbose */
337                         verbose++;
338                         break;
339                 case 'V':                                                                       /* version */
340                         print_revision (progname, revision);
341                         exit (STATE_OK);
342                 case 'h':                                                                       /* help */
343                         print_help ();
344                         exit (STATE_OK);
345                 case '?':                                                                       /* error */
346                         usage (_("Invalid argument\n"));
347                 }
348         }
350         c = optind;
351         if (c == argc)
352                 return validate_arguments ();
353         if (warn_percent == 0 && is_intnonneg (argv[c]))
354                 warn_percent = atoi (argv[c++]);
356         if (c == argc)
357                 return validate_arguments ();
358         if (crit_percent == 0 && is_intnonneg (argv[c]))
359                 crit_percent = atoi (argv[c++]);
361         if (c == argc)
362                 return validate_arguments ();
363         if (warn_size == 0 && is_intnonneg (argv[c]))
364                 warn_size = atoi (argv[c++]);
366         if (c == argc)
367                 return validate_arguments ();
368         if (crit_size == 0 && is_intnonneg (argv[c]))
369                 crit_size = atoi (argv[c++]);
371         return validate_arguments ();
378 int
379 validate_arguments (void)
381         if (warn_percent == 0 && crit_percent == 0 && warn_size == 0
382                         && crit_size == 0) {
383                 return ERROR;
384         }
385         else if (warn_percent < crit_percent) {
386                 usage
387                         (_("Warning percentage should be more than critical percentage\n"));
388         }
389         else if (warn_size < crit_size) {
390                 usage
391                         (_("Warning free space should be more than critical free space\n"));
392         }
393         return OK;
400 \f
401 void
402 print_help (void)
404         print_revision (progname, revision);
406         printf (_(COPYRIGHT), copyright, email);
408         printf (_("Check swap space on local server.\n\n"));
410         print_usage ();
412         printf (_(UT_HELP_VRSN));
414         printf (_("\n\
415  -w, --warning=INTEGER\n\
416    Exit with WARNING status if less than INTEGER bytes of swap space are free\n\
417  -w, --warning=PERCENT%%\n\
418    Exit with WARNING status if less than PERCENT of swap space is free\n\
419  -c, --critical=INTEGER\n\
420    Exit with CRITICAL status if less than INTEGER bytes of swap space are free\n\
421  -c, --critical=PERCENT%%\n\
422    Exit with CRITCAL status if less than PERCENT of swap space is free\n\
423  -a, --allswaps\n\
424     Conduct comparisons for all swap partitions, one by one\n\
425  -v, --verbose\n\
426     Verbose output. Up to 3 levels\n"));
428         printf (_("\n\
429 On Solaris, if -a specified, uses swap -l, otherwise uses swap -s.\n\
430 Will be discrepencies because swap -s counts allocated swap and includes\n\
431 real memory\n"));
432         printf (_("\n\
433 On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s.\n"));
435         printf (_(UT_SUPPORT));
441 void
442 print_usage (void)
444         printf (_("Usage:\n\
445  %s [-av] -w <percent_free>%% -c <percent_free>%%\n\
446  %s [-av] -w <bytes_free> -c <bytes_free>\n\
447  %s (-h | --help) for detailed help\n\
448  %s (-V | --version) for version information\n"),
449                 progname, progname, progname, progname);