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 total = total / 1048576;
84 used = used / 1048576;
85 free = free / 1048576;
86 #endif
87 #ifdef HAVE_SWAP
88 if (!allswaps && sun) {
89 asprintf(&swap_command, "%s", "/usr/sbin/swap -s");
90 asprintf(&swap_format, "%s", "%*s %*dk %*s %*s + %*dk %*s = %dk %*s %dk %*s");
91 conv_factor = 2048;
92 } else {
93 asprintf(&swap_command, "%s", SWAP_COMMAND);
94 asprintf(&swap_format, "%s", SWAP_FORMAT);
95 conv_factor = SWAP_CONVERSION;
96 }
98 if (verbose >= 2)
99 printf ("Command: %s\n", swap_command);
100 if (verbose >= 3)
101 printf ("Format: %s\n", swap_format);
103 child_process = spopen (swap_command);
104 if (child_process == NULL) {
105 printf ("Could not open pipe: %s\n", swap_command);
106 return STATE_UNKNOWN;
107 }
109 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
110 if (child_stderr == NULL)
111 printf ("Could not open stderr for %s\n", swap_command);
113 sprintf (str, "%s", "");
114 /* read 1st line */
115 fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
116 if (strcmp (swap_format, "") == 0) {
117 temp_buffer = strtok (input_buffer, " \n");
118 while (temp_buffer) {
119 if (strstr (temp_buffer, "blocks"))
120 sprintf (str, "%s %s", str, "%f");
121 else if (strstr (temp_buffer, "free"))
122 sprintf (str, "%s %s", str, "%f");
123 else
124 sprintf (str, "%s %s", str, "%*s");
125 temp_buffer = strtok (NULL, " \n");
126 }
127 }
129 if (!allswaps && sun) {
130 sscanf (input_buffer, swap_format, &used_swap, &free_swap);
131 used_swap = used_swap / 1024;
132 free_swap = free_swap / 1024;
133 total_swap = used_swap + free_swap;
134 } else {
135 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
136 sscanf (input_buffer, swap_format, &total, &free);
138 total = total / conv_factor;
139 free = free / conv_factor;
140 if (verbose >= 3)
141 printf ("total=%d, free=%d\n", total, free);
143 used = total - free;
144 #endif
145 total_swap += total;
146 used_swap += used;
147 free_swap += free;
148 if (allswaps) {
149 percent = 100 * (((double) used) / ((double) total));
150 result = max_state (result, check_swap (percent, free));
151 if (verbose)
152 asprintf (&status, "%s [%lu (%d%%)]", status, free, 100 - percent);
153 }
154 }
155 }
156 percent_used = 100 * ((double) used_swap) / ((double) total_swap);
157 result = max_state (result, check_swap (percent_used, free_swap));
158 asprintf (&status, " %d%% free (%lu MB out of %lu MB)%s",
159 (100 - percent_used), free_swap, total_swap, status);
161 #ifdef HAVE_PROC_MEMINFO
162 fclose(fp);
163 #endif
164 #ifdef HAVE_SWAP
165 /* If we get anything on STDERR, at least set warning */
166 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
167 result = max_state (result, STATE_WARNING);
169 /* close stderr */
170 (void) fclose (child_stderr);
172 /* close the pipe */
173 if (spclose (child_process))
174 result = max_state (result, STATE_WARNING);
175 #endif
177 terminate (result, "SWAP %s:%s\n", state_text (result), status);
178 }
182 \f
183 int
184 check_swap (int usp, int free_swap)
185 {
186 int result = STATE_UNKNOWN;
187 if (usp >= 0 && usp >= (100.0 - crit_percent))
188 result = STATE_CRITICAL;
189 else if (crit_size >= 0 && free_swap <= crit_size)
190 result = STATE_CRITICAL;
191 else if (usp >= 0 && usp >= (100.0 - warn_percent))
192 result = STATE_WARNING;
193 else if (warn_size >= 0 && free_swap <= warn_size)
194 result = STATE_WARNING;
195 else if (usp >= 0.0)
196 result = STATE_OK;
197 return result;
198 }
201 /* process command-line arguments */
202 int
203 process_arguments (int argc, char **argv)
204 {
205 int c = 0; /* option character */
206 int wc = 0; /* warning counter */
207 int cc = 0; /* critical counter */
209 int option_index = 0;
210 static struct option long_options[] = {
211 {"warning", required_argument, 0, 'w'},
212 {"critical", required_argument, 0, 'c'},
213 {"allswaps", no_argument, 0, 'a'},
214 {"verbose", no_argument, 0, 'v'},
215 {"version", no_argument, 0, 'V'},
216 {"help", no_argument, 0, 'h'},
217 {0, 0, 0, 0}
218 };
220 if (argc < 2)
221 return ERROR;
223 while (1) {
224 c = getopt_long (argc, argv, "+?Vvhac:w:", long_options, &option_index);
226 if (c == -1 || c == EOF)
227 break;
229 switch (c) {
230 case 'w': /* warning time threshold */
231 if (is_intnonneg (optarg)) {
232 warn_size = atoi (optarg);
233 break;
234 }
235 else if (strstr (optarg, ",") &&
236 strstr (optarg, "%") &&
237 sscanf (optarg, "%lu,%d%%", &warn_size, &warn_percent) == 2) {
238 break;
239 }
240 else if (strstr (optarg, "%") &&
241 sscanf (optarg, "%d%%", &warn_percent) == 1) {
242 break;
243 }
244 else {
245 usage ("Warning threshold must be integer or percentage!\n");
246 }
247 wc++;
248 case 'c': /* critical time threshold */
249 if (is_intnonneg (optarg)) {
250 crit_size = atoi (optarg);
251 break;
252 }
253 else if (strstr (optarg, ",") &&
254 strstr (optarg, "%") &&
255 sscanf (optarg, "%lu,%d%%", &crit_size, &crit_percent) == 2) {
256 break;
257 }
258 else if (strstr (optarg, "%") &&
259 sscanf (optarg, "%d%%", &crit_percent) == 1) {
260 break;
261 }
262 else {
263 usage ("Critical threshold must be integer or percentage!\n");
264 }
265 cc++;
266 case 'a': /* all swap */
267 allswaps = TRUE;
268 break;
269 case 'v': /* verbose */
270 verbose++;
271 break;
272 case 'V': /* version */
273 print_revision (progname, "$Revision$");
274 exit (STATE_OK);
275 case 'h': /* help */
276 print_help ();
277 exit (STATE_OK);
278 case '?': /* help */
279 usage ("Invalid argument\n");
280 }
281 }
283 c = optind;
284 if (c == argc)
285 return validate_arguments ();
286 if (warn_percent > 100 && is_intnonneg (argv[c]))
287 warn_percent = atoi (argv[c++]);
289 if (c == argc)
290 return validate_arguments ();
291 if (crit_percent > 100 && is_intnonneg (argv[c]))
292 crit_percent = atoi (argv[c++]);
294 if (c == argc)
295 return validate_arguments ();
296 if (warn_size < 0 && is_intnonneg (argv[c]))
297 warn_size = atoi (argv[c++]);
299 if (c == argc)
300 return validate_arguments ();
301 if (crit_size < 0 && is_intnonneg (argv[c]))
302 crit_size = atoi (argv[c++]);
304 return validate_arguments ();
305 }
311 int
312 validate_arguments (void)
313 {
314 if (warn_percent > 100 && crit_percent > 100 && warn_size < 0
315 && crit_size < 0) {
316 return ERROR;
317 }
318 else if (warn_percent < crit_percent) {
319 usage
320 ("Warning percentage should be more than critical percentage\n");
321 }
322 else if (warn_size < crit_size) {
323 usage
324 ("Warning free space should be more than critical free space\n");
325 }
326 return OK;
327 }
333 void
334 print_usage (void)
335 {
336 printf
337 ("Usage:\n"
338 " %s [-a] -w <used_percentage>%% -c <used_percentage>%%\n"
339 " %s [-a] -w <bytes_free> -c <bytes_free>\n"
340 " %s (-h | --help) for detailed help\n"
341 " %s (-V | --version) for version information\n",
342 progname, progname, progname, progname);
343 }
349 void
350 print_help (void)
351 {
352 print_revision (progname, REVISION);
353 printf
354 ("Copyright (c) %s %s <%s>\n\n%s\n", COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
355 print_usage ();
356 printf
357 ("\nOptions:\n"
358 " -w, --warning=INTEGER\n"
359 " Exit with WARNING status if less than INTEGER bytes of swap space are free\n"
360 " -w, --warning=PERCENT%%\n"
361 " Exit with WARNING status if less than PERCENT of swap space has been used\n"
362 " -c, --critical=INTEGER\n"
363 " Exit with CRITICAL status if less than INTEGER bytes of swap space are free\n"
364 " -c, --critical=PERCENT%%\n"
365 " Exit with CRITCAL status if less than PERCENT of swap space has been used\n"
366 " -a, --allswaps\n"
367 " Conduct comparisons for all swap partitions, one by one\n"
368 " -h, --help\n"
369 " Print detailed help screen\n"
370 " -V, --version\n" " Print version information\n"
371 #ifdef sun
372 "\nOn Solaris, if -a specified, uses swap -l, otherwise uses swap -s.\n"
373 "Will be discrepencies because swap -s counts allocated swap and includes real memory\n"
374 #endif
375 "\n"
376 );
377 support ();
378 }