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;
187 }
193 /* process command-line arguments */
194 int
195 process_arguments (int argc, char **argv)
196 {
197 int c = 0; /* option character */
198 int wc = 0; /* warning counter */
199 int cc = 0; /* critical counter */
201 #ifdef HAVE_GETOPT_H
202 int option_index = 0;
203 static struct option long_options[] = {
204 {"warning", required_argument, 0, 'w'},
205 {"critical", required_argument, 0, 'c'},
206 {"allswaps", no_argument, 0, 'a'},
207 {"verbose", no_argument, 0, 'v'},
208 {"version", no_argument, 0, 'V'},
209 {"help", no_argument, 0, 'h'},
210 {0, 0, 0, 0}
211 };
212 #endif
214 if (argc < 2)
215 return ERROR;
217 while (1) {
218 #ifdef HAVE_GETOPT_H
219 c = getopt_long (argc, argv, "+?Vvhac:w:", long_options, &option_index);
220 #else
221 c = getopt (argc, argv, "+?Vvhac:w:");
222 #endif
224 if (c == -1 || c == EOF)
225 break;
227 switch (c) {
228 case 'w': /* warning time threshold */
229 if (is_intnonneg (optarg)) {
230 warn_size = atoi (optarg);
231 break;
232 }
233 else if (strstr (optarg, ",") &&
234 strstr (optarg, "%") &&
235 sscanf (optarg, "%lu,%d%%", &warn_size, &warn_percent) == 2) {
236 break;
237 }
238 else if (strstr (optarg, "%") &&
239 sscanf (optarg, "%d%%", &warn_percent) == 1) {
240 break;
241 }
242 else {
243 usage ("Warning threshold must be integer or percentage!\n");
244 }
245 wc++;
246 case 'c': /* critical time threshold */
247 if (is_intnonneg (optarg)) {
248 crit_size = atoi (optarg);
249 break;
250 }
251 else if (strstr (optarg, ",") &&
252 strstr (optarg, "%") &&
253 sscanf (optarg, "%lu,%d%%", &crit_size, &crit_percent) == 2) {
254 break;
255 }
256 else if (strstr (optarg, "%") &&
257 sscanf (optarg, "%d%%", &crit_percent) == 1) {
258 break;
259 }
260 else {
261 usage ("Critical threshold must be integer or percentage!\n");
262 }
263 cc++;
264 case 'a': /* verbose */
265 allswaps = TRUE;
266 break;
267 case 'v': /* verbose */
268 verbose = TRUE;
269 break;
270 case 'V': /* version */
271 print_revision (progname, "$Revision$");
272 exit (STATE_OK);
273 case 'h': /* help */
274 print_help ();
275 exit (STATE_OK);
276 case '?': /* help */
277 usage ("Invalid argument\n");
278 }
279 }
281 c = optind;
282 if (c == argc)
283 return validate_arguments ();
284 if (warn_percent > 100 && is_intnonneg (argv[c]))
285 warn_percent = atoi (argv[c++]);
287 if (c == argc)
288 return validate_arguments ();
289 if (crit_percent > 100 && is_intnonneg (argv[c]))
290 crit_percent = atoi (argv[c++]);
292 if (c == argc)
293 return validate_arguments ();
294 if (warn_size < 0 && is_intnonneg (argv[c]))
295 warn_size = atoi (argv[c++]);
297 if (c == argc)
298 return validate_arguments ();
299 if (crit_size < 0 && is_intnonneg (argv[c]))
300 crit_size = atoi (argv[c++]);
302 return validate_arguments ();
303 }
309 int
310 validate_arguments (void)
311 {
312 if (warn_percent > 100 && crit_percent > 100 && warn_size < 0
313 && crit_size < 0) {
314 return ERROR;
315 }
316 else if (warn_percent > crit_percent) {
317 usage
318 ("Warning percentage should not be less than critical percentage\n");
319 }
320 else if (warn_size < crit_size) {
321 usage
322 ("Warning free space should not be more than critical free space\n");
323 }
324 return OK;
325 }
331 void
332 print_usage (void)
333 {
334 printf
335 ("Usage:\n"
336 " %s [-a] -w <used_percentage>%% -c <used_percentage>%%\n"
337 " %s [-a] -w <bytes_free> -c <bytes_free>\n"
338 " %s (-h | --help) for detailed help\n"
339 " %s (-V | --version) for version information\n",
340 progname, progname, progname, progname);
341 }
347 void
348 print_help (void)
349 {
350 print_revision (progname, REVISION);
351 printf
352 ("Copyright (c) %s %s <%s>\n\n%s\n", COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
353 print_usage ();
354 printf
355 ("\nOptions:\n"
356 " -w, --warning=INTEGER\n"
357 " Exit with WARNING status if less than INTEGER bytes of swap space are free\n"
358 " -w, --warning=PERCENT%%\n"
359 " Exit with WARNING status if more than PERCENT of swap space has been used\n"
360 " -c, --critical=INTEGER\n"
361 " Exit with CRITICAL status if less than INTEGER bytes of swap space are free\n"
362 " -c, --critical=PERCENT%%\n"
363 " Exit with CRITCAL status if more than PERCENT of swap space has been used\n"
364 " -a, --allswaps\n"
365 " Conduct comparisons for all swap partitions, one by one\n"
366 " -h, --help\n"
367 " Print detailed help screen\n"
368 " -V, --version\n" " Print version information\n\n");
369 support ();
370 }