b8f82db1e7156a09ba2db62f0227133cec73802c
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 #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 free_swap = free_swap * 1024; /* Convert back to bytes as warn and crit specified in bytes */
237 if (usp >= 0 && crit_percent != 0 && usp >= (100.0 - crit_percent))
238 result = STATE_CRITICAL;
239 else if (crit_size > 0 && free_swap <= crit_size)
240 result = STATE_CRITICAL;
241 else if (usp >= 0 && warn_percent != 0 && usp >= (100.0 - warn_percent))
242 result = STATE_WARNING;
243 else if (warn_size > 0 && free_swap <= warn_size)
244 result = STATE_WARNING;
245 else if (usp >= 0.0)
246 result = STATE_OK;
247 return result;
248 }
251 /* process command-line arguments */
252 int
253 process_arguments (int argc, char **argv)
254 {
255 int c = 0; /* option character */
257 int option = 0;
258 static struct option longopts[] = {
259 {"warning", required_argument, 0, 'w'},
260 {"critical", required_argument, 0, 'c'},
261 {"allswaps", no_argument, 0, 'a'},
262 {"verbose", no_argument, 0, 'v'},
263 {"version", no_argument, 0, 'V'},
264 {"help", no_argument, 0, 'h'},
265 {0, 0, 0, 0}
266 };
268 if (argc < 2)
269 return ERROR;
271 while (1) {
272 c = getopt_long (argc, argv, "+?Vvhac:w:", longopts, &option);
274 if (c == -1 || c == EOF)
275 break;
277 switch (c) {
278 case 'w': /* warning size threshold */
279 if (is_intnonneg (optarg)) {
280 warn_size = atoi (optarg);
281 break;
282 }
283 else if (strstr (optarg, ",") &&
284 strstr (optarg, "%") &&
285 sscanf (optarg, "%lu,%d%%", &warn_size, &warn_percent) == 2) {
286 break;
287 }
288 else if (strstr (optarg, "%") &&
289 sscanf (optarg, "%d%%", &warn_percent) == 1) {
290 break;
291 }
292 else {
293 usage (_("Warning threshold must be integer or percentage!\n"));
294 }
295 case 'c': /* critical size threshold */
296 if (is_intnonneg (optarg)) {
297 crit_size = atoi (optarg);
298 break;
299 }
300 else if (strstr (optarg, ",") &&
301 strstr (optarg, "%") &&
302 sscanf (optarg, "%lu,%d%%", &crit_size, &crit_percent) == 2) {
303 break;
304 }
305 else if (strstr (optarg, "%") &&
306 sscanf (optarg, "%d%%", &crit_percent) == 1) {
307 break;
308 }
309 else {
310 usage (_("Critical threshold must be integer or percentage!\n"));
311 }
312 case 'a': /* all swap */
313 allswaps = TRUE;
314 break;
315 case 'v': /* verbose */
316 verbose++;
317 break;
318 case 'V': /* version */
319 print_revision (progname, revision);
320 exit (STATE_OK);
321 case 'h': /* help */
322 print_help ();
323 exit (STATE_OK);
324 case '?': /* help */
325 usage (_("Invalid argument\n"));
326 }
327 }
329 c = optind;
330 if (c == argc)
331 return validate_arguments ();
332 if (warn_percent > 100 && is_intnonneg (argv[c]))
333 warn_percent = atoi (argv[c++]);
335 if (c == argc)
336 return validate_arguments ();
337 if (crit_percent > 100 && is_intnonneg (argv[c]))
338 crit_percent = atoi (argv[c++]);
340 if (c == argc)
341 return validate_arguments ();
342 if (warn_size == 0 && is_intnonneg (argv[c]))
343 warn_size = atoi (argv[c++]);
345 if (c == argc)
346 return validate_arguments ();
347 if (crit_size == 0 && is_intnonneg (argv[c]))
348 crit_size = atoi (argv[c++]);
350 return validate_arguments ();
351 }
357 int
358 validate_arguments (void)
359 {
360 if (warn_percent > 100 && crit_percent > 100 && warn_size == 0
361 && crit_size == 0) {
362 return ERROR;
363 }
364 else if (warn_percent < crit_percent) {
365 usage
366 (_("Warning percentage should be more than critical percentage\n"));
367 }
368 else if (warn_size < crit_size) {
369 usage
370 (_("Warning free space should be more than critical free space\n"));
371 }
372 return OK;
373 }
379 \f
380 void
381 print_help (void)
382 {
383 print_revision (progname, revision);
385 printf (_(COPYRIGHT), copyright, email);
387 printf (_("Check swap space on local server.\n\n"));
389 print_usage ();
391 printf (_(UT_HELP_VRSN));
393 printf (_("\n\
394 -w, --warning=INTEGER\n\
395 Exit with WARNING status if less than INTEGER bytes of swap space are free\n\
396 -w, --warning=PERCENT%%\n\
397 Exit with WARNING status if less than PERCENT of swap space has been used\n\
398 -c, --critical=INTEGER\n\
399 Exit with CRITICAL status if less than INTEGER bytes of swap space are free\n\
400 -c, --critical=PERCENT%%\n\
401 Exit with CRITCAL status if less than PERCENT of swap space has been used\n\
402 -a, --allswaps\n\
403 Conduct comparisons for all swap partitions, one by one\n"));
405 printf (_("\n\
406 On Solaris, if -a specified, uses swap -l, otherwise uses swap -s.\n\
407 Will be discrepencies because swap -s counts allocated swap and includes\n\
408 real memory\n"));
409 printf (_("\n\
410 On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s.\n"));
412 printf (_(UT_SUPPORT));
413 }
418 void
419 print_usage (void)
420 {
421 printf (_("Usage:\n\
422 %s [-a] -w <used_percentage>%% -c <used_percentage>%%\n\
423 %s [-a] -w <bytes_free> -c <bytes_free>\n\
424 %s (-h | --help) for detailed help\n\
425 %s (-V | --version) for version information\n"),
426 progname, progname, progname, progname);
427 }