diff options
Diffstat (limited to 'plugins-scripts/check_file_age.pl')
-rwxr-xr-x | plugins-scripts/check_file_age.pl | 61 |
1 files changed, 48 insertions, 13 deletions
diff --git a/plugins-scripts/check_file_age.pl b/plugins-scripts/check_file_age.pl index 56b8e97c..26281ddd 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,10 +51,10 @@ 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@'); @@ -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: file size is ignored (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://www.monitoring-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 doing what is desired\n"; print "\n"; support(); } + |