summaryrefslogtreecommitdiffstats
path: root/plugins/check_swap.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_swap.c')
-rw-r--r--plugins/check_swap.c163
1 files changed, 82 insertions, 81 deletions
diff --git a/plugins/check_swap.c b/plugins/check_swap.c
index 96eacf3..5edb806 100644
--- a/plugins/check_swap.c
+++ b/plugins/check_swap.c
@@ -30,12 +30,11 @@
30#include "utils.h" 30#include "utils.h"
31 31
32const char *progname = "check_swap"; 32const char *progname = "check_swap";
33#define REVISION "$Revision$" 33const char *revision = "$Revision$";
34#define COPYRIGHT "2000-2002" 34const char *copyright = "2000-2003";
35#define AUTHOR "Karl DeBisschop" 35const char *email = "nagiosplug-devel@lists.sourceforge.net";
36#define EMAIL "kdebisschop@users.sourceforge.net"
37#define SUMMARY "Check swap space on local server.\n"
38 36
37int check_swap (int usp, int free_swap);
39int process_arguments (int argc, char **argv); 38int process_arguments (int argc, char **argv);
40int validate_arguments (void); 39int validate_arguments (void);
41void print_usage (void); 40void print_usage (void);
@@ -52,16 +51,68 @@ int allswaps;
52int sun = 0; /* defined by compiler if it is a sun solaris system */ 51int sun = 0; /* defined by compiler if it is a sun solaris system */
53#endif 52#endif
54 53
54void
55print_usage (void)
56{
57 printf (_("Usage:\n\
58 %s [-a] -w <used_percentage>%% -c <used_percentage>%%\n\
59 %s [-a] -w <bytes_free> -c <bytes_free>\n\
60 %s (-h | --help) for detailed help\n\
61 %s (-V | --version) for version information\n"),
62 progname, progname, progname, progname);
63}
64
65
66
67
68
69void
70print_help (void)
71{
72 print_revision (progname, revision);
73
74 printf (_(COPYRIGHT), copyright, email);
75
76 printf (_("Check swap space on local server.\n\n"));
77
78 print_usage ();
79
80 printf (_(HELP_VRSN));
81
82 printf (_("\n\
83 -w, --warning=INTEGER\n\
84 Exit with WARNING status if less than INTEGER bytes of swap space are free\n\
85 -w, --warning=PERCENT%%\n\
86 Exit with WARNING status if less than PERCENT of swap space has been used\n\
87 -c, --critical=INTEGER\n\
88 Exit with CRITICAL status if less than INTEGER bytes of swap space are free\n\
89 -c, --critical=PERCENT%%\n\
90 Exit with CRITCAL status if less than PERCENT of swap space has been used\n\
91 -a, --allswaps\n\
92 Conduct comparisons for all swap partitions, one by one\n"));
93
94#ifdef sun
95 printf (_("\n\
96On Solaris, if -a specified, uses swap -l, otherwise uses swap -s.\n\
97Will be discrepencies because swap -s counts allocated swap and includes\n\
98real memory\n"));
99#endif
100
101 support ();
102}
103
104
105
55int 106int
56main (int argc, char **argv) 107main (int argc, char **argv)
57{ 108{
58 int percent_used, percent; 109 int percent_used, percent;
59 long unsigned int total_swap = 0, used_swap = 0, free_swap = 0; 110 long unsigned int total_swap = 0, used_swap = 0, free_swap = 0;
60 long unsigned int total, used, free; 111 long unsigned int dsktotal, dskused, dskfree;
61 int conv_factor; /* Convert to MBs */
62 int result = STATE_OK; 112 int result = STATE_OK;
63 char input_buffer[MAX_INPUT_BUFFER]; 113 char input_buffer[MAX_INPUT_BUFFER];
64#ifdef HAVE_SWAP 114#ifdef HAVE_SWAP
115 int conv_factor; /* Convert to MBs */
65 char *temp_buffer; 116 char *temp_buffer;
66 char *swap_command; 117 char *swap_command;
67 char *swap_format; 118 char *swap_format;
@@ -78,11 +129,11 @@ main (int argc, char **argv)
78#ifdef HAVE_PROC_MEMINFO 129#ifdef HAVE_PROC_MEMINFO
79 fp = fopen (PROC_MEMINFO, "r"); 130 fp = fopen (PROC_MEMINFO, "r");
80 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) { 131 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
81 if (sscanf (input_buffer, " %s %lu %lu %lu", str, &total, &used, &free) == 4 && 132 if (sscanf (input_buffer, " %s %lu %lu %lu", str, &dsktotal, &dskused, &dskfree) == 4 &&
82 strstr (str, "Swap")) { 133 strstr (str, "Swap")) {
83 total = total / 1048576; 134 dsktotal = dsktotal / 1048576;
84 used = used / 1048576; 135 dskused = dskused / 1048576;
85 free = free / 1048576; 136 dskfree = dskfree / 1048576;
86#endif 137#endif
87#ifdef HAVE_SWAP 138#ifdef HAVE_SWAP
88 if (!allswaps && sun) { 139 if (!allswaps && sun) {
@@ -118,7 +169,7 @@ main (int argc, char **argv)
118 while (temp_buffer) { 169 while (temp_buffer) {
119 if (strstr (temp_buffer, "blocks")) 170 if (strstr (temp_buffer, "blocks"))
120 sprintf (str, "%s %s", str, "%f"); 171 sprintf (str, "%s %s", str, "%f");
121 else if (strstr (temp_buffer, "free")) 172 else if (strstr (temp_buffer, "dskfree"))
122 sprintf (str, "%s %s", str, "%f"); 173 sprintf (str, "%s %s", str, "%f");
123 else 174 else
124 sprintf (str, "%s %s", str, "%*s"); 175 sprintf (str, "%s %s", str, "%*s");
@@ -133,23 +184,23 @@ main (int argc, char **argv)
133 total_swap = used_swap + free_swap; 184 total_swap = used_swap + free_swap;
134 } else { 185 } else {
135 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { 186 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
136 sscanf (input_buffer, swap_format, &total, &free); 187 sscanf (input_buffer, swap_format, &dsktotal, &dskfree);
137 188
138 total = total / conv_factor; 189 dsktotal = dsktotal / conv_factor;
139 free = free / conv_factor; 190 dskfree = dskfree / conv_factor;
140 if (verbose >= 3) 191 if (verbose >= 3)
141 printf ("total=%d, free=%d\n", total, free); 192 printf ("total=%d, free=%d\n", dsktotal, dskfree);
142 193
143 used = total - free; 194 dskused = dsktotal - dskfree;
144#endif 195#endif
145 total_swap += total; 196 total_swap += dsktotal;
146 used_swap += used; 197 used_swap += dskused;
147 free_swap += free; 198 free_swap += dskfree;
148 if (allswaps) { 199 if (allswaps) {
149 percent = 100 * (((double) used) / ((double) total)); 200 percent = 100 * (((double) dskused) / ((double) dsktotal));
150 result = max_state (result, check_swap (percent, free)); 201 result = max_state (result, check_swap (percent, dskfree));
151 if (verbose) 202 if (verbose)
152 asprintf (&status, "%s [%lu (%d%%)]", status, free, 100 - percent); 203 asprintf (&status, "%s [%lu (%d%%)]", status, dskfree, 100 - percent);
153 } 204 }
154 } 205 }
155 } 206 }
@@ -175,6 +226,7 @@ main (int argc, char **argv)
175#endif 226#endif
176 227
177 terminate (result, "SWAP %s:%s\n", state_text (result), status); 228 terminate (result, "SWAP %s:%s\n", state_text (result), status);
229 return STATE_UNKNOWN;
178} 230}
179 231
180 232
@@ -186,11 +238,11 @@ check_swap (int usp, int free_swap)
186 int result = STATE_UNKNOWN; 238 int result = STATE_UNKNOWN;
187 if (usp >= 0 && usp >= (100.0 - crit_percent)) 239 if (usp >= 0 && usp >= (100.0 - crit_percent))
188 result = STATE_CRITICAL; 240 result = STATE_CRITICAL;
189 else if (crit_size >= 0 && free_swap <= crit_size) 241 else if (crit_size > 0 && (unsigned)free_swap <= crit_size)
190 result = STATE_CRITICAL; 242 result = STATE_CRITICAL;
191 else if (usp >= 0 && usp >= (100.0 - warn_percent)) 243 else if (usp >= 0 && usp >= (100.0 - warn_percent))
192 result = STATE_WARNING; 244 result = STATE_WARNING;
193 else if (warn_size >= 0 && free_swap <= warn_size) 245 else if (warn_size > 0 && (unsigned)free_swap <= warn_size)
194 result = STATE_WARNING; 246 result = STATE_WARNING;
195 else if (usp >= 0.0) 247 else if (usp >= 0.0)
196 result = STATE_OK; 248 result = STATE_OK;
@@ -270,7 +322,7 @@ process_arguments (int argc, char **argv)
270 verbose++; 322 verbose++;
271 break; 323 break;
272 case 'V': /* version */ 324 case 'V': /* version */
273 print_revision (progname, "$Revision$"); 325 print_revision (progname, revision);
274 exit (STATE_OK); 326 exit (STATE_OK);
275 case 'h': /* help */ 327 case 'h': /* help */
276 print_help (); 328 print_help ();
@@ -293,12 +345,12 @@ process_arguments (int argc, char **argv)
293 345
294 if (c == argc) 346 if (c == argc)
295 return validate_arguments (); 347 return validate_arguments ();
296 if (warn_size < 0 && is_intnonneg (argv[c])) 348 if (warn_size == 0 && is_intnonneg (argv[c]))
297 warn_size = atoi (argv[c++]); 349 warn_size = atoi (argv[c++]);
298 350
299 if (c == argc) 351 if (c == argc)
300 return validate_arguments (); 352 return validate_arguments ();
301 if (crit_size < 0 && is_intnonneg (argv[c])) 353 if (crit_size == 0 && is_intnonneg (argv[c]))
302 crit_size = atoi (argv[c++]); 354 crit_size = atoi (argv[c++]);
303 355
304 return validate_arguments (); 356 return validate_arguments ();
@@ -311,8 +363,8 @@ process_arguments (int argc, char **argv)
311int 363int
312validate_arguments (void) 364validate_arguments (void)
313{ 365{
314 if (warn_percent > 100 && crit_percent > 100 && warn_size < 0 366 if (warn_percent > 100 && crit_percent > 100 && warn_size == 0
315 && crit_size < 0) { 367 && crit_size == 0) {
316 return ERROR; 368 return ERROR;
317 } 369 }
318 else if (warn_percent < crit_percent) { 370 else if (warn_percent < crit_percent) {
@@ -325,54 +377,3 @@ validate_arguments (void)
325 } 377 }
326 return OK; 378 return OK;
327} 379}
328
329
330
331
332
333void
334print_usage (void)
335{
336 printf
337 ("Usage:\n"
338 " %s [-a] -w <used_percentage>%% -c <used_percentage>%%\n"
339 " %s [-a] -w <bytes_free> -c <bytes_free>\n"
340 " %s (-h | --help) for detailed help\n"
341 " %s (-V | --version) for version information\n",
342 progname, progname, progname, progname);
343}
344
345
346
347
348
349void
350print_help (void)
351{
352 print_revision (progname, REVISION);
353 printf
354 ("Copyright (c) %s %s <%s>\n\n%s\n", COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
355 print_usage ();
356 printf
357 ("\nOptions:\n"
358 " -w, --warning=INTEGER\n"
359 " Exit with WARNING status if less than INTEGER bytes of swap space are free\n"
360 " -w, --warning=PERCENT%%\n"
361 " Exit with WARNING status if less than PERCENT of swap space has been used\n"
362 " -c, --critical=INTEGER\n"
363 " Exit with CRITICAL status if less than INTEGER bytes of swap space are free\n"
364 " -c, --critical=PERCENT%%\n"
365 " Exit with CRITCAL status if less than PERCENT of swap space has been used\n"
366 " -a, --allswaps\n"
367 " Conduct comparisons for all swap partitions, one by one\n"
368 " -h, --help\n"
369 " Print detailed help screen\n"
370 " -V, --version\n" " Print version information\n"
371#ifdef sun
372 "\nOn Solaris, if -a specified, uses swap -l, otherwise uses swap -s.\n"
373 "Will be discrepencies because swap -s counts allocated swap and includes real memory\n"
374#endif
375 "\n"
376 );
377 support ();
378}