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, long unsigned 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 = 0;
44 int crit_percent = 0;
45 long unsigned int warn_size = 0;
46 long unsigned int crit_size = 0;
47 int verbose;
48 int allswaps;
50 int
51 main (int argc, char **argv)
52 {
53 int percent_used, percent;
54 long unsigned int total_swap = 0, used_swap = 0, free_swap = 0;
55 long unsigned int dsktotal, dskused, dskfree;
56 int result = STATE_OK;
57 char input_buffer[MAX_INPUT_BUFFER];
58 char *perf;
59 #ifdef HAVE_PROC_MEMINFO
60 FILE *fp;
61 #else
62 # ifdef HAVE_SWAP
63 int conv_factor; /* Convert to MBs */
64 char *temp_buffer;
65 char *swap_command;
66 char *swap_format;
67 # endif
68 #endif
69 char str[32];
70 char *status;
72 setlocale (LC_ALL, "");
73 bindtextdomain (PACKAGE, LOCALEDIR);
74 textdomain (PACKAGE);
76 status = strdup("");
77 perf = strdup("");
79 if (process_arguments (argc, argv) != OK)
80 usage (_("Invalid command arguments supplied\n"));
82 #ifdef HAVE_PROC_MEMINFO
83 fp = fopen (PROC_MEMINFO, "r");
84 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
85 if (sscanf (input_buffer, " %s %lu %lu %lu", str, &dsktotal, &dskused, &dskfree) == 4 &&
86 strstr (str, "Swap")) {
87 dsktotal = dsktotal / 1048576;
88 dskused = dskused / 1048576;
89 dskfree = dskfree / 1048576;
90 total_swap += dsktotal;
91 used_swap += dskused;
92 free_swap += dskfree;
93 if (allswaps) {
94 percent = 100 * (((double) dskused) / ((double) dsktotal));
95 result = max_state (result, check_swap (percent, dskfree));
96 if (verbose)
97 asprintf (&status, "%s [%lu (%d%%)]", status, dskfree, 100 - percent);
98 }
99 }
100 }
101 fclose(fp);
102 #else
103 # ifdef HAVE_SWAP
104 asprintf(&swap_command, "%s", SWAP_COMMAND);
105 asprintf(&swap_format, "%s", SWAP_FORMAT);
106 conv_factor = SWAP_CONVERSION;
108 /* These override the command used if a summary (and thus ! allswaps) is required */
109 /* The summary flag returns more accurate information about swap usage on these OSes */
110 # ifdef _AIX
111 if (!allswaps) {
112 asprintf(&swap_command, "%s", "/usr/sbin/lsps -s");
113 asprintf(&swap_format, "%s", "%d%*s %d");
114 conv_factor = 1;
115 }
116 # else
117 # ifdef sun
118 if (!allswaps) {
119 asprintf(&swap_command, "%s", "/usr/sbin/swap -s");
120 asprintf(&swap_format, "%s", "%*s %*dk %*s %*s + %*dk %*s = %dk %*s %dk %*s");
121 conv_factor = 2048;
122 }
123 # endif
124 # endif
126 if (verbose >= 2)
127 printf (_("Command: %s\n"), swap_command);
128 if (verbose >= 3)
129 printf (_("Format: %s\n"), swap_format);
131 child_process = spopen (swap_command);
132 if (child_process == NULL) {
133 printf (_("Could not open pipe: %s\n"), swap_command);
134 return STATE_UNKNOWN;
135 }
137 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
138 if (child_stderr == NULL)
139 printf (_("Could not open stderr for %s\n"), swap_command);
141 sprintf (str, "%s", "");
142 /* read 1st line */
143 fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
144 if (strcmp (swap_format, "") == 0) {
145 temp_buffer = strtok (input_buffer, " \n");
146 while (temp_buffer) {
147 if (strstr (temp_buffer, "blocks"))
148 sprintf (str, "%s %s", str, "%f");
149 else if (strstr (temp_buffer, "dskfree"))
150 sprintf (str, "%s %s", str, "%f");
151 else
152 sprintf (str, "%s %s", str, "%*s");
153 temp_buffer = strtok (NULL, " \n");
154 }
155 }
157 /* If different swap command is used for summary switch, need to read format differently */
158 # ifdef _AIX
159 if (!allswaps) {
160 fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process); /* Ignore first line */
161 sscanf (input_buffer, swap_format, &total_swap, &used_swap);
162 free_swap = total_swap * (100 - used_swap) /100;
163 used_swap = total_swap - free_swap;
164 if (verbose >= 3)
165 printf (_("total=%d, used=%d, free=%d\n"), total_swap, used_swap, free_swap);
166 } else {
167 # else
168 # ifdef sun
169 if (!allswaps) {
170 sscanf (input_buffer, swap_format, &used_swap, &free_swap);
171 used_swap = used_swap / 1024;
172 free_swap = free_swap / 1024;
173 total_swap = used_swap + free_swap;
174 } else {
175 # endif
176 # endif
177 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
178 sscanf (input_buffer, swap_format, &dsktotal, &dskfree);
180 dsktotal = dsktotal / conv_factor;
181 /* AIX lists percent used, so this converts to dskfree in MBs */
182 # ifdef _AIX
183 dskfree = dsktotal * (100 - dskfree) / 100;
184 # else
185 dskfree = dskfree / conv_factor;
186 # endif
187 if (verbose >= 3)
188 printf (_("total=%d, free=%d\n"), dsktotal, dskfree);
190 dskused = dsktotal - dskfree;
191 total_swap += dsktotal;
192 used_swap += dskused;
193 free_swap += dskfree;
194 if (allswaps) {
195 percent = 100 * (((double) dskused) / ((double) dsktotal));
196 result = max_state (result, check_swap (percent, dskfree));
197 if (verbose)
198 asprintf (&status, "%s [%lu (%d%%)]", status, dskfree, 100 - percent);
199 }
200 }
201 # ifdef _AIX
202 }
203 # else
204 # ifdef sun
205 }
206 # endif
207 # endif
209 /* If we get anything on STDERR, at least set warning */
210 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
211 result = max_state (result, STATE_WARNING);
213 /* close stderr */
214 (void) fclose (child_stderr);
216 /* close the pipe */
217 if (spclose (child_process))
218 result = max_state (result, STATE_WARNING);
219 # endif /* HAVE_SWAP */
220 #endif /* HAVE_PROC_MEMINFO */
222 percent_used = 100 * ((double) used_swap) / ((double) total_swap);
223 result = max_state (result, check_swap (percent_used, free_swap));
224 asprintf (&status, _(" %d%% free (%lu MB out of %lu MB)%s"),
225 (100 - percent_used), free_swap, total_swap, status);
227 asprintf (&perf, "%s", perfdata ("swap", (long) free_swap, "MB",
228 TRUE, (long) max (warn_size/1024, warn_percent/100.0*total_swap),
229 TRUE, (long) max (crit_size/1024, crit_percent/100.0*total_swap),
230 TRUE, 0,
231 TRUE, (long) total_swap));
232 printf ("SWAP %s:%s |%s\n", state_text (result), status, perf);
233 return result;
234 }
238 \f
239 int
240 check_swap (int usp, long unsigned int free_swap)
241 {
242 int result = STATE_UNKNOWN;
243 free_swap = free_swap * 1024; /* Convert back to bytes as warn and crit specified in bytes */
244 if (usp >= 0 && crit_percent != 0 && usp >= (100.0 - crit_percent))
245 result = STATE_CRITICAL;
246 else if (crit_size > 0 && free_swap <= crit_size)
247 result = STATE_CRITICAL;
248 else if (usp >= 0 && warn_percent != 0 && usp >= (100.0 - warn_percent))
249 result = STATE_WARNING;
250 else if (warn_size > 0 && free_swap <= warn_size)
251 result = STATE_WARNING;
252 else if (usp >= 0.0)
253 result = STATE_OK;
254 return result;
255 }
258 /* process command-line arguments */
259 int
260 process_arguments (int argc, char **argv)
261 {
262 int c = 0; /* option character */
264 int option = 0;
265 static struct option longopts[] = {
266 {"warning", required_argument, 0, 'w'},
267 {"critical", required_argument, 0, 'c'},
268 {"allswaps", no_argument, 0, 'a'},
269 {"verbose", no_argument, 0, 'v'},
270 {"version", no_argument, 0, 'V'},
271 {"help", no_argument, 0, 'h'},
272 {0, 0, 0, 0}
273 };
275 if (argc < 2)
276 return ERROR;
278 while (1) {
279 c = getopt_long (argc, argv, "+?Vvhac:w:", longopts, &option);
281 if (c == -1 || c == EOF)
282 break;
284 switch (c) {
285 case 'w': /* warning size threshold */
286 if (is_intnonneg (optarg)) {
287 warn_size = atoi (optarg);
288 break;
289 }
290 else if (strstr (optarg, ",") &&
291 strstr (optarg, "%") &&
292 sscanf (optarg, "%lu,%d%%", &warn_size, &warn_percent) == 2) {
293 break;
294 }
295 else if (strstr (optarg, "%") &&
296 sscanf (optarg, "%d%%", &warn_percent) == 1) {
297 break;
298 }
299 else {
300 usage (_("Warning threshold must be integer or percentage!\n"));
301 }
302 case 'c': /* critical size threshold */
303 if (is_intnonneg (optarg)) {
304 crit_size = atoi (optarg);
305 break;
306 }
307 else if (strstr (optarg, ",") &&
308 strstr (optarg, "%") &&
309 sscanf (optarg, "%lu,%d%%", &crit_size, &crit_percent) == 2) {
310 break;
311 }
312 else if (strstr (optarg, "%") &&
313 sscanf (optarg, "%d%%", &crit_percent) == 1) {
314 break;
315 }
316 else {
317 usage (_("Critical threshold must be integer or percentage!\n"));
318 }
319 case 'a': /* all swap */
320 allswaps = TRUE;
321 break;
322 case 'v': /* verbose */
323 verbose++;
324 break;
325 case 'V': /* version */
326 print_revision (progname, revision);
327 exit (STATE_OK);
328 case 'h': /* help */
329 print_help ();
330 exit (STATE_OK);
331 case '?': /* error */
332 usage (_("Invalid argument\n"));
333 }
334 }
336 c = optind;
337 if (c == argc)
338 return validate_arguments ();
339 if (warn_percent == 0 && is_intnonneg (argv[c]))
340 warn_percent = atoi (argv[c++]);
342 if (c == argc)
343 return validate_arguments ();
344 if (crit_percent == 0 && is_intnonneg (argv[c]))
345 crit_percent = atoi (argv[c++]);
347 if (c == argc)
348 return validate_arguments ();
349 if (warn_size == 0 && is_intnonneg (argv[c]))
350 warn_size = atoi (argv[c++]);
352 if (c == argc)
353 return validate_arguments ();
354 if (crit_size == 0 && is_intnonneg (argv[c]))
355 crit_size = atoi (argv[c++]);
357 return validate_arguments ();
358 }
364 int
365 validate_arguments (void)
366 {
367 if (warn_percent == 0 && crit_percent == 0 && warn_size == 0
368 && crit_size == 0) {
369 return ERROR;
370 }
371 else if (warn_percent < crit_percent) {
372 usage
373 (_("Warning percentage should be more than critical percentage\n"));
374 }
375 else if (warn_size < crit_size) {
376 usage
377 (_("Warning free space should be more than critical free space\n"));
378 }
379 return OK;
380 }
386 \f
387 void
388 print_help (void)
389 {
390 print_revision (progname, revision);
392 printf (_(COPYRIGHT), copyright, email);
394 printf (_("Check swap space on local server.\n\n"));
396 print_usage ();
398 printf (_(UT_HELP_VRSN));
400 printf (_("\n\
401 -w, --warning=INTEGER\n\
402 Exit with WARNING status if less than INTEGER bytes of swap space are free\n\
403 -w, --warning=PERCENT%%\n\
404 Exit with WARNING status if less than PERCENT of swap space is free\n\
405 -c, --critical=INTEGER\n\
406 Exit with CRITICAL status if less than INTEGER bytes of swap space are free\n\
407 -c, --critical=PERCENT%%\n\
408 Exit with CRITCAL status if less than PERCENT of swap space is free\n\
409 -a, --allswaps\n\
410 Conduct comparisons for all swap partitions, one by one\n\
411 -v, --verbose\n\
412 Verbose output. Up to 3 levels\n"));
414 printf (_("\n\
415 On Solaris, if -a specified, uses swap -l, otherwise uses swap -s.\n\
416 Will be discrepencies because swap -s counts allocated swap and includes\n\
417 real memory\n"));
418 printf (_("\n\
419 On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s.\n"));
421 printf (_(UT_SUPPORT));
422 }
427 void
428 print_usage (void)
429 {
430 printf (_("Usage:\n\
431 %s [-av] -w <percent_free>%% -c <percent_free>%%\n\
432 %s [-av] -w <bytes_free> -c <bytes_free>\n\
433 %s (-h | --help) for detailed help\n\
434 %s (-V | --version) for version information\n"),
435 progname, progname, progname, progname);
436 }