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