#! /usr/bin/perl -w # check_file_age_fmt Copyright (C) 2014 Tim Kiselev # based on # check_file_age.pl Copyright (C) 2003 Steven Grimm # # 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., 51 Franklin Street, Fifth Floor, # Boston, MA 02110-1301, USA # $Id: check_file_age_fmt 664 2014-06-03 16:37:21Z ikk $ use strict; use English; use Getopt::Long; use File::stat; use vars qw($PROGNAME); use lib "/usr/local/nagios/libexec" ; use utils qw (%ERRORS &print_revision &support); sub print_help (); sub print_usage (); sub convert_time; 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_fmt"; $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, "i" => \$opt_i, "ignore-missing" => \$opt_i, "f=s" => \$opt_f, "file" => \$opt_f, "w=s" => \$opt_w, "warning-age=s" => \$opt_w, "W=f" => \$opt_W, "warning-size=f" => \$opt_W, "c=s" => \$opt_c, "critical-age=s" => \$opt_c, "C=f" => \$opt_C, "critical-size=f" => \$opt_C); if ($opt_V) { print_revision($PROGNAME, '2.0.2'); exit $ERRORS{'OK'}; } if ($opt_h) { print_help(); exit $ERRORS{'OK'}; } $opt_f = shift unless ($opt_f); if (! $opt_f) { print "FILE_AGE UNKNOWN: No file specified\n"; exit $ERRORS{'UNKNOWN'}; } # Format times $opt_w = convert_time($opt_w); $opt_c = convert_time($opt_c); # Check that file exists (can be directory or link) unless (-e $opt_f) { 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); $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'; } # Process the seconds since file mod into human-readable format my ($age_string, $age_override); $age_string = ""; $age_override = 0; if (int($age / 86400)) { $age_string .= int($age / 86400) . "d "; $age %= 86400; $age_override = 1; } if ($age_override or int($age / 3600)) { $age_string .= int($age / 3600) . "h "; $age %= 3600; $age_override = 1; } if ($age_override or int($age / 60)) { $age_string .= int($age / 60) . "m "; $age %= 60; } $age_string .= $age . "s"; # Convert size bytes into human-readable unit my ($size_string, $size_scale, @size_unit); $size_string = ""; $size_scale = 1; @size_unit = ("B", "KB", "MB", "GB", "TB"); $size_scale++ until ($size < 1024 ** $size_scale); $size_scale--; if ($size_scale) { $size_string = sprintf("%.2f ", $size / (1024 ** $size_scale)) . $size_unit[$size_scale]; } else { $size_string = "$size B"; } # Print result of comparison print "FILE_AGE $result: $opt_f is $age_string old and $size_string\n"; exit $ERRORS{$result}; # Converts string of [d][h][m][s] to seconds sub convert_time (\$) { my $time_long = shift(@_); my $time_sec = 0; if ($time_long and $time_long =~ /^(?:\d+d)?(?:\d+h)?(?:\d+m)?(?:\d+s)?$/i) { $time_sec += $1 * 86400 if ($time_long =~ /(\d+)d/i); $time_sec += $1 * 3600 if ($time_long =~ /(\d+)h/i); $time_sec += $1 * 60 if ($time_long =~ /(\d+)m/i); $time_sec += $1 if ($time_long =~ /(\d+)s/i); return $time_sec; } else { unless ($time_long =~ /^(\d+)$/) { $result = 'CRITICAL'; print "FILE_AGE $result: Could not parse time string $time_long\n"; exit $ERRORS{$result}; } return $time_long; } } sub print_usage () { print "Usage:\n"; print " $PROGNAME [-w