#! /usr/bin/perl -w # check_fileage Copyright (C) 2011 Mohan Cheema # # Checks a file's size and modification time to make sure it's not empty # and that it's sufficiently recent. # # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # you should have received a copy of the GNU General Public License # along with this program (or with Nagios); if not, write to the # Free Software Foundation, Inc., 59 Temple Place - Suite 330, # Boston, MA 02111-1307, USA # ============================= SETUP NOTES ==================================== # # Copy this file to your Nagios plugin folder # On a Centos install this is /usr/lib/nagios/plugins or /usr/lib64/nagios/plugins # other distributions may vary. # # NAGIOS SETUP # # 1. Create the check service commands. (Commonly found in nrpe.cfg) # # command[check_abc_full_age]=/usr/lib/nagios/plugins/check_fileage -w 97200 -c 100800 -f /home/backup/full/aws # # 2. In your service config (Commonly found on linux.cfg may vary in your case) add # the service check commands: # #define service{ # use generic-service # host_name localhost # service_description ABC File Age # check_command check_nrpe1!check_noti_full_age # } use strict; use English; use Getopt::Long; use File::stat; use vars qw($PROGNAME); use lib "/usr/lib/nagios/plugins" ; use utils qw (%ERRORS &print_revision &support); sub print_help (); sub print_usage (); my ($opt_c, $opt_f, $opt_w, $opt_C, $opt_W, $opt_h, $opt_V); my ($result, $message, $age, $size, $st, $file, $attrs, $diff, $new); my $temp=0; $PROGNAME="check_fileage"; $opt_w = 240; $opt_c = 600; $opt_W = 0; $opt_C = 0; $opt_f = ""; Getopt::Long::Configure('bundling'); GetOptions( "V" => \$opt_V, "version" => \$opt_V, "h" => \$opt_h, "help" => \$opt_h, "f=s" => \$opt_f, "folder" => \$opt_f, "w=f" => \$opt_w, "warning-age=f" => \$opt_w, "W=f" => \$opt_W, "warning-size=f" => \$opt_W, "c=f" => \$opt_c, "critical-age=f" => \$opt_c, "C=f" => \$opt_C, "critical-size=f" => \$opt_C); if ($opt_V) { print_revision($PROGNAME, '1.0'); exit $ERRORS{'OK'}; } if ($opt_h) { print_help(); exit $ERRORS{'OK'}; } $opt_f = shift unless ($opt_f); # Check that file exists (can be directory or link) unless (-d $opt_f) { print "FILE_AGE CRITICAL: Folder not found - $opt_f\n"; exit $ERRORS{'CRITICAL'}; } my $list = opendir DIRHANDLE, $opt_f or die "Cant open directory: $!"; while ($_ = readdir(DIRHANDLE)) { $file=sprintf("%s/%s",$opt_f,$_); $attrs = stat("$file"); $diff = time()-$attrs->mtime; if($temp == 0) { $temp=$diff; $new=$file; } if($_ ne "." && $_ ne "..") { if($diff<$temp) { $temp=$diff; $new=$_; } else { $temp=$diff; $new=$_; } } } $st = File::stat::stat($opt_f."/".$new); $age = time - $st->mtime; $size = $st->size; $result = 'OK'; if (($opt_c and $age > $opt_c) or ($opt_C and $size < $opt_C)) { $result = 'CRITICAL'; } elsif (($opt_w and $age > $opt_w) or ($opt_W and $size < $opt_W)) { $result = 'WARNING'; } print "FILE_AGE $result: $new is $age seconds old and $size bytes\n"; exit $ERRORS{$result}; sub print_usage () { print "Usage:\n"; print " $PROGNAME [-w ] [-c ] [-W ] [-C ] -f \n"; print " $PROGNAME [-h | --help]\n"; print " $PROGNAME [-V | --version]\n"; } sub print_help () { print_revision($PROGNAME, '1.0'); print "Copyright (c) 2011 Mohan Cheema\n\n"; print_usage(); print "\n"; print " File must be no more than this many seconds old (default: warn 240 secs, crit 600)\n"; print " File must be at least this many bytes long (default: crit 0 bytes)\n"; print "\n"; support(); }