Created
March 5, 2013 14:45
-
-
Save lazyfrosch/5090757 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env perl | |
# | |
# check_oldestfile_in_directory | |
# | |
# (c) 2013 NETWAYS GmbH | |
# Markus Frosch <markus.frosch@netways.de> | |
# | |
# license GPL2+ | |
# | |
=head1 NAME | |
check_oldestfile_in_directory - check file age of oldest file in a directory | |
when the filecount in that dir exceeds a | |
parameter | |
=head1 SYNOPSIS | |
check_oldestfile_in_directory -d <directory> [-l <filecount>] | |
[-w <minutes>] [-c <minutes>] | |
=head1 OPTIONS | |
=item -d|--directory <directory> | |
Directory to check | |
=item -l|--filecount <filecount> | |
Filecount must be exceeded to check oldestfile for age | |
Default: 10 | |
=item -w|--warnage <minutes> | |
age of the oldest file which would cause a WARNING (default: 60) | |
=item -c|--critage <minutes> | |
age of the oldest file which would cause a CRITICAL (default: 120) | |
=item -h|--help | |
print this help | |
=item -V|--version | |
print version of script | |
=cut | |
use strict; | |
use Getopt::Long qw(:config no_ignore_case bundling); | |
use Pod::Usage; | |
use File::stat; | |
use 5.5.0; | |
our $VERSION = '0.1'; | |
# defaults | |
our $directory; | |
our $filecount = 10; | |
our $agewarn = 60; | |
our $agecrit = 120; | |
# parameters | |
our $opt; | |
GetOptions( | |
"d|directory=s" => \$directory, | |
"l|filecount" => \$filecount, | |
"w|warnage=i" => \$agewarn, | |
"c|critage=i" => \$agecrit, | |
"h|help" => \$opt->{help}, | |
"V|version" => \$opt->{version}, | |
); | |
if (defined $opt->{version}) { print $VERSION."\n"; exit 0; } | |
if (defined $opt->{help} || !defined $directory) { | |
pod2usage(1); | |
} | |
# open directory | |
opendir(my $dh, $directory) || die "couldn't open directory '$directory'!"; | |
chdir($directory) || die "couldn't change to directory '$directory'!"; | |
# read the content and iterate | |
my $counter = 0; | |
my $oldestfile; | |
my $oldestfilestats; | |
while(my $file = readdir($dh)) { | |
# ignore dot files | |
next if $file =~ m/^\./; | |
# ignore subdirs | |
next if -d $file; | |
$counter++; | |
my $stat = stat($file) or die "couldn't stat file '$file'!"; | |
# check if I already found a older file | |
if(!defined $oldestfile || $stat->mtime lt $oldestfilestats->mtime) { | |
# save the data | |
$oldestfile = $file; | |
$oldestfilestats = $stat; | |
} | |
} | |
closedir($dh); | |
# do we have more files than we want | |
if ($counter > $filecount) { | |
# calculate timestamps | |
my $now = time(); | |
my $age = int ($now - $oldestfilestats->mtime); | |
my $agemin = int $age / 60; | |
# critical? | |
if( $agemin >= $agecrit ) { | |
returnstate(2, sprintf("Datei $directory/$oldestfile ist %d Minuten alt!", $agemin), $counter, $age); | |
} | |
# warning? | |
elsif( $agemin >= $agewarn ) { | |
returnstate(1, sprintf("Datei $directory/$oldestfile ist %d Minuten alt!", $agemin), $counter, $age); | |
} | |
# else -> ok | |
else { | |
returnstate(0, "$directory enthaelt $counter Dateien, aber alle juenger als $agewarn Minuten.", $counter, $age); | |
} | |
# only a few files | |
} else { | |
returnstate(0, sprintf("nur %d Dateien in $directory vorhanden.", $counter), $counter, 0); | |
} | |
sub returnstate { | |
my $state = shift; | |
my $message = shift; | |
my $counter = shift; | |
my $oldestage = shift; | |
my $prefix = ""; | |
if($state eq 0) { $prefix = "OK: "; } | |
elsif($state eq 1) { $prefix = "WARNING: "; } | |
elsif($state eq 2) { $prefix = "CRITICAL: "; } | |
else { | |
$prefix = "UNKNOWN: "; | |
$state = 3; | |
} | |
print $prefix.$message; | |
printf(" | files=%d;%d;0 oldestfileage=%ds;%ds;%ds;0s", $counter, $filecount+1, $oldestage, $agewarn * 60, $agecrit * 60); | |
print "\n"; | |
exit $state; | |
} | |
# vi: ts=4 sw=4 expandtab : |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment