diff options
author | phowen <phowen@cisco.com> | 2017-02-14 15:19:18 +0000 |
---|---|---|
committer | phowen <phowen@cisco.com> | 2017-02-14 15:19:18 +0000 |
commit | 98a365d76c9b7e270d7b00953905d80c80c1467d (patch) | |
tree | 57c4a7a17bd138344b31de2f8f2de7e0f4ed78d1 /plugins-scripts/check_file_age.pl | |
parent | 0d73b499dd939924d49f955ac84550fab5f3cdde (diff) | |
download | monitoring-plugins-98a365d76c9b7e270d7b00953905d80c80c1467d.tar.gz |
add range checking to check_file_age
Diffstat (limited to 'plugins-scripts/check_file_age.pl')
-rwxr-xr-x | plugins-scripts/check_file_age.pl | 63 |
1 files changed, 49 insertions, 14 deletions
diff --git a/plugins-scripts/check_file_age.pl b/plugins-scripts/check_file_age.pl index 56b8e97c..dc49ce79 100755 --- a/plugins-scripts/check_file_age.pl +++ b/plugins-scripts/check_file_age.pl @@ -43,8 +43,6 @@ $ENV{'ENV'}=''; $opt_w = 240; $opt_c = 600; -$opt_W = 0; -$opt_C = 0; $opt_f = ""; Getopt::Long::Configure('bundling'); @@ -53,13 +51,13 @@ GetOptions( "h" => \$opt_h, "help" => \$opt_h, "i" => \$opt_i, "ignore-missing" => \$opt_i, "f=s" => \$opt_f, "file" => \$opt_f, - "w=f" => \$opt_w, "warning-age=f" => \$opt_w, - "W=f" => \$opt_W, "warning-size=f" => \$opt_W, - "c=f" => \$opt_c, "critical-age=f" => \$opt_c, - "C=f" => \$opt_C, "critical-size=f" => \$opt_C); + "w=s" => \$opt_w, "warning-age=s" => \$opt_w, + "W=s" => \$opt_W, "warning-size=s" => \$opt_W, + "c=s" => \$opt_c, "critical-age=s" => \$opt_c, + "C=s" => \$opt_C, "critical-size=s" => \$opt_C); if ($opt_V) { - print_revision($PROGNAME, '@NP_VERSION@'); + print_revision($PROGNAME, '2.2.22.g0d73b'); exit $ERRORS{'UNKNOWN'}; } @@ -91,18 +89,47 @@ unless (-e $opt_f) { $st = File::stat::stat($opt_f); $age = time - $st->mtime; $size = $st->size; -$perfdata = "age=${age}s;${opt_w};${opt_c} size=${size}B;${opt_W};${opt_C};0"; - $result = 'OK'; -if (($opt_c and $age > $opt_c) or ($opt_C and $size < $opt_C)) { - $result = 'CRITICAL'; +if ($opt_c !~ m/^\d+$/ or ($opt_C and $opt_C !~ m/^\d+$/) + or $opt_w !~ m/^\d+$/ or ($opt_W and $opt_W !~ m/^\d+$/)) { + # range has been specified so use M::P::R to process + require Monitoring::Plugin::Range; + # use permissive range defaults for size when none specified + $opt_W = "0:" unless ($opt_W); + $opt_C = "0:" unless ($opt_C); + + if (Monitoring::Plugin::Range->parse_range_string($opt_c) + ->check_range($age) == 1) { # 1 means it raises an alert because it's OUTSIDE the range + $result = 'CRITICAL'; + } + elsif (Monitoring::Plugin::Range->parse_range_string($opt_C) + ->check_range($size) == 1) { + $result = 'CRITICAL'; + } + elsif (Monitoring::Plugin::Range->parse_range_string($opt_w) + ->check_range($age) == 1) { + $result = 'WARNING'; + } + elsif (Monitoring::Plugin::Range->parse_range_string($opt_W) + ->check_range($size) == 1) { + $result = 'WARNING'; + } } -elsif (($opt_w and $age > $opt_w) or ($opt_W and $size < $opt_W)) { - $result = 'WARNING'; +else { + # use permissive defaults for size when none specified + $opt_W = 0 unless ($opt_W); + $opt_C = 0 unless ($opt_C); + if ($age > $opt_c or $size < $opt_C) { + $result = 'CRITICAL'; + } + elsif ($age > $opt_w or $size < $opt_W) { + $result = 'WARNING'; + } } +$perfdata = "age=${age}s;${opt_w};${opt_c} size=${size}B;${opt_W};${opt_C};0"; print "FILE_AGE $result: $opt_f is $age seconds old and $size bytes | $perfdata\n"; exit $ERRORS{$result}; @@ -120,7 +147,15 @@ sub print_help () { print "\n"; print " -i | --ignore-missing : return OK if the file does not exist\n"; print " <secs> File must be no more than this many seconds old (default: warn 240 secs, crit 600)\n"; - print " <size> File must be at least this many bytes long (default: crit 0 bytes)\n"; + print " <size> File must be at least this many bytes long (default: crit 0 bytes)\n\n"; + print " Both <secs> and <size> can specify a range using the standard plugin syntax\n"; + print " If any of the warning and critical arguments are in range syntax (not just bare numbers)\n"; + print " then all warning and critical arguments will be interpreted as ranges.\n"; + print " To use range processing the perl module Monitoring::Plugin must be installed\n"; + print " For range syntax see https://nagios-plugins.org/doc/guidelines.html#THRESHOLDFORMAT\n"; + print " It is strongly recommended when using range syntax that all four of -w, -W, -c and -C are specified\n"; + print " otherwise it is unlikely that the size test will be doint wat is desired\n"; print "\n"; support(); } + |