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.c181
1 files changed, 94 insertions, 87 deletions
diff --git a/plugins/check_swap.c b/plugins/check_swap.c
index 74b9daf6..21a3bc8b 100644
--- a/plugins/check_swap.c
+++ b/plugins/check_swap.c
@@ -34,7 +34,7 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net";
34#include "popen.h" 34#include "popen.h"
35#include "utils.h" 35#include "utils.h"
36 36
37int check_swap (int usp, float free_swap); 37int check_swap (int usp, float free_swap_mb);
38int process_arguments (int argc, char **argv); 38int process_arguments (int argc, char **argv);
39int validate_arguments (void); 39int validate_arguments (void);
40void print_usage (void); 40void print_usage (void);
@@ -42,8 +42,8 @@ void print_help (void);
42 42
43int warn_percent = 0; 43int warn_percent = 0;
44int crit_percent = 0; 44int crit_percent = 0;
45double warn_size = 0; 45float warn_size_bytes = 0;
46double crit_size = 0; 46float crit_size_bytes= 0;
47int verbose; 47int verbose;
48int allswaps; 48int allswaps;
49 49
@@ -51,8 +51,8 @@ int
51main (int argc, char **argv) 51main (int argc, char **argv)
52{ 52{
53 int percent_used, percent; 53 int percent_used, percent;
54 float total_swap = 0, used_swap = 0, free_swap = 0; 54 float total_swap_mb = 0, used_swap_mb = 0, free_swap_mb = 0;
55 float dsktotal = 0, dskused = 0, dskfree = 0, tmp = 0; 55 float dsktotal_mb = 0, dskused_mb = 0, dskfree_mb = 0, tmp_mb = 0;
56 int result = STATE_UNKNOWN; 56 int result = STATE_UNKNOWN;
57 char input_buffer[MAX_INPUT_BUFFER]; 57 char input_buffer[MAX_INPUT_BUFFER];
58#ifdef HAVE_PROC_MEMINFO 58#ifdef HAVE_PROC_MEMINFO
@@ -90,39 +90,46 @@ main (int argc, char **argv)
90 usage4 (_("Could not parse arguments")); 90 usage4 (_("Could not parse arguments"));
91 91
92#ifdef HAVE_PROC_MEMINFO 92#ifdef HAVE_PROC_MEMINFO
93 if (verbose >= 3) {
94 printf("Reading PROC_MEMINFO at %s\n", PROC_MEMINFO);
95 }
93 fp = fopen (PROC_MEMINFO, "r"); 96 fp = fopen (PROC_MEMINFO, "r");
94 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) { 97 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
95 if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %f %f %f", &dsktotal, &dskused, &dskfree) == 3) { 98 if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %f %f %f", &dsktotal_mb, &dskused_mb, &dskfree_mb) == 3) {
96 dsktotal = dsktotal / 1048576; 99 dsktotal_mb = dsktotal_mb / 1048576; /* Apply conversion */
97 dskused = dskused / 1048576; 100 dskused_mb = dskused_mb / 1048576;
98 dskfree = dskfree / 1048576; 101 dskfree_mb = dskfree_mb / 1048576;
99 total_swap += dsktotal; 102 total_swap_mb += dsktotal_mb;
100 used_swap += dskused; 103 used_swap_mb += dskused_mb;
101 free_swap += dskfree; 104 free_swap_mb += dskfree_mb;
102 if (allswaps) { 105 if (allswaps) {
103 if (dsktotal == 0) 106 if (dsktotal_mb == 0)
104 percent=100.0; 107 percent=100.0;
105 else 108 else
106 percent = 100 * (((double) dskused) / ((double) dsktotal)); 109 percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb));
107 result = max_state (result, check_swap (percent, dskfree)); 110 result = max_state (result, check_swap (percent, dskfree_mb));
108 if (verbose) 111 if (verbose)
109 asprintf (&status, "%s [%.0f (%d%%)]", status, dskfree, 100 - percent); 112 asprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent);
110 } 113 }
111 } 114 }
112 else if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%[TotalFre]%*[:] %f %*[k]%*[B]", str, &tmp)) { 115 else if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%[TotalFre]%*[:] %f %*[k]%*[B]", str, &tmp_mb)) {
116 if (verbose >= 3) {
117 printf("Got %s with %f\n", str, tmp_mb);
118 }
119 /* I think this part is always in Kb, so convert to mb */
113 if (strcmp ("Total", str) == 0) { 120 if (strcmp ("Total", str) == 0) {
114 dsktotal = tmp / 1024; 121 dsktotal_mb = tmp_mb / 1024;
115 } 122 }
116 else if (strcmp ("Free", str) == 0) { 123 else if (strcmp ("Free", str) == 0) {
117 dskfree = tmp / 1024; 124 dskfree_mb = tmp_mb / 1024;
118 } 125 }
119 } 126 }
120 } 127 }
121 fclose(fp); 128 fclose(fp);
122 dskused = dsktotal - dskfree; 129 dskused_mb = dsktotal_mb - dskfree_mb;
123 total_swap = dsktotal; 130 total_swap_mb = dsktotal_mb;
124 used_swap = dskused; 131 used_swap_mb = dskused_mb;
125 free_swap = dskfree; 132 free_swap_mb = dskfree_mb;
126#else 133#else
127# ifdef HAVE_SWAP 134# ifdef HAVE_SWAP
128 asprintf(&swap_command, "%s", SWAP_COMMAND); 135 asprintf(&swap_command, "%s", SWAP_COMMAND);
@@ -173,35 +180,35 @@ main (int argc, char **argv)
173# ifdef _AIX 180# ifdef _AIX
174 if (!allswaps) { 181 if (!allswaps) {
175 fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process); /* Ignore first line */ 182 fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process); /* Ignore first line */
176 sscanf (input_buffer, swap_format, &total_swap, &used_swap); 183 sscanf (input_buffer, swap_format, &total_swap_mb, &used_swap_mb);
177 free_swap = total_swap * (100 - used_swap) /100; 184 free_swap_mb = total_swap_mb * (100 - used_swap_mb) /100;
178 used_swap = total_swap - free_swap; 185 used_swap_mb = total_swap_mb - free_swap_mb;
179 if (verbose >= 3) 186 if (verbose >= 3)
180 printf (_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap, used_swap, free_swap); 187 printf (_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap_mb, used_swap_mb, free_swap_mb);
181 } else { 188 } else {
182# endif 189# endif
183 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { 190 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
184 sscanf (input_buffer, swap_format, &dsktotal, &dskfree); 191 sscanf (input_buffer, swap_format, &dsktotal_mb, &dskfree_mb);
185 192
186 dsktotal = dsktotal / conv_factor; 193 dsktotal_mb = dsktotal_mb / conv_factor;
187 /* AIX lists percent used, so this converts to dskfree in MBs */ 194 /* AIX lists percent used, so this converts to dskfree in MBs */
188# ifdef _AIX 195# ifdef _AIX
189 dskfree = dsktotal * (100 - dskfree) / 100; 196 dskfree_mb = dsktotal_mb * (100 - dskfree_mb) / 100;
190# else 197# else
191 dskfree = dskfree / conv_factor; 198 dskfree_mb = dskfree_mb / conv_factor;
192# endif 199# endif
193 if (verbose >= 3) 200 if (verbose >= 3)
194 printf (_("total=%.0f, free=%.0f\n"), dsktotal, dskfree); 201 printf (_("total=%.0f, free=%.0f\n"), dsktotal_mb, dskfree_mb);
195 202
196 dskused = dsktotal - dskfree; 203 dskused_mb = dsktotal_mb - dskfree_mb;
197 total_swap += dsktotal; 204 total_swap_mb += dsktotal_mb;
198 used_swap += dskused; 205 used_swap_mb += dskused_mb;
199 free_swap += dskfree; 206 free_swap_mb += dskfree_mb;
200 if (allswaps) { 207 if (allswaps) {
201 percent = 100 * (((double) dskused) / ((double) dsktotal)); 208 percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb));
202 result = max_state (result, check_swap (percent, dskfree)); 209 result = max_state (result, check_swap (percent, dskfree_mb));
203 if (verbose) 210 if (verbose)
204 asprintf (&status, "%s [%.0f (%d%%)]", status, dskfree, 100 - percent); 211 asprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent);
205 } 212 }
206 } 213 }
207# ifdef _AIX 214# ifdef _AIX
@@ -241,24 +248,24 @@ main (int argc, char **argv)
241 } 248 }
242 249
243 for(i=0;i<nswaps;i++){ 250 for(i=0;i<nswaps;i++){
244 dsktotal = (float) tbl->swt_ent[i].ste_pages / SWAP_CONVERSION; 251 dsktotal_mb = (float) tbl->swt_ent[i].ste_pages / SWAP_CONVERSION;
245 dskfree = (float) tbl->swt_ent[i].ste_free / SWAP_CONVERSION; 252 dskfree_mb = (float) tbl->swt_ent[i].ste_free / SWAP_CONVERSION;
246 dskused = ( dsktotal - dskfree ); 253 dskused_mb = ( dsktotal_mb - dskfree_mb );
247 254
248 if (verbose >= 3) 255 if (verbose >= 3)
249 printf ("dsktotal=%.0f dskfree=%.0f dskused=%.0f\n", dsktotal, dskfree, dskused); 256 printf ("dsktotal_mb=%.0f dskfree_mb=%.0f dskused_mb=%.0f\n", dsktotal_mb, dskfree_mb, dskused_mb);
250 257
251 if(allswaps && dsktotal > 0){ 258 if(allswaps && dsktotal_mb > 0){
252 percent = 100 * (((double) dskused) / ((double) dsktotal)); 259 percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb));
253 result = max_state (result, check_swap (percent, dskfree)); 260 result = max_state (result, check_swap (percent, dskfree_mb));
254 if (verbose) { 261 if (verbose) {
255 asprintf (&status, "%s [%.0f (%d%%)]", status, dskfree, 100 - percent); 262 asprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent);
256 } 263 }
257 } 264 }
258 265
259 total_swap += dsktotal; 266 total_swap_mb += dsktotal_mb;
260 free_swap += dskfree; 267 free_swap_mb += dskfree_mb;
261 used_swap += dskused; 268 used_swap_mb += dskused_mb;
262 } 269 }
263 270
264 /* and clean up after ourselves */ 271 /* and clean up after ourselves */
@@ -283,21 +290,21 @@ main (int argc, char **argv)
283 } 290 }
284 291
285 for(i=0;i<nswaps;i++){ 292 for(i=0;i<nswaps;i++){
286 dsktotal = (float) ent->se_nblks / conv_factor; 293 dsktotal_mb = (float) ent->se_nblks / conv_factor;
287 dskused = (float) ent->se_inuse / conv_factor; 294 dskused_mb = (float) ent->se_inuse / conv_factor;
288 dskfree = ( dsktotal - dskused ); 295 dskfree_mb = ( dsktotal_mb - dskused_mb );
289 296
290 if(allswaps && dsktotal > 0){ 297 if(allswaps && dsktotal_mb > 0){
291 percent = 100 * (((double) dskused) / ((double) dsktotal)); 298 percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb));
292 result = max_state (result, check_swap (percent, dskfree)); 299 result = max_state (result, check_swap (percent, dskfree_mb));
293 if (verbose) { 300 if (verbose) {
294 asprintf (&status, "%s [%.0f (%d%%)]", status, dskfree, 100 - percent); 301 asprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent);
295 } 302 }
296 } 303 }
297 304
298 total_swap += dsktotal; 305 total_swap_mb += dsktotal_mb;
299 free_swap += dskfree; 306 free_swap_mb += dskfree_mb;
300 used_swap += dskused; 307 used_swap_mb += dskused_mb;
301 } 308 }
302 309
303 /* and clean up after ourselves */ 310 /* and clean up after ourselves */
@@ -308,23 +315,23 @@ main (int argc, char **argv)
308# endif /* HAVE_SWAP */ 315# endif /* HAVE_SWAP */
309#endif /* HAVE_PROC_MEMINFO */ 316#endif /* HAVE_PROC_MEMINFO */
310 317
311 /* if total_swap == 0, let's not divide by 0 */ 318 /* if total_swap_mb == 0, let's not divide by 0 */
312 if(total_swap) { 319 if(total_swap_mb) {
313 percent_used = 100 * ((double) used_swap) / ((double) total_swap); 320 percent_used = 100 * ((double) used_swap_mb) / ((double) total_swap_mb);
314 } else { 321 } else {
315 percent_used = 0; 322 percent_used = 0;
316 } 323 }
317 324
318 result = max_state (result, check_swap (percent_used, free_swap)); 325 result = max_state (result, check_swap (percent_used, free_swap_mb));
319 printf (_("SWAP %s - %d%% free (%.0f MB out of %.0f MB) %s|"), 326 printf (_("SWAP %s - %d%% free (%d MB out of %d MB) %s|"),
320 state_text (result), 327 state_text (result),
321 (100 - percent_used), free_swap, total_swap, status); 328 (100 - percent_used), (int) free_swap_mb, (int) total_swap_mb, status);
322 329
323 puts (perfdata ("swap", (long) free_swap, "MB", 330 puts (perfdata ("swap", (long) free_swap_mb, "MB",
324 TRUE, (long) max (warn_size/1024, warn_percent/100.0*total_swap), 331 TRUE, (long) max (warn_size_bytes/(1024 * 1024), warn_percent/100.0*total_swap_mb),
325 TRUE, (long) max (crit_size/1024, crit_percent/100.0*total_swap), 332 TRUE, (long) max (crit_size_bytes/(1024 * 1024), crit_percent/100.0*total_swap_mb),
326 TRUE, 0, 333 TRUE, 0,
327 TRUE, (long) total_swap)); 334 TRUE, (long) total_swap_mb));
328 335
329 return result; 336 return result;
330} 337}
@@ -332,17 +339,17 @@ main (int argc, char **argv)
332 339
333 340
334int 341int
335check_swap (int usp, float free_swap) 342check_swap (int usp, float free_swap_mb)
336{ 343{
337 int result = STATE_UNKNOWN; 344 int result = STATE_UNKNOWN;
338 free_swap = free_swap * 1024; /* Convert back to bytes as warn and crit specified in bytes */ 345 float free_swap = free_swap_mb * (1024 * 1024); /* Convert back to bytes as warn and crit specified in bytes */
339 if (usp >= 0 && crit_percent != 0 && usp >= (100.0 - crit_percent)) 346 if (usp >= 0 && crit_percent != 0 && usp >= (100.0 - crit_percent))
340 result = STATE_CRITICAL; 347 result = STATE_CRITICAL;
341 else if (crit_size > 0 && free_swap <= crit_size) 348 else if (crit_size_bytes > 0 && free_swap <= crit_size_bytes)
342 result = STATE_CRITICAL; 349 result = STATE_CRITICAL;
343 else if (usp >= 0 && warn_percent != 0 && usp >= (100.0 - warn_percent)) 350 else if (usp >= 0 && warn_percent != 0 && usp >= (100.0 - warn_percent))
344 result = STATE_WARNING; 351 result = STATE_WARNING;
345 else if (warn_size > 0 && free_swap <= warn_size) 352 else if (warn_size_bytes > 0 && free_swap <= warn_size_bytes)
346 result = STATE_WARNING; 353 result = STATE_WARNING;
347 else if (usp >= 0.0) 354 else if (usp >= 0.0)
348 result = STATE_OK; 355 result = STATE_OK;
@@ -380,13 +387,13 @@ process_arguments (int argc, char **argv)
380 switch (c) { 387 switch (c) {
381 case 'w': /* warning size threshold */ 388 case 'w': /* warning size threshold */
382 if (is_intnonneg (optarg)) { 389 if (is_intnonneg (optarg)) {
383 warn_size = (double) atoi (optarg); 390 warn_size_bytes = (float) atoi (optarg);
384 break; 391 break;
385 } 392 }
386 else if (strstr (optarg, ",") && 393 else if (strstr (optarg, ",") &&
387 strstr (optarg, "%") && 394 strstr (optarg, "%") &&
388 sscanf (optarg, "%lf,%d%%", &warn_size, &warn_percent) == 2) { 395 sscanf (optarg, "%f,%d%%", &warn_size_bytes, &warn_percent) == 2) {
389 warn_size = floor(warn_size); 396 warn_size_bytes = floorf(warn_size_bytes);
390 break; 397 break;
391 } 398 }
392 else if (strstr (optarg, "%") && 399 else if (strstr (optarg, "%") &&
@@ -398,13 +405,13 @@ process_arguments (int argc, char **argv)
398 } 405 }
399 case 'c': /* critical size threshold */ 406 case 'c': /* critical size threshold */
400 if (is_intnonneg (optarg)) { 407 if (is_intnonneg (optarg)) {
401 crit_size = (double) atoi (optarg); 408 crit_size_bytes = (float) atoi (optarg);
402 break; 409 break;
403 } 410 }
404 else if (strstr (optarg, ",") && 411 else if (strstr (optarg, ",") &&
405 strstr (optarg, "%") && 412 strstr (optarg, "%") &&
406 sscanf (optarg, "%lf,%d%%", &crit_size, &crit_percent) == 2) { 413 sscanf (optarg, "%f,%d%%", &crit_size_bytes, &crit_percent) == 2) {
407 crit_size = floor(crit_size); 414 crit_size_bytes = floorf(crit_size_bytes);
408 break; 415 break;
409 } 416 }
410 else if (strstr (optarg, "%") && 417 else if (strstr (optarg, "%") &&
@@ -444,13 +451,13 @@ process_arguments (int argc, char **argv)
444 451
445 if (c == argc) 452 if (c == argc)
446 return validate_arguments (); 453 return validate_arguments ();
447 if (warn_size == 0 && is_intnonneg (argv[c])) 454 if (warn_size_bytes == 0 && is_intnonneg (argv[c]))
448 warn_size = (double) atoi (argv[c++]); 455 warn_size_bytes = (float) atoi (argv[c++]);
449 456
450 if (c == argc) 457 if (c == argc)
451 return validate_arguments (); 458 return validate_arguments ();
452 if (crit_size == 0 && is_intnonneg (argv[c])) 459 if (crit_size_bytes == 0 && is_intnonneg (argv[c]))
453 crit_size = (double) atoi (argv[c++]); 460 crit_size_bytes = (float) atoi (argv[c++]);
454 461
455 return validate_arguments (); 462 return validate_arguments ();
456} 463}
@@ -460,15 +467,15 @@ process_arguments (int argc, char **argv)
460int 467int
461validate_arguments (void) 468validate_arguments (void)
462{ 469{
463 if (warn_percent == 0 && crit_percent == 0 && warn_size == 0 470 if (warn_percent == 0 && crit_percent == 0 && warn_size_bytes == 0
464 && crit_size == 0) { 471 && crit_size_bytes == 0) {
465 return ERROR; 472 return ERROR;
466 } 473 }
467 else if (warn_percent < crit_percent) { 474 else if (warn_percent < crit_percent) {
468 usage4 475 usage4
469 (_("Warning percentage should be more than critical percentage")); 476 (_("Warning percentage should be more than critical percentage"));
470 } 477 }
471 else if (warn_size < crit_size) { 478 else if (warn_size_bytes < crit_size_bytes) {
472 usage4 479 usage4
473 (_("Warning free space should be more than critical free space")); 480 (_("Warning free space should be more than critical free space"));
474 } 481 }