diff options
Diffstat (limited to 'plugins-scripts/check_file_age.pl')
-rwxr-xr-x | plugins-scripts/check_file_age.pl | 66 |
1 files changed, 50 insertions, 16 deletions
diff --git a/plugins-scripts/check_file_age.pl b/plugins-scripts/check_file_age.pl index 4415fdf4..26281ddd 100755 --- a/plugins-scripts/check_file_age.pl +++ b/plugins-scripts/check_file_age.pl | |||
@@ -27,7 +27,6 @@ use File::stat; | |||
27 | use vars qw($PROGNAME); | 27 | use vars qw($PROGNAME); |
28 | use FindBin; | 28 | use FindBin; |
29 | use lib "$FindBin::Bin"; | 29 | use lib "$FindBin::Bin"; |
30 | use lib '@libexecdir@'; | ||
31 | use utils qw (%ERRORS &print_revision &support); | 30 | use utils qw (%ERRORS &print_revision &support); |
32 | 31 | ||
33 | sub print_help (); | 32 | sub print_help (); |
@@ -44,8 +43,6 @@ $ENV{'ENV'}=''; | |||
44 | 43 | ||
45 | $opt_w = 240; | 44 | $opt_w = 240; |
46 | $opt_c = 600; | 45 | $opt_c = 600; |
47 | $opt_W = 0; | ||
48 | $opt_C = 0; | ||
49 | $opt_f = ""; | 46 | $opt_f = ""; |
50 | 47 | ||
51 | Getopt::Long::Configure('bundling'); | 48 | Getopt::Long::Configure('bundling'); |
@@ -54,19 +51,19 @@ GetOptions( | |||
54 | "h" => \$opt_h, "help" => \$opt_h, | 51 | "h" => \$opt_h, "help" => \$opt_h, |
55 | "i" => \$opt_i, "ignore-missing" => \$opt_i, | 52 | "i" => \$opt_i, "ignore-missing" => \$opt_i, |
56 | "f=s" => \$opt_f, "file" => \$opt_f, | 53 | "f=s" => \$opt_f, "file" => \$opt_f, |
57 | "w=f" => \$opt_w, "warning-age=f" => \$opt_w, | 54 | "w=s" => \$opt_w, "warning-age=s" => \$opt_w, |
58 | "W=f" => \$opt_W, "warning-size=f" => \$opt_W, | 55 | "W=s" => \$opt_W, "warning-size=s" => \$opt_W, |
59 | "c=f" => \$opt_c, "critical-age=f" => \$opt_c, | 56 | "c=s" => \$opt_c, "critical-age=s" => \$opt_c, |
60 | "C=f" => \$opt_C, "critical-size=f" => \$opt_C); | 57 | "C=s" => \$opt_C, "critical-size=s" => \$opt_C); |
61 | 58 | ||
62 | if ($opt_V) { | 59 | if ($opt_V) { |
63 | print_revision($PROGNAME, '@NP_VERSION@'); | 60 | print_revision($PROGNAME, '@NP_VERSION@'); |
64 | exit $ERRORS{'OK'}; | 61 | exit $ERRORS{'UNKNOWN'}; |
65 | } | 62 | } |
66 | 63 | ||
67 | if ($opt_h) { | 64 | if ($opt_h) { |
68 | print_help(); | 65 | print_help(); |
69 | exit $ERRORS{'OK'}; | 66 | exit $ERRORS{'UNKNOWN'}; |
70 | } | 67 | } |
71 | 68 | ||
72 | $opt_f = shift unless ($opt_f); | 69 | $opt_f = shift unless ($opt_f); |
@@ -92,18 +89,47 @@ unless (-e $opt_f) { | |||
92 | $st = File::stat::stat($opt_f); | 89 | $st = File::stat::stat($opt_f); |
93 | $age = time - $st->mtime; | 90 | $age = time - $st->mtime; |
94 | $size = $st->size; | 91 | $size = $st->size; |
95 | $perfdata = "age=${age}s;${opt_w};${opt_c} size=${size}B;${opt_W};${opt_C};0"; | ||
96 | |||
97 | 92 | ||
98 | $result = 'OK'; | 93 | $result = 'OK'; |
99 | 94 | ||
100 | if (($opt_c and $age > $opt_c) or ($opt_C and $size < $opt_C)) { | 95 | if ($opt_c !~ m/^\d+$/ or ($opt_C and $opt_C !~ m/^\d+$/) |
101 | $result = 'CRITICAL'; | 96 | or $opt_w !~ m/^\d+$/ or ($opt_W and $opt_W !~ m/^\d+$/)) { |
97 | # range has been specified so use M::P::R to process | ||
98 | require Monitoring::Plugin::Range; | ||
99 | # use permissive range defaults for size when none specified | ||
100 | $opt_W = "0:" unless ($opt_W); | ||
101 | $opt_C = "0:" unless ($opt_C); | ||
102 | |||
103 | if (Monitoring::Plugin::Range->parse_range_string($opt_c) | ||
104 | ->check_range($age) == 1) { # 1 means it raises an alert because it's OUTSIDE the range | ||
105 | $result = 'CRITICAL'; | ||
106 | } | ||
107 | elsif (Monitoring::Plugin::Range->parse_range_string($opt_C) | ||
108 | ->check_range($size) == 1) { | ||
109 | $result = 'CRITICAL'; | ||
110 | } | ||
111 | elsif (Monitoring::Plugin::Range->parse_range_string($opt_w) | ||
112 | ->check_range($age) == 1) { | ||
113 | $result = 'WARNING'; | ||
114 | } | ||
115 | elsif (Monitoring::Plugin::Range->parse_range_string($opt_W) | ||
116 | ->check_range($size) == 1) { | ||
117 | $result = 'WARNING'; | ||
118 | } | ||
102 | } | 119 | } |
103 | elsif (($opt_w and $age > $opt_w) or ($opt_W and $size < $opt_W)) { | 120 | else { |
104 | $result = 'WARNING'; | 121 | # use permissive defaults for size when none specified |
122 | $opt_W = 0 unless ($opt_W); | ||
123 | $opt_C = 0 unless ($opt_C); | ||
124 | if ($age > $opt_c or $size < $opt_C) { | ||
125 | $result = 'CRITICAL'; | ||
126 | } | ||
127 | elsif ($age > $opt_w or $size < $opt_W) { | ||
128 | $result = 'WARNING'; | ||
129 | } | ||
105 | } | 130 | } |
106 | 131 | ||
132 | $perfdata = "age=${age}s;${opt_w};${opt_c} size=${size}B;${opt_W};${opt_C};0"; | ||
107 | print "FILE_AGE $result: $opt_f is $age seconds old and $size bytes | $perfdata\n"; | 133 | print "FILE_AGE $result: $opt_f is $age seconds old and $size bytes | $perfdata\n"; |
108 | exit $ERRORS{$result}; | 134 | exit $ERRORS{$result}; |
109 | 135 | ||
@@ -121,7 +147,15 @@ sub print_help () { | |||
121 | print "\n"; | 147 | print "\n"; |
122 | print " -i | --ignore-missing : return OK if the file does not exist\n"; | 148 | print " -i | --ignore-missing : return OK if the file does not exist\n"; |
123 | print " <secs> File must be no more than this many seconds old (default: warn 240 secs, crit 600)\n"; | 149 | print " <secs> File must be no more than this many seconds old (default: warn 240 secs, crit 600)\n"; |
124 | print " <size> File must be at least this many bytes long (default: crit 0 bytes)\n"; | 150 | print " <size> File must be at least this many bytes long (default: file size is ignored (0 bytes))\n\n"; |
151 | print " Both <secs> and <size> can specify a range using the standard plugin syntax\n"; | ||
152 | print " If any of the warning and critical arguments are in range syntax (not just bare numbers)\n"; | ||
153 | print " then all warning and critical arguments will be interpreted as ranges.\n"; | ||
154 | print " To use range processing the perl module Monitoring::Plugin must be installed\n"; | ||
155 | print " For range syntax see https://www.monitoring-plugins.org/doc/guidelines.html#THRESHOLDFORMAT\n"; | ||
156 | print " It is strongly recommended when using range syntax that all four of -w, -W, -c and -C are specified\n"; | ||
157 | print " otherwise it is unlikely that the size test will be doing what is desired\n"; | ||
125 | print "\n"; | 158 | print "\n"; |
126 | support(); | 159 | support(); |
127 | } | 160 | } |
161 | |||