Code

last changes to configure broke check_swap on RHLinux. Restore /proc/meminfo check...
[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 = 200;
44 int crit_percent = 200;
45 long unsigned int warn_size = 0;
46 long unsigned int crit_size = 0;
47 int verbose;
48 int allswaps;
50 #if !defined(sun)
51 int sun = 0;    /* defined by compiler if it is a sun solaris system */
52 #endif
54 int
55 main (int argc, char **argv)
56 {
57         int percent_used, percent;
58         long unsigned int total_swap = 0, used_swap = 0, free_swap = 0;
59         long unsigned int dsktotal, dskused, dskfree;
60         int result = STATE_OK;
61         char input_buffer[MAX_INPUT_BUFFER];
62 #ifdef HAVE_PROC_MEMINFO
63         FILE *fp;
64 #else
65 # ifdef HAVE_SWAP
66         int conv_factor;                /* Convert to MBs */
67         char *temp_buffer;
68         char *swap_command;
69         char *swap_format;
70 # endif
71 #endif
72         char str[32];
73         char *status;
75         setlocale (LC_ALL, "");
76         bindtextdomain (PACKAGE, LOCALEDIR);
77         textdomain (PACKAGE);
79         status = strdup("");
81         if (process_arguments (argc, argv) != OK)
82                 usage (_("Invalid command arguments supplied\n"));
84 #ifdef HAVE_PROC_MEMINFO
85         fp = fopen (PROC_MEMINFO, "r");
86         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
87                 if (sscanf (input_buffer, " %s %lu %lu %lu", str, &dsktotal, &dskused, &dskfree) == 4 &&
88                     strstr (str, "Swap")) {
89                         dsktotal = dsktotal / 1048576;
90                         dskused = dskused / 1048576;
91                         dskfree = dskfree / 1048576;
92                         total_swap += dsktotal;
93                         used_swap += dskused;
94                         free_swap += dskfree;
95                         if (allswaps) {
96                                 percent = 100 * (((double) dskused) / ((double) dsktotal));
97                                 result = max_state (result, check_swap (percent, dskfree));
98                                 if (verbose)
99                                         asprintf (&status, "%s [%lu (%d%%)]", status, dskfree, 100 - percent);
100                         }
101                 }
102         }
103         fclose(fp);
104 #else
105 # ifdef HAVE_SWAP
106         if (!allswaps && sun) {
107                 asprintf(&swap_command, "%s", "/usr/sbin/swap -s");
108                 asprintf(&swap_format, "%s", "%*s %*dk %*s %*s + %*dk %*s = %dk %*s %dk %*s");
109                 conv_factor = 2048;
110         } else {
111                 asprintf(&swap_command, "%s", SWAP_COMMAND);
112                 asprintf(&swap_format, "%s", SWAP_FORMAT);
113                 conv_factor = SWAP_CONVERSION;
114         }
116         if (verbose >= 2)
117                 printf (_("Command: %s\n"), swap_command);
118         if (verbose >= 3)
119                 printf (_("Format: %s\n"), swap_format);
121         child_process = spopen (swap_command);
122         if (child_process == NULL) {
123                 printf (_("Could not open pipe: %s\n"), swap_command);
124                 return STATE_UNKNOWN;
125         }
127         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
128         if (child_stderr == NULL)
129                 printf (_("Could not open stderr for %s\n"), swap_command);
131         sprintf (str, "%s", "");
132         /* read 1st line */
133         fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
134         if (strcmp (swap_format, "") == 0) {
135                 temp_buffer = strtok (input_buffer, " \n");
136                 while (temp_buffer) {
137                         if (strstr (temp_buffer, "blocks"))
138                                 sprintf (str, "%s %s", str, "%f");
139                         else if (strstr (temp_buffer, "dskfree"))
140                                 sprintf (str, "%s %s", str, "%f");
141                         else
142                                 sprintf (str, "%s %s", str, "%*s");
143                         temp_buffer = strtok (NULL, " \n");
144                 }
145         }
147         if (!allswaps && sun) {
148                 sscanf (input_buffer, swap_format, &used_swap, &free_swap);
149                 used_swap = used_swap / 1024;
150                 free_swap = free_swap / 1024;
151                 total_swap = used_swap + free_swap;
152         } else {
153                 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
154                         sscanf (input_buffer, swap_format, &dsktotal, &dskfree);
156                         dsktotal = dsktotal / conv_factor;
157                         dskfree = dskfree / conv_factor;
158                         if (verbose >= 3)
159                                 printf (_("total=%d, free=%d\n"), dsktotal, dskfree);
161                         dskused = dsktotal - dskfree;
162                         total_swap += dsktotal;
163                         used_swap += dskused;
164                         free_swap += dskfree;
165                         if (allswaps) {
166                                 percent = 100 * (((double) dskused) / ((double) dsktotal));
167                                 result = max_state (result, check_swap (percent, dskfree));
168                                 if (verbose)
169                                         asprintf (&status, "%s [%lu (%d%%)]", status, dskfree, 100 - percent);
170                         }
171                 }
172         }
173         /* If we get anything on STDERR, at least set warning */
174         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
175                 result = max_state (result, STATE_WARNING);
177         /* close stderr */
178         (void) fclose (child_stderr);
180         /* close the pipe */
181         if (spclose (child_process))
182                 result = max_state (result, STATE_WARNING);
183 # endif
184 #endif
186         percent_used = 100 * ((double) used_swap) / ((double) total_swap);
187         result = max_state (result, check_swap (percent_used, free_swap));
188         asprintf (&status, _(" %d%% free (%lu MB out of %lu MB)%s"),
189                                                 (100 - percent_used), free_swap, total_swap, status);
191         die (result, "SWAP %s:%s\n", state_text (result), status);
192         return STATE_UNKNOWN;
197 \f
198 int
199 check_swap (int usp, long unsigned int free_swap)
201         int result = STATE_UNKNOWN;
202         if (usp >= 0 && usp >= (100.0 - crit_percent))
203                 result = STATE_CRITICAL;
204         else if (crit_size > 0 && free_swap <= crit_size)
205                 result = STATE_CRITICAL;
206         else if (usp >= 0 && usp >= (100.0 - warn_percent))
207                 result = STATE_WARNING;
208         else if (warn_size > 0 && free_swap <= warn_size)
209                 result = STATE_WARNING;
210         else if (usp >= 0.0)
211                 result = STATE_OK;
212         return result;
216 /* process command-line arguments */
217 int
218 process_arguments (int argc, char **argv)
220         int c = 0;  /* option character */
222         int option = 0;
223         static struct option longopts[] = {
224                 {"warning", required_argument, 0, 'w'},
225                 {"critical", required_argument, 0, 'c'},
226                 {"allswaps", no_argument, 0, 'a'},
227                 {"verbose", no_argument, 0, 'v'},
228                 {"version", no_argument, 0, 'V'},
229                 {"help", no_argument, 0, 'h'},
230                 {0, 0, 0, 0}
231         };
233         if (argc < 2)
234                 return ERROR;
236         while (1) {
237                 c = getopt_long (argc, argv, "+?Vvhac:w:", longopts, &option);
239                 if (c == -1 || c == EOF)
240                         break;
242                 switch (c) {
243                 case 'w':                                                                       /* warning size threshold */
244                         if (is_intnonneg (optarg)) {
245                                 warn_size = atoi (optarg);
246                                 break;
247                         }
248                         else if (strstr (optarg, ",") &&
249                                                          strstr (optarg, "%") &&
250                                                          sscanf (optarg, "%lu,%d%%", &warn_size, &warn_percent) == 2) {
251                                 break;
252                         }
253                         else if (strstr (optarg, "%") &&
254                                                          sscanf (optarg, "%d%%", &warn_percent) == 1) {
255                                 break;
256                         }
257                         else {
258                                 usage (_("Warning threshold must be integer or percentage!\n"));
259                         }
260                 case 'c':                                                                       /* critical size threshold */
261                         if (is_intnonneg (optarg)) {
262                                 crit_size = atoi (optarg);
263                                 break;
264                         }
265                         else if (strstr (optarg, ",") &&
266                                                          strstr (optarg, "%") &&
267                                                          sscanf (optarg, "%lu,%d%%", &crit_size, &crit_percent) == 2) {
268                                 break;
269                         }
270                         else if (strstr (optarg, "%") &&
271                                                          sscanf (optarg, "%d%%", &crit_percent) == 1) {
272                                 break;
273                         }
274                         else {
275                                 usage (_("Critical threshold must be integer or percentage!\n"));
276                         }
277                 case 'a':                                                                       /* all swap */
278                         allswaps = TRUE;
279                         break;
280                 case 'v':                                                                       /* verbose */
281                         verbose++;
282                         break;
283                 case 'V':                                                                       /* version */
284                         print_revision (progname, revision);
285                         exit (STATE_OK);
286                 case 'h':                                                                       /* help */
287                         print_help ();
288                         exit (STATE_OK);
289                 case '?':                                                                       /* help */
290                         usage (_("Invalid argument\n"));
291                 }
292         }
294         c = optind;
295         if (c == argc)
296                 return validate_arguments ();
297         if (warn_percent > 100 && is_intnonneg (argv[c]))
298                 warn_percent = atoi (argv[c++]);
300         if (c == argc)
301                 return validate_arguments ();
302         if (crit_percent > 100 && is_intnonneg (argv[c]))
303                 crit_percent = atoi (argv[c++]);
305         if (c == argc)
306                 return validate_arguments ();
307         if (warn_size == 0 && is_intnonneg (argv[c]))
308                 warn_size = atoi (argv[c++]);
310         if (c == argc)
311                 return validate_arguments ();
312         if (crit_size == 0 && is_intnonneg (argv[c]))
313                 crit_size = atoi (argv[c++]);
315         return validate_arguments ();
322 int
323 validate_arguments (void)
325         if (warn_percent > 100 && crit_percent > 100 && warn_size == 0
326                         && crit_size == 0) {
327                 return ERROR;
328         }
329         else if (warn_percent < crit_percent) {
330                 usage
331                         (_("Warning percentage should be more than critical percentage\n"));
332         }
333         else if (warn_size < crit_size) {
334                 usage
335                         (_("Warning free space should be more than critical free space\n"));
336         }
337         return OK;
344 \f
345 void
346 print_help (void)
348         print_revision (progname, revision);
350         printf (_(COPYRIGHT), copyright, email);
352         printf (_("Check swap space on local server.\n\n"));
354         print_usage ();
356         printf (_(UT_HELP_VRSN));
358         printf (_("\n\
359  -w, --warning=INTEGER\n\
360    Exit with WARNING status if less than INTEGER bytes of swap space are free\n\
361  -w, --warning=PERCENT%%\n\
362    Exit with WARNING status if less than PERCENT of swap space has been used\n\
363  -c, --critical=INTEGER\n\
364    Exit with CRITICAL status if less than INTEGER bytes of swap space are free\n\
365  -c, --critical=PERCENT%%\n\
366    Exit with CRITCAL status if less than PERCENT of swap space has been used\n\
367  -a, --allswaps\n\
368     Conduct comparisons for all swap partitions, one by one\n"));
370 #ifdef sun
371         printf (_("\n\
372 On Solaris, if -a specified, uses swap -l, otherwise uses swap -s.\n\
373 Will be discrepencies because swap -s counts allocated swap and includes\n\
374 real memory\n"));
375 #endif
377         printf (_(UT_SUPPORT));
383 void
384 print_usage (void)
386         printf (_("Usage:\n\
387  %s [-a] -w <used_percentage>%% -c <used_percentage>%%\n\
388  %s [-a] -w <bytes_free> -c <bytes_free>\n\
389  %s (-h | --help) for detailed help\n\
390  %s (-V | --version) for version information\n"),
391                 progname, progname, progname, progname);