summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'plugins')
-rw-r--r--plugins/check_swap.c239
1 files changed, 137 insertions, 102 deletions
diff --git a/plugins/check_swap.c b/plugins/check_swap.c
index 0ff0c770..25bcb3d5 100644
--- a/plugins/check_swap.c
+++ b/plugins/check_swap.c
@@ -34,6 +34,10 @@ const char *email = "devel@monitoring-plugins.org";
34#include "common.h" 34#include "common.h"
35#include "popen.h" 35#include "popen.h"
36#include "utils.h" 36#include "utils.h"
37#include <string.h>
38#include <math.h>
39#include <libintl.h>
40#include <stdbool.h>
37 41
38#ifdef HAVE_DECL_SWAPCTL 42#ifdef HAVE_DECL_SWAPCTL
39# ifdef HAVE_SYS_PARAM_H 43# ifdef HAVE_SYS_PARAM_H
@@ -51,26 +55,30 @@ const char *email = "devel@monitoring-plugins.org";
51# define SWAP_CONVERSION 1 55# define SWAP_CONVERSION 1
52#endif 56#endif
53 57
54int check_swap (int usp, float free_swap_mb, float total_swap_mb); 58typedef struct {
59 bool is_percentage;
60 uint64_t value;
61} threshold_t;
62
63int check_swap (float free_swap_mb, float total_swap_mb);
55int process_arguments (int argc, char **argv); 64int process_arguments (int argc, char **argv);
56int validate_arguments (void); 65int validate_arguments (void);
57void print_usage (void); 66void print_usage (void);
58void print_help (void); 67void print_help (void);
59 68
60int warn_percent = 0; 69threshold_t warn;
61int crit_percent = 0; 70threshold_t crit;
62float warn_size_bytes = 0;
63float crit_size_bytes = 0;
64int verbose; 71int verbose;
65int allswaps; 72bool allswaps;
66int no_swap_state = STATE_CRITICAL; 73int no_swap_state = STATE_CRITICAL;
67 74
68int 75int
69main (int argc, char **argv) 76main (int argc, char **argv)
70{ 77{
71 int percent_used, percent; 78 unsigned int percent_used, percent;
72 float total_swap_mb = 0, used_swap_mb = 0, free_swap_mb = 0; 79 uint64_t total_swap_mb = 0, used_swap_mb = 0, free_swap_mb = 0;
73 float dsktotal_mb = 0, dskused_mb = 0, dskfree_mb = 0, tmp_mb = 0; 80 uint64_t dsktotal_mb = 0, dskused_mb = 0, dskfree_mb = 0;
81 uint64_t tmp_KB = 0;
74 int result = STATE_UNKNOWN; 82 int result = STATE_UNKNOWN;
75 char input_buffer[MAX_INPUT_BUFFER]; 83 char input_buffer[MAX_INPUT_BUFFER];
76#ifdef HAVE_PROC_MEMINFO 84#ifdef HAVE_PROC_MEMINFO
@@ -116,10 +124,15 @@ main (int argc, char **argv)
116 } 124 }
117 fp = fopen (PROC_MEMINFO, "r"); 125 fp = fopen (PROC_MEMINFO, "r");
118 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) { 126 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
119 if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %f %f %f", &dsktotal_mb, &dskused_mb, &dskfree_mb) == 3) { 127 /*
120 dsktotal_mb = dsktotal_mb / 1048576; /* Apply conversion */ 128 * The following sscanf call looks for a line looking like: "Swap: 123 123 123"
121 dskused_mb = dskused_mb / 1048576; 129 * On which kind of system this format exists, I can not say, but I wanted to
122 dskfree_mb = dskfree_mb / 1048576; 130 * document this for people who are not adapt with sscanf anymore, like me
131 */
132 if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %lu %lu %lu", &dsktotal_mb, &dskused_mb, &dskfree_mb) == 3) {
133 dsktotal_mb = dsktotal_mb / (1024 * 1024); /* Apply conversion */
134 dskused_mb = dskused_mb / (1024 * 1024);
135 dskfree_mb = dskfree_mb / (1024 * 1024);
123 total_swap_mb += dsktotal_mb; 136 total_swap_mb += dsktotal_mb;
124 used_swap_mb += dskused_mb; 137 used_swap_mb += dskused_mb;
125 free_swap_mb += dskfree_mb; 138 free_swap_mb += dskfree_mb;
@@ -128,21 +141,25 @@ main (int argc, char **argv)
128 percent=100.0; 141 percent=100.0;
129 else 142 else
130 percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); 143 percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb));
131 result = max_state (result, check_swap (percent, dskfree_mb, dsktotal_mb)); 144 result = max_state (result, check_swap (dskfree_mb, dsktotal_mb));
132 if (verbose) 145 if (verbose)
133 xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); 146 xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent);
134 } 147 }
135 } 148 }
136 else if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%[TotalFre]%*[:] %f %*[k]%*[B]", str, &tmp_mb)) { 149 /*
150 * The following sscanf call looks for lines looking like: "SwapTotal: 123" and "SwapFree: 123"
151 * This format exists at least on Debian Linux with a 5.* kernel
152 */
153 else if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%[TotalFre]%*[:] %lu %*[k]%*[B]", str, &tmp_KB)) {
137 if (verbose >= 3) { 154 if (verbose >= 3) {
138 printf("Got %s with %f\n", str, tmp_mb); 155 printf("Got %s with %lu\n", str, tmp_KB);
139 } 156 }
140 /* I think this part is always in Kb, so convert to mb */ 157 /* I think this part is always in Kb, so convert to mb */
141 if (strcmp ("Total", str) == 0) { 158 if (strcmp ("Total", str) == 0) {
142 dsktotal_mb = tmp_mb / 1024; 159 dsktotal_mb = tmp_KB / 1024;
143 } 160 }
144 else if (strcmp ("Free", str) == 0) { 161 else if (strcmp ("Free", str) == 0) {
145 dskfree_mb = tmp_mb / 1024; 162 dskfree_mb = tmp_KB / 1024;
146 } 163 }
147 } 164 }
148 } 165 }
@@ -227,7 +244,7 @@ main (int argc, char **argv)
227 free_swap_mb += dskfree_mb; 244 free_swap_mb += dskfree_mb;
228 if (allswaps) { 245 if (allswaps) {
229 percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); 246 percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb));
230 result = max_state (result, check_swap (percent, dskfree_mb, dsktotal_mb)); 247 result = max_state (result, check_swap (dskfree_mb, dsktotal_mb));
231 if (verbose) 248 if (verbose)
232 xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); 249 xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent);
233 } 250 }
@@ -289,7 +306,7 @@ main (int argc, char **argv)
289 306
290 if(allswaps && dsktotal_mb > 0){ 307 if(allswaps && dsktotal_mb > 0){
291 percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); 308 percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb));
292 result = max_state (result, check_swap (percent, dskfree_mb, dsktotal_mb)); 309 result = max_state (result, check_swap (dskfree_mb, dsktotal_mb));
293 if (verbose) { 310 if (verbose) {
294 xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); 311 xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent);
295 } 312 }
@@ -328,7 +345,7 @@ main (int argc, char **argv)
328 345
329 if(allswaps && dsktotal_mb > 0){ 346 if(allswaps && dsktotal_mb > 0){
330 percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); 347 percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb));
331 result = max_state (result, check_swap (percent, dskfree_mb, dsktotal_mb)); 348 result = max_state (result, check_swap(dskfree_mb, dsktotal_mb));
332 if (verbose) { 349 if (verbose) {
333 xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); 350 xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent);
334 } 351 }
@@ -355,14 +372,19 @@ main (int argc, char **argv)
355 status = "- Swap is either disabled, not present, or of zero size. "; 372 status = "- Swap is either disabled, not present, or of zero size. ";
356 } 373 }
357 374
358 result = max_state (result, check_swap (percent_used, free_swap_mb, total_swap_mb)); 375 result = max_state (result, check_swap(free_swap_mb, total_swap_mb));
359 printf (_("SWAP %s - %d%% free (%d MB out of %d MB) %s|"), 376 printf (_("SWAP %s - %d%% free (%dMB out of %dMB) %s|"),
360 state_text (result), 377 state_text (result),
361 (100 - percent_used), (int) free_swap_mb, (int) total_swap_mb, status); 378 (100 - percent_used), (int) free_swap_mb, (int) total_swap_mb, status);
362 379
363 puts (perfdata ("swap", (long) free_swap_mb, "MB", 380 uint64_t warn_print = warn.value;
364 TRUE, (long) max (warn_size_bytes/(1024 * 1024), warn_percent/100.0*total_swap_mb), 381 if (warn.is_percentage) warn_print = warn.value * (total_swap_mb *1024 *1024/100);
365 TRUE, (long) max (crit_size_bytes/(1024 * 1024), crit_percent/100.0*total_swap_mb), 382 uint64_t crit_print = crit.value;
383 if (crit.is_percentage) crit_print = crit.value * (total_swap_mb *1024 *1024/100);
384
385 puts (perfdata_uint64 ("swap", free_swap_mb *1024 *1024, "B",
386 TRUE, warn_print,
387 TRUE, crit_print,
366 TRUE, 0, 388 TRUE, 0,
367 TRUE, (long) total_swap_mb)); 389 TRUE, (long) total_swap_mb));
368 390
@@ -370,26 +392,37 @@ main (int argc, char **argv)
370} 392}
371 393
372 394
373
374int 395int
375check_swap (int usp, float free_swap_mb, float total_swap_mb) 396check_swap(float free_swap_mb, float total_swap_mb)
376{ 397{
377 398
378 if (!total_swap_mb) return no_swap_state; 399 if (!total_swap_mb) return no_swap_state;
379 400
380 int result = STATE_UNKNOWN; 401 uint64_t free_swap = free_swap_mb * (1024 * 1024); /* Convert back to bytes as warn and crit specified in bytes */
381 float free_swap = free_swap_mb * (1024 * 1024); /* Convert back to bytes as warn and crit specified in bytes */ 402
382 if (usp >= 0 && crit_percent != 0 && usp >= (100.0 - crit_percent)) 403 if (!crit.is_percentage && crit.value <= free_swap) return STATE_CRITICAL;
383 result = STATE_CRITICAL; 404 if (!warn.is_percentage && warn.value <= free_swap) return STATE_WARNING;
384 else if (crit_size_bytes > 0 && free_swap <= crit_size_bytes) 405
385 result = STATE_CRITICAL; 406
386 else if (usp >= 0 && warn_percent != 0 && usp >= (100.0 - warn_percent)) 407 uint64_t usage_percentage = ((total_swap_mb - free_swap_mb) / total_swap_mb) * 100;
387 result = STATE_WARNING; 408
388 else if (warn_size_bytes > 0 && free_swap <= warn_size_bytes) 409 if (crit.is_percentage &&
389 result = STATE_WARNING; 410 usage_percentage >= 0 &&
390 else if (usp >= 0.0) 411 crit.value != 0 &&
391 result = STATE_OK; 412 usage_percentage >= (100 - crit.value))
392 return result; 413 {
414 return STATE_CRITICAL;
415 }
416
417 if (warn.is_percentage &&
418 usage_percentage >= 0 &&
419 warn.value != 0 &&
420 usage_percentage >= (100 - warn.value))
421 {
422 return STATE_WARNING;
423 }
424
425 return STATE_OK;
393} 426}
394 427
395 428
@@ -422,42 +455,67 @@ process_arguments (int argc, char **argv)
422 break; 455 break;
423 456
424 switch (c) { 457 switch (c) {
425 case 'w': /* warning size threshold */ 458 case 'w': /* warning size threshold */
426 if (is_intnonneg (optarg)) { 459 {
427 warn_size_bytes = (float) atoi (optarg); 460 /*
428 break; 461 * We expect either a positive integer value without a unit, which means
429 } 462 * the unit is Bytes or a positive integer value and a percentage sign (%),
430 else if (strstr (optarg, ",") && 463 * which means the value must be with 0 and 100 and is relative to the total swap
431 strstr (optarg, "%") && 464 */
432 sscanf (optarg, "%f,%d%%", &warn_size_bytes, &warn_percent) == 2) { 465 size_t length;
433 warn_size_bytes = floorf(warn_size_bytes); 466 length = strlen(optarg);
434 break; 467
435 } 468 if (optarg[length - 1] == '%') {
436 else if (strstr (optarg, "%") && 469 // It's percentage!
437 sscanf (optarg, "%d%%", &warn_percent) == 1) { 470 warn.is_percentage = true;
438 break; 471 optarg[length - 1] = '\0';
439 } 472 if (is_uint64(optarg, &warn.value)) {
440 else { 473 if (warn.value > 100) {
441 usage4 (_("Warning threshold must be integer or percentage!")); 474 usage4 (_("Warning threshold percentage must be <= 100!"));
442 } 475 } else {
443 case 'c': /* critical size threshold */ 476 break;
444 if (is_intnonneg (optarg)) { 477 }
445 crit_size_bytes = (float) atoi (optarg); 478 }
446 break; 479 } else {
447 } 480 // It's Bytes
448 else if (strstr (optarg, ",") && 481 warn.is_percentage = false;
449 strstr (optarg, "%") && 482 if (is_uint64(optarg, &warn.value)) {
450 sscanf (optarg, "%f,%d%%", &crit_size_bytes, &crit_percent) == 2) { 483 break;
451 crit_size_bytes = floorf(crit_size_bytes); 484 } else {
452 break; 485 usage4 (_("Warning threshold be positive integer or percentage!"));
453 } 486 }
454 else if (strstr (optarg, "%") && 487 }
455 sscanf (optarg, "%d%%", &crit_percent) == 1) {
456 break;
457 }
458 else {
459 usage4 (_("Critical threshold must be integer or percentage!"));
460 } 488 }
489 case 'c': /* critical size threshold */
490 {
491 /*
492 * We expect either a positive integer value without a unit, which means
493 * the unit is Bytes or a positive integer value and a percentage sign (%),
494 * which means the value must be with 0 and 100 and is relative to the total swap
495 */
496 size_t length;
497 length = strlen(optarg);
498
499 if (optarg[length - 1] == '%') {
500 // It's percentage!
501 crit.is_percentage = true;
502 optarg[length - 1] = '\0';
503 if (is_uint64(optarg, &crit.value)) {
504 if (crit.value> 100) {
505 usage4 (_("Critical threshold percentage must be <= 100!"));
506 } else {
507 break;
508 }
509 }
510 } else {
511 crit.is_percentage = false;
512 if (is_uint64(optarg, &crit.value)) {
513 break;
514 } else {
515 usage4 (_("Critical threshold be positive integer or percentage!"));
516 }
517 }
518 }
461 case 'a': /* all swap */ 519 case 'a': /* all swap */
462 allswaps = TRUE; 520 allswaps = TRUE;
463 break; 521 break;
@@ -482,23 +540,6 @@ process_arguments (int argc, char **argv)
482 c = optind; 540 c = optind;
483 if (c == argc) 541 if (c == argc)
484 return validate_arguments (); 542 return validate_arguments ();
485 if (warn_percent == 0 && is_intnonneg (argv[c]))
486 warn_percent = atoi (argv[c++]);
487
488 if (c == argc)
489 return validate_arguments ();
490 if (crit_percent == 0 && is_intnonneg (argv[c]))
491 crit_percent = atoi (argv[c++]);
492
493 if (c == argc)
494 return validate_arguments ();
495 if (warn_size_bytes == 0 && is_intnonneg (argv[c]))
496 warn_size_bytes = (float) atoi (argv[c++]);
497
498 if (c == argc)
499 return validate_arguments ();
500 if (crit_size_bytes == 0 && is_intnonneg (argv[c]))
501 crit_size_bytes = (float) atoi (argv[c++]);
502 543
503 return validate_arguments (); 544 return validate_arguments ();
504} 545}
@@ -508,17 +549,12 @@ process_arguments (int argc, char **argv)
508int 549int
509validate_arguments (void) 550validate_arguments (void)
510{ 551{
511 if (warn_percent == 0 && crit_percent == 0 && warn_size_bytes == 0 552 if (warn.value == 0 && crit.value == 0) {
512 && crit_size_bytes == 0) {
513 return ERROR; 553 return ERROR;
514 } 554 }
515 else if (warn_percent < crit_percent) { 555 else if (warn.value < crit.value) {
516 usage4
517 (_("Warning percentage should be more than critical percentage"));
518 }
519 else if (warn_size_bytes < crit_size_bytes) {
520 usage4 556 usage4
521 (_("Warning free space should be more than critical free space")); 557 (_("Warning should be more than critical"));
522 } 558 }
523 return OK; 559 return OK;
524} 560}
@@ -564,7 +600,6 @@ print_help (void)
564} 600}
565 601
566 602
567
568void 603void
569print_usage (void) 604print_usage (void)
570{ 605{