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 int warn_size = -1;
47 int crit_size = -1;
48 int verbose;
49 int allswaps;
51 int
52 main (int argc, char **argv)
53 {
54 int total_swap = 0, used_swap = 0, free_swap = 0, percent_used;
55 int total, used, free, percent;
56 int result = STATE_OK;
57 char input_buffer[MAX_INPUT_BUFFER];
58 #ifdef HAVE_SWAP
59 char *temp_buffer;
60 #endif
61 #ifdef HAVE_PROC_MEMINFO
62 FILE *fp;
63 #endif
64 char str[32];
65 char *status = "";
67 if (process_arguments (argc, argv) != OK)
68 usage ("Invalid command arguments supplied\n");
70 #ifdef HAVE_PROC_MEMINFO
71 fp = fopen (PROC_MEMINFO, "r");
72 asprintf (&status, "%s", "Swap used:");
73 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
74 if (sscanf (input_buffer, " %s %d %d %d", str, &total, &used, &free) == 4 &&
75 strstr (str, "Swap")) {
76 total_swap += total;
77 used_swap += used;
78 free_swap += free;
79 if (allswaps) {
80 percent = 100 * (((float) used) / ((float) total));
81 if (percent >= crit_percent || free <= crit_size)
82 result = max_state (STATE_CRITICAL, result);
83 else if (percent >= warn_percent || free <= warn_size)
84 result = max_state (STATE_WARNING, result);
85 if (verbose)
86 asprintf (&status, "%s [%d/%d]", status, used, total);
87 }
88 }
89 }
90 percent_used = 100 * (((float) used_swap) / ((float) total_swap));
91 if (percent_used >= crit_percent || free_swap <= crit_size)
92 result = max_state (STATE_CRITICAL, result);
93 else if (percent_used >= warn_percent || free_swap <= warn_size)
94 result = max_state (STATE_WARNING, result);
95 asprintf (&status, "%s %2d%% (%d out of %d)", status, percent_used,
96 used_swap, total_swap);
97 fclose (fp);
98 #else
99 #ifdef HAVE_SWAP
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 asprintf (&status, "%s", "Swap used:");
127 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
128 sscanf (input_buffer, SWAP_FORMAT, &total, &free);
129 used = total - free;
130 total_swap += total;
131 used_swap += used;
132 free_swap += free;
133 if (allswaps) {
134 percent = 100 * (((float) used) / ((float) total));
135 if (percent >= crit_percent || free <= crit_size)
136 result = max_state (STATE_CRITICAL, result);
137 else if (percent >= warn_percent || free <= warn_size)
138 result = max_state (STATE_WARNING, result);
139 if (verbose)
140 asprintf (&status, "%s [%d/%d]", status, used, total);
141 }
142 }
143 percent_used = 100 * ((float) used_swap) / ((float) total_swap);
144 asprintf (&status, "%s %2d%% (%d out of %d)",
145 status, percent_used, used_swap, total_swap);
146 if (percent_used >= crit_percent || free_swap <= crit_size)
147 result = max_state (STATE_CRITICAL, result);
148 else if (percent_used >= warn_percent || free_swap <= warn_size)
149 result = max_state (STATE_WARNING, result);
151 /* If we get anything on STDERR, at least set warning */
152 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
153 result = max_state (result, STATE_WARNING);
155 /* close stderr */
156 (void) fclose (child_stderr);
158 /* close the pipe */
159 if (spclose (child_process))
160 result = max_state (result, STATE_WARNING);
161 #endif
162 #endif
164 #ifndef SWAP_COMMAND
165 #ifndef SWAP_FILE
166 #ifndef HAVE_PROC_MEMINFO
167 return STATE_UNKNOWN;
168 #endif
169 #endif
170 #endif
172 if (result == STATE_OK)
173 printf ("Swap ok - %s\n", status);
174 else if (result == STATE_CRITICAL)
175 printf ("CRITICAL - %s\n", status);
176 else if (result == STATE_WARNING)
177 printf ("WARNING - %s\n", status);
178 else if (result == STATE_UNKNOWN)
179 printf ("Unable to read output\n");
180 else {
181 result = STATE_UNKNOWN;
182 printf ("UNKNOWN - %s\n", status);
183 }
185 return result;
186 }
192 /* process command-line arguments */
193 int
194 process_arguments (int argc, char **argv)
195 {
196 int c = 0; /* option character */
197 int wc = 0; /* warning counter */
198 int cc = 0; /* critical counter */
200 #ifdef HAVE_GETOPT_H
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 };
211 #endif
213 if (argc < 2)
214 return ERROR;
216 while (1) {
217 #ifdef HAVE_GETOPT_H
218 c = getopt_long (argc, argv, "+?Vvhac:w:", long_options, &option_index);
219 #else
220 c = getopt (argc, argv, "+?Vvhac:w:");
221 #endif
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, "%d,%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, "%d,%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': /* verbose */
264 allswaps = TRUE;
265 break;
266 case 'v': /* verbose */
267 verbose = TRUE;
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 ();
302 }
308 int
309 validate_arguments (void)
310 {
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 not be less than critical percentage\n");
318 }
319 else if (warn_size < crit_size) {
320 usage
321 ("Warning free space should not be more than critical free space\n");
322 }
323 return OK;
324 }
330 void
331 print_usage (void)
332 {
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);
340 }
346 void
347 print_help (void)
348 {
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 more 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 more 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\n");
368 support ();
369 }