Skip to content

Instantly share code, notes, and snippets.

@cou929
Created December 27, 2012 08:34
Show Gist options
  • Save cou929/4386609 to your computer and use it in GitHub Desktop.
Save cou929/4386609 to your computer and use it in GitHub Desktop.
ファイルの更新を監視する nagios plugin
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use File::Find::Rule;
use File::Basename;
my $STATE_OK = 0;
my $STATE_WARNING = 1;
my $STATE_CRITICAL = 2;
my $STATE_UNKNOWN = 3;
my $STATE_DEPENDENT = 4;
my $target_dir = '/tmp';
my $file_pattern = '*foo.bar.*';
my @excludes = qw/baz blah/;
sub usage {
print
"usage: check_file_update.pl -c <interval seconds of critical> -c <interval seconds of warning>\n\n";
print "options:\n";
print " -c, --critical=<INTEGER>\n";
print " -w, --warning=<INTEGER>\n";
exit $STATE_UNKNOWN;
}
my $opt = sub {
my %opt;
my @options = ( \%opt, qw/help critical=i warning=i/ );
GetOptions(@options) or usage();
\%opt;
}
->();
my $critical_sec = $opt->{critical} or usage();
my $warning_sec = $opt->{warning} or usage();
my @targets = list_files();
my @criticals;
my @warnings;
for my $target (@targets) {
my $got = get_sec_from_last_mtime($target);
push @criticals, basename $target if $got > $critical_sec;
push @warnings, basename $target if $got > $warning_sec;
}
if (@criticals) {
print join( ", ", @criticals )
. " has not been updated $critical_sec seconds.\n";
exit $STATE_CRITICAL;
}
if (@warnings) {
print join( ", ", @warnings )
. " has not been updated $warning_sec seconds.\n";
exit $STATE_WARNING;
}
print "files are updated\n";
exit $STATE_OK;
sub list_files {
my $rule = File::Find::Rule->new;
$rule->file;
$rule->name($file_pattern);
$rule->not_name( map { "*$_*" } @excludes );
$rule->in($target_dir);
}
sub get_sec_from_last_mtime {
my $filename = shift;
my $now = time;
my $mtime = ( stat $filename )[9];
$now - $mtime;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment