summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--plugins/check_swap.c115
1 files changed, 85 insertions, 30 deletions
diff --git a/plugins/check_swap.c b/plugins/check_swap.c
index da18ccb8..968779dc 100644
--- a/plugins/check_swap.c
+++ b/plugins/check_swap.c
@@ -1,33 +1,58 @@
1/****************************************************************************** 1/******************************************************************************
2* 2 *
3* CHECK_SWAP.C 3 * Program: Swap space plugin for Nagios
4* 4 * License: GPL
5* Program: Process plugin for Nagios 5 *
6* License: GPL 6 * License Information:
7* Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net) 7 *
8* 8 * This program is free software; you can redistribute it and/or modify
9* $Id$ 9 * it under the terms of the GNU General Public License as published by
10* 10 * the Free Software Foundation; either version 2 of the License, or
11******************************************************************************/ 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 *****************************************************************************/
12 27
13#include "common.h" 28#include "common.h"
14#include "popen.h" 29#include "popen.h"
15#include "utils.h" 30#include "utils.h"
16 31
17#define PROGNAME "check_swap" 32#define PROGNAME "check_swap"
33#define REVISION "$Revision$"
34#define COPYRIGHT "2000-2002"
35#define AUTHOR "Karl DeBisschop"
36#define EMAIL "kdebisschop@users.sourceforge.net"
37#define SUMMARY "Check swap space on local server.\n"
18 38
19int process_arguments (int argc, char **argv); 39int process_arguments (int argc, char **argv);
20int validate_arguments (void); 40int validate_arguments (void);
21void print_usage (void); 41void print_usage (void);
22void print_help (void); 42void print_help (void);
23 43
24int warn_percent = 200, crit_percent = 200, warn_size = -1, crit_size = -1; 44int warn_percent = 200;
45int crit_percent = 200;
46int warn_size = -1;
47int crit_size = -1;
48int verbose;
49int allswaps;
25 50
26int 51int
27main (int argc, char **argv) 52main (int argc, char **argv)
28{ 53{
29 int total_swap = 0, used_swap = 0, free_swap = 0, percent_used; 54 int total_swap = 0, used_swap = 0, free_swap = 0, percent_used;
30 int total, used, free; 55 int total, used, free, percent;
31 int result = STATE_OK; 56 int result = STATE_OK;
32 char input_buffer[MAX_INPUT_BUFFER]; 57 char input_buffer[MAX_INPUT_BUFFER];
33#ifdef HAVE_SWAP 58#ifdef HAVE_SWAP
@@ -48,17 +73,25 @@ main (int argc, char **argv)
48 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) { 73 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
49 if (sscanf (input_buffer, " %s %d %d %d", str, &total, &used, &free) == 4 && 74 if (sscanf (input_buffer, " %s %d %d %d", str, &total, &used, &free) == 4 &&
50 strstr (str, "Swap")) { 75 strstr (str, "Swap")) {
51/* asprintf (&status, "%s [%d/%d]", status, used, total); */
52 total_swap += total; 76 total_swap += total;
53 used_swap += used; 77 used_swap += used;
54 free_swap += free; 78 free_swap += free;
79 if (allswaps) {
80 percent = 100 * (((float) used) / ((float) total));
81 if (percent >= crit_percent || free <= crit_size)
82 result = max_state (STATE_CRITICAL, result);
83 else if (percent >= warn_percent || free <= warn_size)
84 result = max_state (STATE_WARNING, result);
85 if (verbose)
86 asprintf (&status, "%s [%d/%d]", status, used, total);
87 }
55 } 88 }
56 } 89 }
57 percent_used = 100 * (((float) used_swap) / ((float) total_swap)); 90 percent_used = 100 * (((float) used_swap) / ((float) total_swap));
58 if (percent_used >= crit_percent || free_swap <= crit_size) 91 if (percent_used >= crit_percent || free_swap <= crit_size)
59 result = STATE_CRITICAL; 92 result = max_state (STATE_CRITICAL, result);
60 else if (percent_used >= warn_percent || free_swap <= warn_size) 93 else if (percent_used >= warn_percent || free_swap <= warn_size)
61 result = STATE_WARNING; 94 result = max_state (STATE_WARNING, result);
62 asprintf (&status, "%s %2d%% (%d out of %d)", status, percent_used, 95 asprintf (&status, "%s %2d%% (%d out of %d)", status, percent_used,
63 used_swap, total_swap); 96 used_swap, total_swap);
64 fclose (fp); 97 fclose (fp);
@@ -94,29 +127,37 @@ main (int argc, char **argv)
94 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { 127 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
95 sscanf (input_buffer, SWAP_FORMAT, &total, &free); 128 sscanf (input_buffer, SWAP_FORMAT, &total, &free);
96 used = total - free; 129 used = total - free;
97/* asprintf (&status, "%s [%d/%d]", status, used, total); */
98 total_swap += total; 130 total_swap += total;
99 used_swap += used; 131 used_swap += used;
100 free_swap += free; 132 free_swap += free;
133 if (allswaps) {
134 percent = 100 * (((float) used) / ((float) total));
135 if (percent >= crit_percent || free <= crit_size)
136 result = max_state (STATE_CRITICAL, result);
137 else if (percent >= warn_percent || free <= warn_size)
138 result = max_state (STATE_WARNING, result);
139 if (verbose)
140 asprintf (&status, "%s [%d/%d]", status, used, total);
141 }
101 } 142 }
102 percent_used = 100 * ((float) used_swap) / ((float) total_swap); 143 percent_used = 100 * ((float) used_swap) / ((float) total_swap);
103 asprintf (&status, "%s %2d%% (%d out of %d)", 144 asprintf (&status, "%s %2d%% (%d out of %d)",
104 status, percent_used, used_swap, total_swap); 145 status, percent_used, used_swap, total_swap);
105 if (percent_used >= crit_percent || free_swap <= crit_size) 146 if (percent_used >= crit_percent || free_swap <= crit_size)
106 result = STATE_CRITICAL; 147 result = max_state (STATE_CRITICAL, result);
107 else if (percent_used >= warn_percent || free_swap <= warn_size) 148 else if (percent_used >= warn_percent || free_swap <= warn_size)
108 result = STATE_WARNING; 149 result = max_state (STATE_WARNING, result);
109 150
110 /* If we get anything on STDERR, at least set warning */ 151 /* If we get anything on STDERR, at least set warning */
111 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) 152 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
112 result = max (result, STATE_WARNING); 153 result = max_state (result, STATE_WARNING);
113 154
114 /* close stderr */ 155 /* close stderr */
115 (void) fclose (child_stderr); 156 (void) fclose (child_stderr);
116 157
117 /* close the pipe */ 158 /* close the pipe */
118 if (spclose (child_process)) 159 if (spclose (child_process))
119 result = max (result, STATE_WARNING); 160 result = max_state (result, STATE_WARNING);
120#endif 161#endif
121#endif 162#endif
122 163
@@ -152,13 +193,16 @@ main (int argc, char **argv)
152int 193int
153process_arguments (int argc, char **argv) 194process_arguments (int argc, char **argv)
154{ 195{
155 int c, i = 0; 196 int c = 0; /* option character */
197 int wc = 0; /* warning counter */
198 int cc = 0; /* critical counter */
156 199
157#ifdef HAVE_GETOPT_H 200#ifdef HAVE_GETOPT_H
158 int option_index = 0; 201 int option_index = 0;
159 static struct option long_options[] = { 202 static struct option long_options[] = {
160 {"warning", required_argument, 0, 'w'}, 203 {"warning", required_argument, 0, 'w'},
161 {"critical", required_argument, 0, 'c'}, 204 {"critical", required_argument, 0, 'c'},
205 {"all", no_argument, 0, 'a'},
162 {"verbose", no_argument, 0, 'v'}, 206 {"verbose", no_argument, 0, 'v'},
163 {"version", no_argument, 0, 'V'}, 207 {"version", no_argument, 0, 'V'},
164 {"help", no_argument, 0, 'h'}, 208 {"help", no_argument, 0, 'h'},
@@ -171,9 +215,9 @@ process_arguments (int argc, char **argv)
171 215
172 while (1) { 216 while (1) {
173#ifdef HAVE_GETOPT_H 217#ifdef HAVE_GETOPT_H
174 c = getopt_long (argc, argv, "+?Vhc:w:", long_options, &option_index); 218 c = getopt_long (argc, argv, "+?Vvhac:w:", long_options, &option_index);
175#else 219#else
176 c = getopt (argc, argv, "+?Vhc:w:"); 220 c = getopt (argc, argv, "+?Vvhac:w:");
177#endif 221#endif
178 222
179 if (c == -1 || c == EOF) 223 if (c == -1 || c == EOF)
@@ -197,6 +241,7 @@ process_arguments (int argc, char **argv)
197 else { 241 else {
198 usage ("Warning threshold must be integer or percentage!\n"); 242 usage ("Warning threshold must be integer or percentage!\n");
199 } 243 }
244 wc++;
200 case 'c': /* critical time threshold */ 245 case 'c': /* critical time threshold */
201 if (is_intnonneg (optarg)) { 246 if (is_intnonneg (optarg)) {
202 crit_size = atoi (optarg); 247 crit_size = atoi (optarg);
@@ -214,6 +259,13 @@ process_arguments (int argc, char **argv)
214 else { 259 else {
215 usage ("Critical threshold must be integer or percentage!\n"); 260 usage ("Critical threshold must be integer or percentage!\n");
216 } 261 }
262 cc++;
263 case 'a': /* verbose */
264 allswaps = TRUE;
265 break;
266 case 'v': /* verbose */
267 verbose = TRUE;
268 break;
217 case 'V': /* version */ 269 case 'V': /* version */
218 print_revision (my_basename (argv[0]), "$Revision$"); 270 print_revision (my_basename (argv[0]), "$Revision$");
219 exit (STATE_OK); 271 exit (STATE_OK);
@@ -279,9 +331,12 @@ void
279print_usage (void) 331print_usage (void)
280{ 332{
281 printf 333 printf
282 ("Usage: check_swap -w <used_percentage>%% -c <used_percentage>%%\n" 334 ("Usage:\n"
283 " check_swap -w <bytes_free> -c <bytes_free>\n" 335 " %s [-a] -w <used_percentage>%% -c <used_percentage>%%\n"
284 " check_swap (-V|--version)\n" " check_swap (-h|--help)\n"); 336 " %s [-a] -w <bytes_free> -c <bytes_free>\n"
337 " %s (-h | --help) for detailed help\n"
338 " %s (-V | --version) for version information\n",
339 PROGNAME, PROGNAME, PROGNAME, PROGNAME);
285} 340}
286 341
287 342
@@ -291,11 +346,9 @@ print_usage (void)
291void 346void
292print_help (void) 347print_help (void)
293{ 348{
294 print_revision (PROGNAME, "$Revision$"); 349 print_revision (PROGNAME, REVISION);
295 printf 350 printf
296 ("Copyright (c) 2000 Karl DeBisschop\n\n" 351 ("Copyright (c) %s %s <%s>\n\n%s\n", COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
297 "This plugin will check all of the swap partitions and return an\n"
298 "error if the the avalable swap space is less than specified.\n\n");
299 print_usage (); 352 print_usage ();
300 printf 353 printf
301 ("\nOptions:\n" 354 ("\nOptions:\n"
@@ -307,6 +360,8 @@ print_help (void)
307 " Exit with CRITICAL status if less than INTEGER bytes of swap space are free\n" 360 " Exit with CRITICAL status if less than INTEGER bytes of swap space are free\n"
308 " -c, --critical=PERCENT%%\n" 361 " -c, --critical=PERCENT%%\n"
309 " Exit with CRITCAL status if more than PERCENT of swap space has been used\n" 362 " Exit with CRITCAL status if more than PERCENT of swap space has been used\n"
363 " -a, --allswaps\n"
364 " Conduct comparisons for all swap partitions, one by one\n"
310 " -h, --help\n" 365 " -h, --help\n"
311 " Print detailed help screen\n" 366 " Print detailed help screen\n"
312 " -V, --version\n" " Print version information\n\n"); 367 " -V, --version\n" " Print version information\n\n");