Code

Support for swap -s for solaris. Also changes size of swap to MBs through
[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 #define REVISION "$Revision$"
34 #define COPYRIGHT "2000-2002"
35 #define AUTHOR "Karl DeBisschop"
36 #define EMAIL "kdebisschop@users.sourceforge.net"
37 #define SUMMARY "Check swap space on local server.\n"
39 int process_arguments (int argc, char **argv);
40 int validate_arguments (void);
41 void print_usage (void);
42 void print_help (void);
44 int warn_percent = 200;
45 int crit_percent = 200;
46 long unsigned int warn_size = 0;
47 long unsigned int crit_size = 0;
48 int verbose;
49 int allswaps;
51 #if !defined(sun)
52 int sun = 0;    /* defined by compiler if it is a sun solaris system */
53 #endif
55 int
56 main (int argc, char **argv)
57 {
58         int percent_used, percent;
59         long unsigned int total_swap = 0, used_swap = 0, free_swap = 0;
60         long unsigned int total, used, free;
61         int conv_factor;                /* Convert to MBs */
62         int result = STATE_OK;
63         char input_buffer[MAX_INPUT_BUFFER];
64 #ifdef HAVE_SWAP
65         char *temp_buffer;
66         char *swap_command;
67         char *swap_format;
68 #endif
69 #ifdef HAVE_PROC_MEMINFO
70         FILE *fp;
71 #endif
72         char str[32];
73         char *status = "";
75         if (process_arguments (argc, argv) != OK)
76                 usage ("Invalid command arguments supplied\n");
78 #ifdef HAVE_PROC_MEMINFO
79         fp = fopen (PROC_MEMINFO, "r");
80         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
81                 if (sscanf (input_buffer, " %s %lu %lu %lu", str, &total, &used, &free) == 4 &&
82                     strstr (str, "Swap")) {
83 #endif
84 #ifdef HAVE_SWAP
85         if (!allswaps && sun) {
86                 asprintf(&swap_command, "%s", "/usr/sbin/swap -s");
87                 asprintf(&swap_format, "%s", "%*s %*dk %*s %*s + %*dk %*s = %dk %*s %dk %*s");
88                 conv_factor = 2048;
89         } else {
90                 asprintf(&swap_command, "%s", SWAP_COMMAND);
91                 asprintf(&swap_format, "%s", SWAP_FORMAT);
92                 conv_factor = SWAP_CONVERSION;
93         }
95         if (verbose >= 2)
96                 printf ("Command: %s\n", swap_command);
97         if (verbose >= 3)
98                 printf ("Format: %s\n", swap_format);
100         child_process = spopen (swap_command);
101         if (child_process == NULL) {
102                 printf ("Could not open pipe: %s\n", swap_command);
103                 return STATE_UNKNOWN;
104         }
106         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
107         if (child_stderr == NULL)
108                 printf ("Could not open stderr for %s\n", swap_command);
110         sprintf (str, "%s", "");
111         /* read 1st line */
112         fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
113         if (strcmp (swap_format, "") == 0) {
114                 temp_buffer = strtok (input_buffer, " \n");
115                 while (temp_buffer) {
116                         if (strstr (temp_buffer, "blocks"))
117                                 sprintf (str, "%s %s", str, "%f");
118                         else if (strstr (temp_buffer, "free"))
119                                 sprintf (str, "%s %s", str, "%f");
120                         else
121                                 sprintf (str, "%s %s", str, "%*s");
122                         temp_buffer = strtok (NULL, " \n");
123                 }
124         }
126         if (!allswaps && sun) {
127                 sscanf (input_buffer, swap_format, &used_swap, &free_swap);
128                 used_swap = used_swap / 1024;
129                 free_swap = free_swap / 1024;
130                 total_swap = used_swap + free_swap;
131         } else {
132                 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
133                         sscanf (input_buffer, swap_format, &total, &free);
135                         total = total / conv_factor;
136                         free = free / conv_factor;
137                         if (verbose >= 3)
138                                 printf ("total=%d, free=%d\n", total, free);
140                         used = total - free;
141 #endif
142                         total_swap += total;
143                         used_swap += used;
144                         free_swap += free;
145                         if (allswaps) {
146                                 percent = 100 * (((double) used) / ((double) total));
147                                 result = max_state (result, check_swap (percent, free));
148                                 if (verbose)
149                                         asprintf (&status, "%s [%lu (%d%%)]", status, free, 100 - percent);
150                         }
151                 }
152         }
153         percent_used = 100 * ((double) used_swap) / ((double) total_swap);
154         result = max_state (result, check_swap (percent_used, free_swap));
155         asprintf (&status, " %d%% free (%lu MB out of %lu MB)%s",
156                                                 (100 - percent_used), free_swap, total_swap, status);
158 #ifdef HAVE_PROC_MEMINFO
159         fclose(fp);
160 #endif
161 #ifdef HAVE_SWAP
162         /* If we get anything on STDERR, at least set warning */
163         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
164                 result = max_state (result, STATE_WARNING);
166         /* close stderr */
167         (void) fclose (child_stderr);
169         /* close the pipe */
170         if (spclose (child_process))
171                 result = max_state (result, STATE_WARNING);
172 #endif
174         terminate (result, "SWAP %s:%s\n", state_text (result), status);
179 \f
180 int
181 check_swap (int usp, int free_swap)
183         int result = STATE_UNKNOWN;
184         if (usp >= 0 && usp >= (100.0 - crit_percent))
185                 result = STATE_CRITICAL;
186         else if (crit_size >= 0 && free_swap <= crit_size)
187                 result = STATE_CRITICAL;
188         else if (usp >= 0 && usp >= (100.0 - warn_percent))
189                 result = STATE_WARNING;
190         else if (warn_size >= 0 && free_swap <= warn_size)
191                 result = STATE_WARNING;
192         else if (usp >= 0.0)
193                 result = STATE_OK;
194         return result;
198 /* process command-line arguments */
199 int
200 process_arguments (int argc, char **argv)
202         int c = 0;  /* option character */
203         int wc = 0; /* warning counter  */
204         int cc = 0; /* critical counter */
206         int option_index = 0;
207         static struct option long_options[] = {
208                 {"warning", required_argument, 0, 'w'},
209                 {"critical", required_argument, 0, 'c'},
210                 {"allswaps", no_argument, 0, 'a'},
211                 {"verbose", no_argument, 0, 'v'},
212                 {"version", no_argument, 0, 'V'},
213                 {"help", no_argument, 0, 'h'},
214                 {0, 0, 0, 0}
215         };
217         if (argc < 2)
218                 return ERROR;
220         while (1) {
221                 c = getopt_long (argc, argv, "+?Vvhac:w:", long_options, &option_index);
223                 if (c == -1 || c == EOF)
224                         break;
226                 switch (c) {
227                 case 'w':                                                                       /* warning time threshold */
228                         if (is_intnonneg (optarg)) {
229                                 warn_size = atoi (optarg);
230                                 break;
231                         }
232                         else if (strstr (optarg, ",") &&
233                                                          strstr (optarg, "%") &&
234                                                          sscanf (optarg, "%lu,%d%%", &warn_size, &warn_percent) == 2) {
235                                 break;
236                         }
237                         else if (strstr (optarg, "%") &&
238                                                          sscanf (optarg, "%d%%", &warn_percent) == 1) {
239                                 break;
240                         }
241                         else {
242                                 usage ("Warning threshold must be integer or percentage!\n");
243                         }
244                         wc++;
245                 case 'c':                                                                       /* critical time threshold */
246                         if (is_intnonneg (optarg)) {
247                                 crit_size = atoi (optarg);
248                                 break;
249                         }
250                         else if (strstr (optarg, ",") &&
251                                                          strstr (optarg, "%") &&
252                                                          sscanf (optarg, "%lu,%d%%", &crit_size, &crit_percent) == 2) {
253                                 break;
254                         }
255                         else if (strstr (optarg, "%") &&
256                                                          sscanf (optarg, "%d%%", &crit_percent) == 1) {
257                                 break;
258                         }
259                         else {
260                                 usage ("Critical threshold must be integer or percentage!\n");
261                         }
262                         cc++;
263                 case 'a':                                                                       /* all swap */
264                         allswaps = TRUE;
265                         break;
266                 case 'v':                                                                       /* verbose */
267                         verbose++;
268                         break;
269                 case 'V':                                                                       /* version */
270                         print_revision (progname, "$Revision$");
271                         exit (STATE_OK);
272                 case 'h':                                                                       /* help */
273                         print_help ();
274                         exit (STATE_OK);
275                 case '?':                                                                       /* help */
276                         usage ("Invalid argument\n");
277                 }
278         }
280         c = optind;
281         if (c == argc)
282                 return validate_arguments ();
283         if (warn_percent > 100 && is_intnonneg (argv[c]))
284                 warn_percent = atoi (argv[c++]);
286         if (c == argc)
287                 return validate_arguments ();
288         if (crit_percent > 100 && is_intnonneg (argv[c]))
289                 crit_percent = atoi (argv[c++]);
291         if (c == argc)
292                 return validate_arguments ();
293         if (warn_size < 0 && is_intnonneg (argv[c]))
294                 warn_size = atoi (argv[c++]);
296         if (c == argc)
297                 return validate_arguments ();
298         if (crit_size < 0 && is_intnonneg (argv[c]))
299                 crit_size = atoi (argv[c++]);
301         return validate_arguments ();
308 int
309 validate_arguments (void)
311         if (warn_percent > 100 && crit_percent > 100 && warn_size < 0
312                         && crit_size < 0) {
313                 return ERROR;
314         }
315         else if (warn_percent < crit_percent) {
316                 usage
317                         ("Warning percentage should be more than critical percentage\n");
318         }
319         else if (warn_size < crit_size) {
320                 usage
321                         ("Warning free space should be more than critical free space\n");
322         }
323         return OK;
330 void
331 print_usage (void)
333         printf
334                 ("Usage:\n"
335                  " %s [-a] -w <used_percentage>%% -c <used_percentage>%%\n"
336                  " %s [-a] -w <bytes_free> -c <bytes_free>\n"
337                  " %s (-h | --help) for detailed help\n"
338                  " %s (-V | --version) for version information\n",
339                  progname, progname, progname, progname);
346 void
347 print_help (void)
349         print_revision (progname, REVISION);
350         printf
351                 ("Copyright (c) %s %s <%s>\n\n%s\n", COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
352         print_usage ();
353         printf
354                 ("\nOptions:\n"
355                  " -w, --warning=INTEGER\n"
356                  "   Exit with WARNING status if less than INTEGER bytes of swap space are free\n"
357                  " -w, --warning=PERCENT%%\n"
358                  "   Exit with WARNING status if less than PERCENT of swap space has been used\n"
359                  " -c, --critical=INTEGER\n"
360                  "   Exit with CRITICAL status if less than INTEGER bytes of swap space are free\n"
361                  " -c, --critical=PERCENT%%\n"
362                  "   Exit with CRITCAL status if less than PERCENT of swap space has been used\n"
363                  " -a, --allswaps\n"
364                  "    Conduct comparisons for all swap partitions, one by one\n"
365                  " -h, --help\n"
366                  "    Print detailed help screen\n"
367                  " -V, --version\n" "    Print version information\n"
368 #ifdef sun
369                  "\nOn Solaris, if -a specified, uses swap -l, otherwise uses swap -s.\n"
370                  "Will be discrepencies because swap -s counts allocated swap and includes real memory\n"
371 #endif
372                  "\n"
373                  );
374         support ();