File check_file-notexist of Package monitoring-plugins-file-notexist
#!/usr/bin/perl -w
# nagios: -epn
#
# check_file_notexist - nagios plugin
# Author: Ruediger Oertel
#
# Based on: check_contentage
# Copyright (C) 2009, Novell, Inc.
# Author: Lars Vogdt
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the Novell nor the names of its contributors may be
# used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# $Id$
#
use lib "/usr/lib/nagios/plugins";
use utils qw{$TIMEOUT %ERRORS print_revision};
use Time::HiRes qw{time alarm};
use strict;
use warnings;
use Getopt::Long;
use File::Basename;
use File::stat;
use POSIX qw(strftime);
our $version="0.1";
our @pathnames=qw();
our @blacklist_w=qw();
our @blacklist_c=qw();
our $help=0;
our $rev=0;
our $errorstr="";
our $DEBUG=0;
our %status;
# nagios requires a 3 for unknown errors.
$SIG{__DIE__} = sub {
print @_;
exit 3;
};
# Just in case of problems, let's not hang Nagios
$SIG{'ALRM'} = sub {
print "UNKNOWN - Plugin timed out\n";
exit $ERRORS{"UNKNOWN"};
};
alarm($TIMEOUT);
sub print_usage {
print "This plugin checks one or more directory for files matching a blacklist.\n\n";
print "Usage: ".basename($0)." -p /tmp -w foo -b bar\n";
print "Options:\n";
print " -p|--pathnames : absolute path to the directory\n";
print " -w|--blacklistw : string that triggers critical status\n";
print " if found as part of the filename\n";
print " -b|--blacklist : string that triggers critical status\n";
print " if found as part of the filename\n";
}
sub print_help {
my $exitcode=shift || 1;
print "Copyright (c) 2010, Novell, Inc.\n\n";
print_usage();
print "\n";
exit $exitcode;
}
sub print_error {
my $error=shift || '';
print STDERR "\nERROR: $error\n\n";
&print_usage;
exit $ERRORS{'UNKNOWN'};
}
sub check_dir($$$){
my $dir = shift;
my $warnlist = shift;
my $critlist = shift;
my %res;
$res{'level'}=$ERRORS{'OK'};
$res{'errorstr'}="";
if (opendir(DIR,"$dir")){
print "Working in $dir\n" if ($DEBUG);
for my $file (readdir(DIR)) {
next if (! -f "$dir/$file");
for (@{$warnlist}) {
if ($file =~ /$_/) {
$res{'level'}=$ERRORS{'WARNING'} if ($res{'level'} < $ERRORS{'WARNING'});
$res{'errorstr'}.="$dir/$file matches warning blacklist\n";
}
}
for (@{$critlist}) {
if ($file =~ /$_/) {
$res{'level'}=$ERRORS{'CRITICAL'};
$res{'errorstr'}.="$dir/$file matches critical blacklist\n";
}
}
}
closedir(DIR);
} else {
$res{'level'}=$ERRORS{'WARNING'};
$res{'errorstr'}="$dir not found or not readable";
}
return \%res;
}
Getopt::Long::Configure('bundling');
if(!GetOptions( 'p|pathname=s' => \@pathnames,
'w|blacklistw=s' => \@blacklist_w,
'b|blacklist=s' => \@blacklist_c,
'd|debug' => \$DEBUG,
'h|help' => \$help,
'v|version' => \$rev,
)){
&print_help(1);
}
&print_help(0) if ($help);
if ($rev){
&print_revision(basename($0),$version);
exit $ERRORS{'UNKNOWN'};
}
print_error("Please provide at least one pathname") if (!defined($pathnames[0]) || ( $pathnames[0] eq "" ));
@pathnames=split(/,/,join(',',@pathnames));
@pathnames=map { glob($_) } @pathnames;
print STDERR "PATHNAMES: ".join(" ",@pathnames)."\nBLACKLIST (warning): ".join(" ",@blacklist_w)."\nBLACKLIST (critical): ".join(" ",@blacklist_c)."\n" if ($DEBUG);
foreach my $path (@pathnames){
$status{"$path"}=check_dir("$path",\@blacklist_w,\@blacklist_c);
}
our $exitcode=0;
foreach my $path (sort(keys %status)){
if ((defined($status{$path}{'level'})) && ($status{$path}{'level'} > $exitcode)){
$exitcode=$status{$path}{'level'};
}
if ((defined($status{$path}{'errorstr'})) && ($status{$path}{'errorstr'} ne '')){
$errorstr .= $status{$path}{'errorstr'};
}
}
if ($exitcode){
if ( $exitcode == $ERRORS{'WARNING'}){
print "WARNING: Found files matching warning blacklist\n";
} elsif ( $exitcode == $ERRORS{'CRITICAL'}){
print "CRITICAL: Found files matching critical blacklist\n";
}
print "$errorstr";
exit $exitcode;
} else {
print "OK: Tested ".join(" ",@pathnames)." - no files matching blacklists found\n";
exit $ERRORS{"OK"};
}