summaryrefslogtreecommitdiffstats
path: root/plugins-scripts/check_file_age.pl
diff options
context:
space:
mode:
authorRincewindsHat <12514511+RincewindsHat@users.noreply.github.com>2021-12-02 15:42:05 (GMT)
committerGitHub <noreply@github.com>2021-12-02 15:42:05 (GMT)
commit911e44045d7291f5ede22739fd176ef55dd3de4a (patch)
treecf36b95a4a964b03d6ecf75770ced2cb3a2ac3a9 /plugins-scripts/check_file_age.pl
parent8294af907bd8482a86df749f562b7ec09e3faeed (diff)
parented7cdf82a42f16532801ea4f118870ce9a130fcf (diff)
downloadmonitoring-plugins-911e44045d7291f5ede22739fd176ef55dd3de4a.tar.gz
Merge branch 'master' into fix/shellcheckrefs/pull/1459/head
Diffstat (limited to 'plugins-scripts/check_file_age.pl')
-rwxr-xr-xplugins-scripts/check_file_age.pl61
1 files changed, 48 insertions, 13 deletions
diff --git a/plugins-scripts/check_file_age.pl b/plugins-scripts/check_file_age.pl
index 56b8e97..26281dd 100755
--- a/plugins-scripts/check_file_age.pl
+++ b/plugins-scripts/check_file_age.pl
@@ -43,8 +43,6 @@ $ENV{'ENV'}='';
43 43
44$opt_w = 240; 44$opt_w = 240;
45$opt_c = 600; 45$opt_c = 600;
46$opt_W = 0;
47$opt_C = 0;
48$opt_f = ""; 46$opt_f = "";
49 47
50Getopt::Long::Configure('bundling'); 48Getopt::Long::Configure('bundling');
@@ -53,10 +51,10 @@ GetOptions(
53 "h" => \$opt_h, "help" => \$opt_h, 51 "h" => \$opt_h, "help" => \$opt_h,
54 "i" => \$opt_i, "ignore-missing" => \$opt_i, 52 "i" => \$opt_i, "ignore-missing" => \$opt_i,
55 "f=s" => \$opt_f, "file" => \$opt_f, 53 "f=s" => \$opt_f, "file" => \$opt_f,
56 "w=f" => \$opt_w, "warning-age=f" => \$opt_w, 54 "w=s" => \$opt_w, "warning-age=s" => \$opt_w,
57 "W=f" => \$opt_W, "warning-size=f" => \$opt_W, 55 "W=s" => \$opt_W, "warning-size=s" => \$opt_W,
58 "c=f" => \$opt_c, "critical-age=f" => \$opt_c, 56 "c=s" => \$opt_c, "critical-age=s" => \$opt_c,
59 "C=f" => \$opt_C, "critical-size=f" => \$opt_C); 57 "C=s" => \$opt_C, "critical-size=s" => \$opt_C);
60 58
61if ($opt_V) { 59if ($opt_V) {
62 print_revision($PROGNAME, '@NP_VERSION@'); 60 print_revision($PROGNAME, '@NP_VERSION@');
@@ -91,18 +89,47 @@ unless (-e $opt_f) {
91$st = File::stat::stat($opt_f); 89$st = File::stat::stat($opt_f);
92$age = time - $st->mtime; 90$age = time - $st->mtime;
93$size = $st->size; 91$size = $st->size;
94$perfdata = "age=${age}s;${opt_w};${opt_c} size=${size}B;${opt_W};${opt_C};0";
95
96 92
97$result = 'OK'; 93$result = 'OK';
98 94
99if (($opt_c and $age > $opt_c) or ($opt_C and $size < $opt_C)) { 95if ($opt_c !~ m/^\d+$/ or ($opt_C and $opt_C !~ m/^\d+$/)
100 $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 }
101} 119}
102elsif (($opt_w and $age > $opt_w) or ($opt_W and $size < $opt_W)) { 120else {
103 $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 }
104} 130}
105 131
132$perfdata = "age=${age}s;${opt_w};${opt_c} size=${size}B;${opt_W};${opt_C};0";
106print "FILE_AGE $result: $opt_f is $age seconds old and $size bytes | $perfdata\n"; 133print "FILE_AGE $result: $opt_f is $age seconds old and $size bytes | $perfdata\n";
107exit $ERRORS{$result}; 134exit $ERRORS{$result};
108 135
@@ -120,7 +147,15 @@ sub print_help () {
120 print "\n"; 147 print "\n";
121 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";
122 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";
123 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";
124 print "\n"; 158 print "\n";
125 support(); 159 support();
126} 160}
161