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, 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 void
55 print_usage (void)
56 {
57 printf (_("Usage:\n\
58 %s [-a] -w <used_percentage>%% -c <used_percentage>%%\n\
59 %s [-a] -w <bytes_free> -c <bytes_free>\n\
60 %s (-h | --help) for detailed help\n\
61 %s (-V | --version) for version information\n"),
62 progname, progname, progname, progname);
63 }
69 void
70 print_help (void)
71 {
72 print_revision (progname, revision);
74 printf (_(COPYRIGHT), copyright, email);
76 printf (_("Check swap space on local server.\n\n"));
78 print_usage ();
80 printf (_(UT_HELP_VRSN));
82 printf (_("\n\
83 -w, --warning=INTEGER\n\
84 Exit with WARNING status if less than INTEGER bytes of swap space are free\n\
85 -w, --warning=PERCENT%%\n\
86 Exit with WARNING status if less than PERCENT of swap space has been used\n\
87 -c, --critical=INTEGER\n\
88 Exit with CRITICAL status if less than INTEGER bytes of swap space are free\n\
89 -c, --critical=PERCENT%%\n\
90 Exit with CRITCAL status if less than PERCENT of swap space has been used\n\
91 -a, --allswaps\n\
92 Conduct comparisons for all swap partitions, one by one\n"));
94 #ifdef sun
95 printf (_("\n\
96 On Solaris, if -a specified, uses swap -l, otherwise uses swap -s.\n\
97 Will be discrepencies because swap -s counts allocated swap and includes\n\
98 real memory\n"));
99 #endif
101 support ();
102 }
103 \f
106 int
107 main (int argc, char **argv)
108 {
109 int percent_used, percent;
110 long unsigned int total_swap = 0, used_swap = 0, free_swap = 0;
111 long unsigned int dsktotal, dskused, dskfree;
112 int result = STATE_OK;
113 char input_buffer[MAX_INPUT_BUFFER];
114 #ifdef HAVE_SWAP
115 int conv_factor; /* Convert to MBs */
116 char *temp_buffer;
117 char *swap_command;
118 char *swap_format;
119 #endif
120 #ifdef HAVE_PROC_MEMINFO
121 FILE *fp;
122 #endif
123 char str[32];
124 char *status = "";
126 if (process_arguments (argc, argv) != OK)
127 usage (_("Invalid command arguments supplied\n"));
129 #ifdef HAVE_PROC_MEMINFO
130 fp = fopen (PROC_MEMINFO, "r");
131 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
132 if (sscanf (input_buffer, " %s %lu %lu %lu", str, &dsktotal, &dskused, &dskfree) == 4 &&
133 strstr (str, "Swap")) {
134 dsktotal = dsktotal / 1048576;
135 dskused = dskused / 1048576;
136 dskfree = dskfree / 1048576;
137 #endif
138 #ifdef HAVE_SWAP
139 if (!allswaps && sun) {
140 asprintf(&swap_command, "%s", "/usr/sbin/swap -s");
141 asprintf(&swap_format, "%s", "%*s %*dk %*s %*s + %*dk %*s = %dk %*s %dk %*s");
142 conv_factor = 2048;
143 } else {
144 asprintf(&swap_command, "%s", SWAP_COMMAND);
145 asprintf(&swap_format, "%s", SWAP_FORMAT);
146 conv_factor = SWAP_CONVERSION;
147 }
149 if (verbose >= 2)
150 printf (_("Command: %s\n"), swap_command);
151 if (verbose >= 3)
152 printf ("_(Format: %s\n"), swap_format);
154 child_process = spopen (swap_command);
155 if (child_process == NULL) {
156 printf (_("Could not open pipe: %s\n"), swap_command);
157 return STATE_UNKNOWN;
158 }
160 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
161 if (child_stderr == NULL)
162 printf (_("Could not open stderr for %s\n"), swap_command);
164 sprintf (str, "%s", "");
165 /* read 1st line */
166 fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
167 if (strcmp (swap_format, "") == 0) {
168 temp_buffer = strtok (input_buffer, " \n");
169 while (temp_buffer) {
170 if (strstr (temp_buffer, "blocks"))
171 sprintf (str, "%s %s", str, "%f");
172 else if (strstr (temp_buffer, "dskfree"))
173 sprintf (str, "%s %s", str, "%f");
174 else
175 sprintf (str, "%s %s", str, "%*s");
176 temp_buffer = strtok (NULL, " \n");
177 }
178 }
180 if (!allswaps && sun) {
181 sscanf (input_buffer, swap_format, &used_swap, &free_swap);
182 used_swap = used_swap / 1024;
183 free_swap = free_swap / 1024;
184 total_swap = used_swap + free_swap;
185 } else {
186 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
187 sscanf (input_buffer, swap_format, &dsktotal, &dskfree);
189 dsktotal = dsktotal / conv_factor;
190 dskfree = dskfree / conv_factor;
191 if (verbose >= 3)
192 printf (_("total=%d, free=%d\n"), dsktotal, dskfree);
194 dskused = dsktotal - dskfree;
195 #endif
196 total_swap += dsktotal;
197 used_swap += dskused;
198 free_swap += dskfree;
199 if (allswaps) {
200 percent = 100 * (((double) dskused) / ((double) dsktotal));
201 result = max_state (result, check_swap (percent, dskfree));
202 if (verbose)
203 asprintf (&status, "%s [%lu (%d%%)]", status, dskfree, 100 - percent);
204 }
205 }
206 }
207 percent_used = 100 * ((double) used_swap) / ((double) total_swap);
208 result = max_state (result, check_swap (percent_used, free_swap));
209 asprintf (&status, _(" %d%% free (%lu MB out of %lu MB)%s"),
210 (100 - percent_used), free_swap, total_swap, status);
212 #ifdef HAVE_PROC_MEMINFO
213 fclose(fp);
214 #endif
215 #ifdef HAVE_SWAP
216 /* If we get anything on STDERR, at least set warning */
217 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
218 result = max_state (result, STATE_WARNING);
220 /* close stderr */
221 (void) fclose (child_stderr);
223 /* close the pipe */
224 if (spclose (child_process))
225 result = max_state (result, STATE_WARNING);
226 #endif
228 die (result, "SWAP %s:%s\n", state_text (result), status);
229 return STATE_UNKNOWN;
230 }
234 \f
235 int
236 check_swap (int usp, int free_swap)
237 {
238 int result = STATE_UNKNOWN;
239 if (usp >= 0 && usp >= (100.0 - crit_percent))
240 result = STATE_CRITICAL;
241 else if (crit_size > 0 && (unsigned)free_swap <= crit_size)
242 result = STATE_CRITICAL;
243 else if (usp >= 0 && usp >= (100.0 - warn_percent))
244 result = STATE_WARNING;
245 else if (warn_size > 0 && (unsigned)free_swap <= warn_size)
246 result = STATE_WARNING;
247 else if (usp >= 0.0)
248 result = STATE_OK;
249 return result;
250 }
253 /* process command-line arguments */
254 int
255 process_arguments (int argc, char **argv)
256 {
257 int c = 0; /* option character */
258 int wc = 0; /* warning counter */
259 int cc = 0; /* critical counter */
261 int option_index = 0;
262 static struct option long_options[] = {
263 {"warning", required_argument, 0, 'w'},
264 {"critical", required_argument, 0, 'c'},
265 {"allswaps", no_argument, 0, 'a'},
266 {"verbose", no_argument, 0, 'v'},
267 {"version", no_argument, 0, 'V'},
268 {"help", no_argument, 0, 'h'},
269 {0, 0, 0, 0}
270 };
272 if (argc < 2)
273 return ERROR;
275 while (1) {
276 c = getopt_long (argc, argv, "+?Vvhac:w:", long_options, &option_index);
278 if (c == -1 || c == EOF)
279 break;
281 switch (c) {
282 case 'w': /* warning time threshold */
283 if (is_intnonneg (optarg)) {
284 warn_size = atoi (optarg);
285 break;
286 }
287 else if (strstr (optarg, ",") &&
288 strstr (optarg, "%") &&
289 sscanf (optarg, "%lu,%d%%", &warn_size, &warn_percent) == 2) {
290 break;
291 }
292 else if (strstr (optarg, "%") &&
293 sscanf (optarg, "%d%%", &warn_percent) == 1) {
294 break;
295 }
296 else {
297 usage (_("Warning threshold must be integer or percentage!\n"));
298 }
299 wc++;
300 case 'c': /* critical time threshold */
301 if (is_intnonneg (optarg)) {
302 crit_size = atoi (optarg);
303 break;
304 }
305 else if (strstr (optarg, ",") &&
306 strstr (optarg, "%") &&
307 sscanf (optarg, "%lu,%d%%", &crit_size, &crit_percent) == 2) {
308 break;
309 }
310 else if (strstr (optarg, "%") &&
311 sscanf (optarg, "%d%%", &crit_percent) == 1) {
312 break;
313 }
314 else {
315 usage (_("Critical threshold must be integer or percentage!\n"));
316 }
317 cc++;
318 case 'a': /* all swap */
319 allswaps = TRUE;
320 break;
321 case 'v': /* verbose */
322 verbose++;
323 break;
324 case 'V': /* version */
325 print_revision (progname, revision);
326 exit (STATE_OK);
327 case 'h': /* help */
328 print_help ();
329 exit (STATE_OK);
330 case '?': /* help */
331 usage (_("Invalid argument\n"));
332 }
333 }
335 c = optind;
336 if (c == argc)
337 return validate_arguments ();
338 if (warn_percent > 100 && is_intnonneg (argv[c]))
339 warn_percent = atoi (argv[c++]);
341 if (c == argc)
342 return validate_arguments ();
343 if (crit_percent > 100 && is_intnonneg (argv[c]))
344 crit_percent = atoi (argv[c++]);
346 if (c == argc)
347 return validate_arguments ();
348 if (warn_size == 0 && is_intnonneg (argv[c]))
349 warn_size = atoi (argv[c++]);
351 if (c == argc)
352 return validate_arguments ();
353 if (crit_size == 0 && is_intnonneg (argv[c]))
354 crit_size = atoi (argv[c++]);
356 return validate_arguments ();
357 }
363 int
364 validate_arguments (void)
365 {
366 if (warn_percent > 100 && crit_percent > 100 && warn_size == 0
367 && crit_size == 0) {
368 return ERROR;
369 }
370 else if (warn_percent < crit_percent) {
371 usage
372 (_("Warning percentage should be more than critical percentage\n"));
373 }
374 else if (warn_size < crit_size) {
375 usage
376 (_("Warning free space should be more than critical free space\n"));
377 }
378 return OK;
379 }