Skip to content

Commit

Permalink
check_file_age: support for --ignore-missing
Browse files Browse the repository at this point in the history
Patch to provide for a -i | --ignore-missing option. This allows files that
don't exist to be monitored for without causing an error. The use case is:

* one process periodically pushes files to an "input" queue for a second
  process
* the second process, when running cleanly will "consume" (process and remove)
  these files

So the conditions to meet are:
1. If the file doesn't exist, that's fine (OK)
2. If the file exists, but is less than X seconds, that's fine (OK)
3. If the file exists, but greater than X seconds, no good (CRITICAL)

Since now check_file_age doesn't permit this behavior, as a non-existent file
is considered CRITICAL.

Test case was contributed by our lovely Holger.

Closes #1181, Closes #989, Closes #862, Closes #1187, Closes #845
Closes Debian Bug #496307
  • Loading branch information
waja committed Jan 24, 2014
1 parent 455fe96 commit 0b9b300
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
17 changes: 13 additions & 4 deletions plugins-scripts/check_file_age.pl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
sub print_help ();
sub print_usage ();

my ($opt_c, $opt_f, $opt_w, $opt_C, $opt_W, $opt_h, $opt_V);
my ($opt_c, $opt_f, $opt_w, $opt_C, $opt_W, $opt_h, $opt_V, $opt_i);
my ($result, $message, $age, $size, $st);

$PROGNAME="check_file_age";
Expand All @@ -46,6 +46,7 @@
GetOptions(
"V" => \$opt_V, "version" => \$opt_V,
"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,
Expand All @@ -71,8 +72,15 @@

# Check that file exists (can be directory or link)
unless (-e $opt_f) {
print "FILE_AGE CRITICAL: File not found - $opt_f\n";
exit $ERRORS{'CRITICAL'};
if ($opt_i) {
$result = 'OK';
print "FILE_AGE $result: $opt_f doesn't exist, but ignore-missing was set\n";
exit $ERRORS{$result};

} else {
print "FILE_AGE CRITICAL: File not found - $opt_f\n";
exit $ERRORS{'CRITICAL'};
}
}

$st = File::stat::stat($opt_f);
Expand All @@ -94,7 +102,7 @@

sub print_usage () {
print "Usage:\n";
print " $PROGNAME [-w <secs>] [-c <secs>] [-W <size>] [-C <size>] -f <file>\n";
print " $PROGNAME [-w <secs>] [-c <secs>] [-W <size>] [-C <size>] [-i] -f <file>\n";
print " $PROGNAME [-h | --help]\n";
print " $PROGNAME [-V | --version]\n";
}
Expand All @@ -104,6 +112,7 @@ ()
print "Copyright (c) 2003 Steven Grimm\n\n";
print_usage();
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 "\n";
Expand Down
7 changes: 6 additions & 1 deletion plugins-scripts/t/check_file_age.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#

use strict;
use Test::More tests => 15;
use Test::More tests => 16;
use NPTest;

my $successOutput = '/^FILE_AGE OK: /';
Expand Down Expand Up @@ -57,6 +57,11 @@ $result = NPTest->testCmd(
);
cmp_ok( $result->return_code, '==', 0, "Checking file size" );

$result = NPTest->testCmd(
"./check_file_age -f /non/existent --ignore-missing"
);
cmp_ok( $result->return_code, '==', 0, "Honours --ignore-missing" );

$result = NPTest->testCmd(
"./check_file_age -f $temp_file -c 1000 -W 101"
);
Expand Down

1 comment on commit 0b9b300

@waja
Copy link
Member Author

@waja waja commented on 0b9b300 Jan 24, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.