Code

ad5eb25ff972912a00d827de3267183b8b9c7dd5
[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 int
52 main (int argc, char **argv)
53 {
54         int percent_used, percent;
55         long unsigned int total_swap = 0, used_swap = 0, free_swap = 0;
56         long unsigned int total, used, free;
57         int result = STATE_OK;
58         char input_buffer[MAX_INPUT_BUFFER];
59 #ifdef HAVE_SWAP
60         char *temp_buffer;
61 #endif
62 #ifdef HAVE_PROC_MEMINFO
63         FILE *fp;
64 #endif
65         char str[32];
66         char *status = "";
68         if (process_arguments (argc, argv) != OK)
69                 usage ("Invalid command arguments supplied\n");
71 #ifdef HAVE_PROC_MEMINFO
72         fp = fopen (PROC_MEMINFO, "r");
73         asprintf (&status, "%s", "Swap used:");
74         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
75                 if (sscanf (input_buffer, " %s %lu %lu %lu", str, &total, &used, &free) == 4 &&
76                     strstr (str, "Swap")) {
77                         total_swap += total;
78                         used_swap += used;
79                         free_swap += free;
80                         if (allswaps) {
81                                 percent = 100 * (((double) used) / ((double) total));
82                                 if (percent >= crit_percent || free <= crit_size)
83                                         result = max_state (STATE_CRITICAL, result);
84                                 else if (percent >= warn_percent || free <= warn_size)
85                                         result = max_state (STATE_WARNING, result);
86                                 if (verbose)
87                                         asprintf (&status, "%s [%lu/%lu]", status, used, total);
88                         }
89                 }
90         }
91         percent_used = 100 * (((double) used_swap) / ((double) total_swap));
92         if (percent_used >= crit_percent || free_swap <= crit_size)
93                 result = max_state (STATE_CRITICAL, result);
94         else if (percent_used >= warn_percent || free_swap <= warn_size)
95                 result = max_state (STATE_WARNING, result);
96         asprintf (&status, "%s %2d%% (%lu out of %lu)", status, percent_used,
97                   used_swap, total_swap);
98         fclose (fp);
99 #else
100 #ifdef HAVE_SWAP
101         child_process = spopen (SWAP_COMMAND);
102         if (child_process == NULL) {
103                 printf ("Could not open pipe: %s\n", SWAP_COMMAND);
104                 return STATE_UNKNOWN;
105         }
107         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
108         if (child_stderr == NULL)
109                 printf ("Could not open stderr for %s\n", SWAP_COMMAND);
111         sprintf (str, "%s", "");
112         /* read 1st line */
113         fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
114         if (strcmp (SWAP_FORMAT, "") == 0) {
115                 temp_buffer = strtok (input_buffer, " \n");
116                 while (temp_buffer) {
117                         if (strstr (temp_buffer, "blocks"))
118                                 sprintf (str, "%s %s", str, "%f");
119                         else if (strstr (temp_buffer, "free"))
120                                 sprintf (str, "%s %s", str, "%f");
121                         else
122                                 sprintf (str, "%s %s", str, "%*s");
123                         temp_buffer = strtok (NULL, " \n");
124                 }
125         }
127         asprintf (&status, "%s", "Swap used:");
128         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
129                 sscanf (input_buffer, SWAP_FORMAT, &total, &free);
130                 used = total - free;
131                 total_swap += total;
132                 used_swap += used;
133                 free_swap += free;
134                 if (allswaps) {
135                         percent = 100 * (((double) used) / ((double) total));
136                         if (percent >= crit_percent || free <= crit_size)
137                                 result = max_state (STATE_CRITICAL, result);
138                         else if (percent >= warn_percent || free <= warn_size)
139                                 result = max_state (STATE_WARNING, result);
140                         if (verbose)
141                                 asprintf (&status, "%s [%lu/%lu]", status, used, total);
142                 }
143         }
144         percent_used = 100 * ((double) used_swap) / ((double) total_swap);
145         if (percent_used >= crit_percent || free_swap <= crit_size)
146                 result = max_state (STATE_CRITICAL, result);
147         else if (percent_used >= warn_percent || free_swap <= warn_size)
148                 result = max_state (STATE_WARNING, result);
149         asprintf (&status, "%s %2d%% (%lu out of %lu)",
150                                                 status, percent_used, used_swap, total_swap);
152         /* If we get anything on STDERR, at least set warning */
153         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
154                 result = max_state (result, STATE_WARNING);
156         /* close stderr */
157         (void) fclose (child_stderr);
159         /* close the pipe */
160         if (spclose (child_process))
161                 result = max_state (result, STATE_WARNING);
162 #endif
163 #endif
165 #ifndef SWAP_COMMAND
166 #ifndef SWAP_FILE
167 #ifndef HAVE_PROC_MEMINFO
168         return STATE_UNKNOWN;
169 #endif
170 #endif
171 #endif
173         if (result == STATE_OK)
174                 printf ("Swap ok - %s\n", status);
175         else if (result == STATE_CRITICAL)
176                 printf ("CRITICAL - %s\n", status);
177         else if (result == STATE_WARNING)
178                 printf ("WARNING - %s\n", status);
179         else if (result == STATE_UNKNOWN)
180                 printf ("Unable to read output\n");
181         else {
182                 result = STATE_UNKNOWN;
183                 printf ("UNKNOWN - %s\n", status);
184         }
186         return result;
193 /* process command-line arguments */
194 int
195 process_arguments (int argc, char **argv)
197         int c = 0;  /* option character */
198         int wc = 0; /* warning counter  */
199         int cc = 0; /* critical counter */
201         int option_index = 0;
202         static struct option long_options[] = {
203                 {"warning", required_argument, 0, 'w'},
204                 {"critical", required_argument, 0, 'c'},
205                 {"allswaps", no_argument, 0, 'a'},
206                 {"verbose", no_argument, 0, 'v'},
207                 {"version", no_argument, 0, 'V'},
208                 {"help", no_argument, 0, 'h'},
209                 {0, 0, 0, 0}
210         };
212         if (argc < 2)
213                 return ERROR;
215         while (1) {
216                 c = getopt_long (argc, argv, "+?Vvhac:w:", long_options, &option_index);
218                 if (c == -1 || c == EOF)
219                         break;
221                 switch (c) {
222                 case 'w':                                                                       /* warning time threshold */
223                         if (is_intnonneg (optarg)) {
224                                 warn_size = atoi (optarg);
225                                 break;
226                         }
227                         else if (strstr (optarg, ",") &&
228                                                          strstr (optarg, "%") &&
229                                                          sscanf (optarg, "%lu,%d%%", &warn_size, &warn_percent) == 2) {
230                                 break;
231                         }
232                         else if (strstr (optarg, "%") &&
233                                                          sscanf (optarg, "%d%%", &warn_percent) == 1) {
234                                 break;
235                         }
236                         else {
237                                 usage ("Warning threshold must be integer or percentage!\n");
238                         }
239                         wc++;
240                 case 'c':                                                                       /* critical time threshold */
241                         if (is_intnonneg (optarg)) {
242                                 crit_size = atoi (optarg);
243                                 break;
244                         }
245                         else if (strstr (optarg, ",") &&
246                                                          strstr (optarg, "%") &&
247                                                          sscanf (optarg, "%lu,%d%%", &crit_size, &crit_percent) == 2) {
248                                 break;
249                         }
250                         else if (strstr (optarg, "%") &&
251                                                          sscanf (optarg, "%d%%", &crit_percent) == 1) {
252                                 break;
253                         }
254                         else {
255                                 usage ("Critical threshold must be integer or percentage!\n");
256                         }
257                         cc++;
258                 case 'a':                                                                       /* verbose */
259                         allswaps = TRUE;
260                         break;
261                 case 'v':                                                                       /* verbose */
262                         verbose = TRUE;
263                         break;
264                 case 'V':                                                                       /* version */
265                         print_revision (progname, "$Revision$");
266                         exit (STATE_OK);
267                 case 'h':                                                                       /* help */
268                         print_help ();
269                         exit (STATE_OK);
270                 case '?':                                                                       /* help */
271                         usage ("Invalid argument\n");
272                 }
273         }
275         c = optind;
276         if (c == argc)
277                 return validate_arguments ();
278         if (warn_percent > 100 && is_intnonneg (argv[c]))
279                 warn_percent = atoi (argv[c++]);
281         if (c == argc)
282                 return validate_arguments ();
283         if (crit_percent > 100 && is_intnonneg (argv[c]))
284                 crit_percent = atoi (argv[c++]);
286         if (c == argc)
287                 return validate_arguments ();
288         if (warn_size < 0 && is_intnonneg (argv[c]))
289                 warn_size = atoi (argv[c++]);
291         if (c == argc)
292                 return validate_arguments ();
293         if (crit_size < 0 && is_intnonneg (argv[c]))
294                 crit_size = atoi (argv[c++]);
296         return validate_arguments ();
303 int
304 validate_arguments (void)
306         if (warn_percent > 100 && crit_percent > 100 && warn_size < 0
307                         && crit_size < 0) {
308                 return ERROR;
309         }
310         else if (warn_percent > crit_percent) {
311                 usage
312                         ("Warning percentage should not be less than critical percentage\n");
313         }
314         else if (warn_size < crit_size) {
315                 usage
316                         ("Warning free space should not be more than critical free space\n");
317         }
318         return OK;
325 void
326 print_usage (void)
328         printf
329                 ("Usage:\n"
330                  " %s [-a] -w <used_percentage>%% -c <used_percentage>%%\n"
331                  " %s [-a] -w <bytes_free> -c <bytes_free>\n"
332                  " %s (-h | --help) for detailed help\n"
333                  " %s (-V | --version) for version information\n",
334                  progname, progname, progname, progname);
341 void
342 print_help (void)
344         print_revision (progname, REVISION);
345         printf
346                 ("Copyright (c) %s %s <%s>\n\n%s\n", COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
347         print_usage ();
348         printf
349                 ("\nOptions:\n"
350                  " -w, --warning=INTEGER\n"
351                  "   Exit with WARNING status if less than INTEGER bytes of swap space are free\n"
352                  " -w, --warning=PERCENT%%\n"
353                  "   Exit with WARNING status if more than PERCENT of swap space has been used\n"
354                  " -c, --critical=INTEGER\n"
355                  "   Exit with CRITICAL status if less than INTEGER bytes of swap space are free\n"
356                  " -c, --critical=PERCENT%%\n"
357                  "   Exit with CRITCAL status if more than PERCENT of swap space has been used\n"
358                  " -a, --allswaps\n"
359                  "    Conduct comparisons for all swap partitions, one by one\n"
360                  " -h, --help\n"
361                  "    Print detailed help screen\n"
362                  " -V, --version\n" "    Print version information\n\n");
363         support ();