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.c93
1 files changed, 47 insertions, 46 deletions
diff --git a/plugins/check_swap.c b/plugins/check_swap.c
index 685c2cc..cd965e3 100644
--- a/plugins/check_swap.c
+++ b/plugins/check_swap.c
@@ -1,30 +1,30 @@
1/***************************************************************************** 1/*****************************************************************************
2* 2*
3* Monitoring check_swap plugin 3* Monitoring check_swap plugin
4* 4*
5* License: GPL 5* License: GPL
6* Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net) 6* Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net)
7* Copyright (c) 2000-2007 Monitoring Plugins Development Team 7* Copyright (c) 2000-2007 Monitoring Plugins Development Team
8* 8*
9* Description: 9* Description:
10* 10*
11* This file contains the check_swap plugin 11* This file contains the check_swap plugin
12* 12*
13* 13*
14* This program is free software: you can redistribute it and/or modify 14* This program is free software: you can redistribute it and/or modify
15* it under the terms of the GNU General Public License as published by 15* it under the terms of the GNU General Public License as published by
16* the Free Software Foundation, either version 3 of the License, or 16* the Free Software Foundation, either version 3 of the License, or
17* (at your option) any later version. 17* (at your option) any later version.
18* 18*
19* This program is distributed in the hope that it will be useful, 19* This program is distributed in the hope that it will be useful,
20* but WITHOUT ANY WARRANTY; without even the implied warranty of 20* but WITHOUT ANY WARRANTY; without even the implied warranty of
21* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22* GNU General Public License for more details. 22* GNU General Public License for more details.
23* 23*
24* You should have received a copy of the GNU General Public License 24* You should have received a copy of the GNU General Public License
25* along with this program. If not, see <http://www.gnu.org/licenses/>. 25* along with this program. If not, see <http://www.gnu.org/licenses/>.
26* 26*
27* 27*
28*****************************************************************************/ 28*****************************************************************************/
29 29
30const char *progname = "check_swap"; 30const char *progname = "check_swap";
@@ -34,9 +34,6 @@ 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 37
41#ifdef HAVE_DECL_SWAPCTL 38#ifdef HAVE_DECL_SWAPCTL
42# ifdef HAVE_SYS_PARAM_H 39# ifdef HAVE_SYS_PARAM_H
@@ -142,14 +139,15 @@ main (int argc, char **argv)
142 percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); 139 percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb));
143 result = max_state (result, check_swap (dskfree_mb, dsktotal_mb)); 140 result = max_state (result, check_swap (dskfree_mb, dsktotal_mb));
144 if (verbose) 141 if (verbose)
145 xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); 142 xasprintf (&status, "%s [%lu (%d%%)]", status, dskfree_mb, 100 - percent);
146 } 143 }
147 } 144 }
145
148 /* 146 /*
149 * The following sscanf call looks for lines looking like: "SwapTotal: 123" and "SwapFree: 123" 147 * The following sscanf call looks for lines looking like: "SwapTotal: 123" and "SwapFree: 123"
150 * This format exists at least on Debian Linux with a 5.* kernel 148 * This format exists at least on Debian Linux with a 5.* kernel
151 */ 149 */
152 else if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%[TotalFre]%*[:] %lu %*[k]%*[B]", str, &tmp_KB)) { 150 else if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu %*[k]%*[B]", str, &tmp_KB)) {
153 if (verbose >= 3) { 151 if (verbose >= 3) {
154 printf("Got %s with %lu\n", str, tmp_KB); 152 printf("Got %s with %lu\n", str, tmp_KB);
155 } 153 }
@@ -158,7 +156,10 @@ main (int argc, char **argv)
158 dsktotal_mb = tmp_KB / 1024; 156 dsktotal_mb = tmp_KB / 1024;
159 } 157 }
160 else if (strcmp ("Free", str) == 0) { 158 else if (strcmp ("Free", str) == 0) {
161 dskfree_mb = tmp_KB / 1024; 159 dskfree_mb = dskfree_mb + tmp_KB / 1024;
160 }
161 else if (strcmp ("Cached", str) == 0) {
162 dskfree_mb = dskfree_mb + tmp_KB / 1024;
162 } 163 }
163 } 164 }
164 } 165 }
@@ -177,7 +178,7 @@ main (int argc, char **argv)
177# ifdef _AIX 178# ifdef _AIX
178 if (!allswaps) { 179 if (!allswaps) {
179 xasprintf(&swap_command, "%s", "/usr/sbin/lsps -s"); 180 xasprintf(&swap_command, "%s", "/usr/sbin/lsps -s");
180 xasprintf(&swap_format, "%s", "%f%*s %f"); 181 xasprintf(&swap_format, "%s", "%lu%*s %lu");
181 conv_factor = 1; 182 conv_factor = 1;
182 } 183 }
183# endif 184# endif
@@ -204,9 +205,9 @@ main (int argc, char **argv)
204 temp_buffer = strtok (input_buffer, " \n"); 205 temp_buffer = strtok (input_buffer, " \n");
205 while (temp_buffer) { 206 while (temp_buffer) {
206 if (strstr (temp_buffer, "blocks")) 207 if (strstr (temp_buffer, "blocks"))
207 sprintf (str, "%s %s", str, "%f"); 208 sprintf (str, "%s %s", str, "%lu");
208 else if (strstr (temp_buffer, "dskfree")) 209 else if (strstr (temp_buffer, "dskfree"))
209 sprintf (str, "%s %s", str, "%f"); 210 sprintf (str, "%s %s", str, "%lu");
210 else 211 else
211 sprintf (str, "%s %s", str, "%*s"); 212 sprintf (str, "%s %s", str, "%*s");
212 temp_buffer = strtok (NULL, " \n"); 213 temp_buffer = strtok (NULL, " \n");
@@ -385,7 +386,7 @@ main (int argc, char **argv)
385 TRUE, warn_print, 386 TRUE, warn_print,
386 TRUE, crit_print, 387 TRUE, crit_print,
387 TRUE, 0, 388 TRUE, 0,
388 TRUE, (long) total_swap_mb)); 389 TRUE, (long) total_swap_mb * 1024 * 1024));
389 390
390 return result; 391 return result;
391} 392}
@@ -406,7 +407,6 @@ check_swap(float free_swap_mb, float total_swap_mb)
406 uint64_t usage_percentage = ((total_swap_mb - free_swap_mb) / total_swap_mb) * 100; 407 uint64_t usage_percentage = ((total_swap_mb - free_swap_mb) / total_swap_mb) * 100;
407 408
408 if (crit.is_percentage && 409 if (crit.is_percentage &&
409 usage_percentage >= 0 &&
410 crit.value != 0 && 410 crit.value != 0 &&
411 usage_percentage >= (100 - crit.value)) 411 usage_percentage >= (100 - crit.value))
412 { 412 {
@@ -414,7 +414,6 @@ check_swap(float free_swap_mb, float total_swap_mb)
414 } 414 }
415 415
416 if (warn.is_percentage && 416 if (warn.is_percentage &&
417 usage_percentage >= 0 &&
418 warn.value != 0 && 417 warn.value != 0 &&
419 usage_percentage >= (100 - warn.value)) 418 usage_percentage >= (100 - warn.value))
420 { 419 {
@@ -471,10 +470,9 @@ process_arguments (int argc, char **argv)
471 if (is_uint64(optarg, &warn.value)) { 470 if (is_uint64(optarg, &warn.value)) {
472 if (warn.value > 100) { 471 if (warn.value > 100) {
473 usage4 (_("Warning threshold percentage must be <= 100!")); 472 usage4 (_("Warning threshold percentage must be <= 100!"));
474 } else {
475 break;
476 } 473 }
477 } 474 }
475 break;
478 } else { 476 } else {
479 /* It's Bytes */ 477 /* It's Bytes */
480 warn.is_percentage = 0; 478 warn.is_percentage = 0;
@@ -502,10 +500,9 @@ process_arguments (int argc, char **argv)
502 if (is_uint64(optarg, &crit.value)) { 500 if (is_uint64(optarg, &crit.value)) {
503 if (crit.value> 100) { 501 if (crit.value> 100) {
504 usage4 (_("Critical threshold percentage must be <= 100!")); 502 usage4 (_("Critical threshold percentage must be <= 100!"));
505 } else {
506 break;
507 } 503 }
508 } 504 }
505 break;
509 } else { 506 } else {
510 /* It's Bytes */ 507 /* It's Bytes */
511 crit.is_percentage = 0; 508 crit.is_percentage = 0;
@@ -523,6 +520,7 @@ process_arguments (int argc, char **argv)
523 if ((no_swap_state = mp_translate_state(optarg)) == ERROR) { 520 if ((no_swap_state = mp_translate_state(optarg)) == ERROR) {
524 usage4 (_("no-swap result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); 521 usage4 (_("no-swap result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
525 } 522 }
523 break;
526 case 'v': /* verbose */ 524 case 'v': /* verbose */
527 verbose++; 525 verbose++;
528 break; 526 break;
@@ -552,9 +550,12 @@ validate_arguments (void)
552 if (warn.value == 0 && crit.value == 0) { 550 if (warn.value == 0 && crit.value == 0) {
553 return ERROR; 551 return ERROR;
554 } 552 }
555 else if (warn.value < crit.value) { 553 else if ((warn.is_percentage == crit.is_percentage) && (warn.value < crit.value)) {
556 usage4 554 /* This is NOT triggered if warn and crit are different units, e.g warn is percentage
557 (_("Warning should be more than critical")); 555 * and crit is absolute. We cannot determine the condition at this point since we
556 * dont know the value of total swap yet
557 */
558 usage4(_("Warning should be more than critical"));
558 } 559 }
559 return OK; 560 return OK;
560} 561}
@@ -570,7 +571,7 @@ print_help (void)
570 571
571 printf ("%s\n", _("Check swap space on local machine.")); 572 printf ("%s\n", _("Check swap space on local machine."));
572 573
573 printf ("\n\n"); 574 printf ("\n\n");
574 575
575 print_usage (); 576 print_usage ();
576 577
@@ -578,23 +579,23 @@ print_help (void)
578 printf (UT_EXTRA_OPTS); 579 printf (UT_EXTRA_OPTS);
579 580
580 printf (" %s\n", "-w, --warning=INTEGER"); 581 printf (" %s\n", "-w, --warning=INTEGER");
581 printf (" %s\n", _("Exit with WARNING status if less than INTEGER bytes of swap space are free")); 582 printf (" %s\n", _("Exit with WARNING status if less than INTEGER bytes of swap space are free"));
582 printf (" %s\n", "-w, --warning=PERCENT%%"); 583 printf (" %s\n", "-w, --warning=PERCENT%");
583 printf (" %s\n", _("Exit with WARNING status if less than PERCENT of swap space is free")); 584 printf (" %s\n", _("Exit with WARNING status if less than PERCENT of swap space is free"));
584 printf (" %s\n", "-c, --critical=INTEGER"); 585 printf (" %s\n", "-c, --critical=INTEGER");
585 printf (" %s\n", _("Exit with CRITICAL status if less than INTEGER bytes of swap space are free")); 586 printf (" %s\n", _("Exit with CRITICAL status if less than INTEGER bytes of swap space are free"));
586 printf (" %s\n", "-c, --critical=PERCENT%%"); 587 printf (" %s\n", "-c, --critical=PERCENT%");
587 printf (" %s\n", _("Exit with CRITICAL status if less than PERCENT of swap space is free")); 588 printf (" %s\n", _("Exit with CRITICAL status if less than PERCENT of swap space is free"));
588 printf (" %s\n", "-a, --allswaps"); 589 printf (" %s\n", "-a, --allswaps");
589 printf (" %s\n", _("Conduct comparisons for all swap partitions, one by one")); 590 printf (" %s\n", _("Conduct comparisons for all swap partitions, one by one"));
590 printf (" %s\n", "-n, --no-swap=<ok|warning|critical|unknown>"); 591 printf (" %s\n", "-n, --no-swap=<ok|warning|critical|unknown>");
591 printf (" %s %s\n", _("Resulting state when there is no swap regardless of thresholds. Default:"), state_text(no_swap_state)); 592 printf (" %s %s\n", _("Resulting state when there is no swap regardless of thresholds. Default:"), state_text(no_swap_state));
592 printf (UT_VERBOSE); 593 printf (UT_VERBOSE);
593 594
594 printf ("\n"); 595 printf ("\n");
595 printf ("%s\n", _("Notes:")); 596 printf ("%s\n", _("Notes:"));
596 printf (" %s\n", _("Both INTEGER and PERCENT thresholds can be specified, they are all checked.")); 597 printf (" %s\n", _("Both INTEGER and PERCENT thresholds can be specified, they are all checked."));
597 printf (" %s\n", _("On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s.")); 598 printf (" %s\n", _("On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s."));
598 599
599 printf (UT_SUPPORT); 600 printf (UT_SUPPORT);
600} 601}
@@ -604,6 +605,6 @@ void
604print_usage (void) 605print_usage (void)
605{ 606{
606 printf ("%s\n", _("Usage:")); 607 printf ("%s\n", _("Usage:"));
607 printf (" %s [-av] -w <percent_free>%% -c <percent_free>%%\n",progname); 608 printf (" %s [-av] -w <percent_free>%% -c <percent_free>%%\n",progname);
608 printf (" -w <bytes_free> -c <bytes_free> [-n <state>]\n"); 609 printf (" -w <bytes_free> -c <bytes_free> [-n <state>]\n");
609} 610}