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 #if !defined(sun)
51 int sun = 0; /* defined by compiler if it is a sun solaris system */
52 #endif
54 int
55 main (int argc, char **argv)
56 {
57 int percent_used, percent;
58 long unsigned int total_swap = 0, used_swap = 0, free_swap = 0;
59 long unsigned int dsktotal, dskused, dskfree;
60 int result = STATE_OK;
61 char input_buffer[MAX_INPUT_BUFFER];
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 #ifdef HAVE_PROC_MEMINFO
69 FILE *fp;
70 #endif
71 char str[32];
72 char *status;
74 status = strdup("");
76 if (process_arguments (argc, argv) != OK)
77 usage (_("Invalid command arguments supplied\n"));
79 #ifdef HAVE_PROC_MEMINFO
80 fp = fopen (PROC_MEMINFO, "r");
81 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
82 if (sscanf (input_buffer, " %s %lu %lu %lu", str, &dsktotal, &dskused, &dskfree) == 4 &&
83 strstr (str, "Swap")) {
84 dsktotal = dsktotal / 1048576;
85 dskused = dskused / 1048576;
86 dskfree = dskfree / 1048576;
87 #endif
88 #ifdef HAVE_SWAP
89 if (!allswaps && sun) {
90 asprintf(&swap_command, "%s", "/usr/sbin/swap -s");
91 asprintf(&swap_format, "%s", "%*s %*dk %*s %*s + %*dk %*s = %dk %*s %dk %*s");
92 conv_factor = 2048;
93 } else {
94 asprintf(&swap_command, "%s", SWAP_COMMAND);
95 asprintf(&swap_format, "%s", SWAP_FORMAT);
96 conv_factor = SWAP_CONVERSION;
97 }
99 if (verbose >= 2)
100 printf (_("Command: %s\n"), swap_command);
101 if (verbose >= 3)
102 printf ("_(Format: %s\n"), swap_format);
104 child_process = spopen (swap_command);
105 if (child_process == NULL) {
106 printf (_("Could not open pipe: %s\n"), swap_command);
107 return STATE_UNKNOWN;
108 }
110 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
111 if (child_stderr == NULL)
112 printf (_("Could not open stderr for %s\n"), swap_command);
114 sprintf (str, "%s", "");
115 /* read 1st line */
116 fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process);
117 if (strcmp (swap_format, "") == 0) {
118 temp_buffer = strtok (input_buffer, " \n");
119 while (temp_buffer) {
120 if (strstr (temp_buffer, "blocks"))
121 sprintf (str, "%s %s", str, "%f");
122 else if (strstr (temp_buffer, "dskfree"))
123 sprintf (str, "%s %s", str, "%f");
124 else
125 sprintf (str, "%s %s", str, "%*s");
126 temp_buffer = strtok (NULL, " \n");
127 }
128 }
130 if (!allswaps && sun) {
131 sscanf (input_buffer, swap_format, &used_swap, &free_swap);
132 used_swap = used_swap / 1024;
133 free_swap = free_swap / 1024;
134 total_swap = used_swap + free_swap;
135 } else {
136 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
137 sscanf (input_buffer, swap_format, &dsktotal, &dskfree);
139 dsktotal = dsktotal / conv_factor;
140 dskfree = dskfree / conv_factor;
141 if (verbose >= 3)
142 printf (_("total=%d, free=%d\n"), dsktotal, dskfree);
144 dskused = dsktotal - dskfree;
145 #endif
146 total_swap += dsktotal;
147 used_swap += dskused;
148 free_swap += dskfree;
149 if (allswaps) {
150 percent = 100 * (((double) dskused) / ((double) dsktotal));
151 result = max_state (result, check_swap (percent, dskfree));
152 if (verbose)
153 asprintf (&status, "%s [%lu (%d%%)]", status, dskfree, 100 - percent);
154 }
155 }
156 }
157 percent_used = 100 * ((double) used_swap) / ((double) total_swap);
158 result = max_state (result, check_swap (percent_used, free_swap));
159 asprintf (&status, _(" %d%% free (%lu MB out of %lu MB)%s"),
160 (100 - percent_used), free_swap, total_swap, status);
162 #ifdef HAVE_PROC_MEMINFO
163 fclose(fp);
164 #endif
165 #ifdef HAVE_SWAP
166 /* If we get anything on STDERR, at least set warning */
167 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
168 result = max_state (result, STATE_WARNING);
170 /* close stderr */
171 (void) fclose (child_stderr);
173 /* close the pipe */
174 if (spclose (child_process))
175 result = max_state (result, STATE_WARNING);
176 #endif
178 die (result, "SWAP %s:%s\n", state_text (result), status);
179 return STATE_UNKNOWN;
180 }
184 \f
185 int
186 check_swap (int usp, long unsigned int free_swap)
187 {
188 int result = STATE_UNKNOWN;
189 if (usp >= 0 && usp >= (100.0 - crit_percent))
190 result = STATE_CRITICAL;
191 else if (crit_size > 0 && free_swap <= crit_size)
192 result = STATE_CRITICAL;
193 else if (usp >= 0 && usp >= (100.0 - warn_percent))
194 result = STATE_WARNING;
195 else if (warn_size > 0 && free_swap <= warn_size)
196 result = STATE_WARNING;
197 else if (usp >= 0.0)
198 result = STATE_OK;
199 return result;
200 }
203 /* process command-line arguments */
204 int
205 process_arguments (int argc, char **argv)
206 {
207 int c = 0; /* option character */
208 int wc = 0; /* warning counter */
209 int cc = 0; /* critical counter */
211 int option = 0;
212 static struct option longopts[] = {
213 {"warning", required_argument, 0, 'w'},
214 {"critical", required_argument, 0, 'c'},
215 {"allswaps", no_argument, 0, 'a'},
216 {"verbose", no_argument, 0, 'v'},
217 {"version", no_argument, 0, 'V'},
218 {"help", no_argument, 0, 'h'},
219 {0, 0, 0, 0}
220 };
222 if (argc < 2)
223 return ERROR;
225 while (1) {
226 c = getopt_long (argc, argv, "+?Vvhac:w:", longopts, &option);
228 if (c == -1 || c == EOF)
229 break;
231 switch (c) {
232 case 'w': /* warning time threshold */
233 if (is_intnonneg (optarg)) {
234 warn_size = atoi (optarg);
235 break;
236 }
237 else if (strstr (optarg, ",") &&
238 strstr (optarg, "%") &&
239 sscanf (optarg, "%lu,%d%%", &warn_size, &warn_percent) == 2) {
240 break;
241 }
242 else if (strstr (optarg, "%") &&
243 sscanf (optarg, "%d%%", &warn_percent) == 1) {
244 break;
245 }
246 else {
247 usage (_("Warning threshold must be integer or percentage!\n"));
248 }
249 case 'c': /* critical time threshold */
250 if (is_intnonneg (optarg)) {
251 crit_size = atoi (optarg);
252 break;
253 }
254 else if (strstr (optarg, ",") &&
255 strstr (optarg, "%") &&
256 sscanf (optarg, "%lu,%d%%", &crit_size, &crit_percent) == 2) {
257 break;
258 }
259 else if (strstr (optarg, "%") &&
260 sscanf (optarg, "%d%%", &crit_percent) == 1) {
261 break;
262 }
263 else {
264 usage (_("Critical threshold must be integer or percentage!\n"));
265 }
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 \f
334 void
335 print_help (void)
336 {
337 print_revision (progname, revision);
339 printf (_(COPYRIGHT), copyright, email);
341 printf (_("Check swap space on local server.\n\n"));
343 print_usage ();
345 printf (_(UT_HELP_VRSN));
347 printf (_("\n\
348 -w, --warning=INTEGER\n\
349 Exit with WARNING status if less than INTEGER bytes of swap space are free\n\
350 -w, --warning=PERCENT%%\n\
351 Exit with WARNING status if less than PERCENT of swap space has been used\n\
352 -c, --critical=INTEGER\n\
353 Exit with CRITICAL status if less than INTEGER bytes of swap space are free\n\
354 -c, --critical=PERCENT%%\n\
355 Exit with CRITCAL status if less than PERCENT of swap space has been used\n\
356 -a, --allswaps\n\
357 Conduct comparisons for all swap partitions, one by one\n"));
359 #ifdef sun
360 printf (_("\n\
361 On Solaris, if -a specified, uses swap -l, otherwise uses swap -s.\n\
362 Will be discrepencies because swap -s counts allocated swap and includes\n\
363 real memory\n"));
364 #endif
366 printf (_(UT_SUPPORT));
367 }
372 void
373 print_usage (void)
374 {
375 printf (_("Usage:\n\
376 %s [-a] -w <used_percentage>%% -c <used_percentage>%%\n\
377 %s [-a] -w <bytes_free> -c <bytes_free>\n\
378 %s (-h | --help) for detailed help\n\
379 %s (-V | --version) for version information\n"),
380 progname, progname, progname, progname);
381 }