#!/usr/bin/perl use strict; use Getopt::Std; my %opt = (); getopts("hd:w:c:", \%opt); my $dir = $opt{'d'}; my $w = $opt{'w'}; my $c = $opt{'c'}; my @files; my $size; my $fullpath; my %filesizes; my $key; my $retval = 0; my $output; my $critfiles; my $warnfiles; my $meg; if( not ($w && $c && $dir) or $opt{'h'} ) { print "check_filesize_dir Copyright by Christian Reiter \n This nagios plugin comes with ABSOLUTELY NO WARRANTY. You may redistribute copies of this plugin under the terms of the GNU General Public License. For more information about these matters, see the file named COPYING. Copyright (c) 2006 by Christian Reiter Check Size of all Files in a Directory Useful for Mailboxes and similar files\n Usage: ./check_filesize_dir -d -w -c -d the Directory to check (example: /var/spool/mail) -c Critical treshold in MB (example: 100) -w Warning treshold in MB (example: 75) -h displays the Help message Filesizes are in Megabytes\n\n"; exit 3; }; unless( $w <= $c ) { print "Error: Warninglevel must not be higher than critical\n\n"; exit 3; }; $w = $w * 1024 * 1024; $c = $c * 1024 * 1024; opendir(DIR, $dir); @files = readdir(DIR); closedir(DIR); foreach my $file (@files) { $fullpath = $dir . '/' . $file; $size = (-s $fullpath); $filesizes{$fullpath} = $size; } foreach $key ( sort hashValueDescendingNum (keys(%filesizes))) { #print "$key: \t $filesizes{$key}\n"; if( $filesizes{$key} >= $c ) { $retval = 2; $meg = $filesizes{$key} / 1024 / 1024; $meg = sprintf ("%.2f", $meg); $critfiles .= " $key ($meg MB) "; next; }; if( $filesizes{$key} >= $w ) { $retval = 1 if $retval < 2; $meg = $filesizes{$key} / 1024 / 1024; $meg = sprintf ("%.2f", $meg); $warnfiles .= " $key ($meg MB) "; }; }; if ( $retval == 0 ) { print "OK: No files to big\n"; exit $retval; } elsif ( $retval == 1 ) { print "WARNING: These files are over WARNING Size: $warnfiles\n"; exit $retval; } elsif ( $retval == 2 ) { print "CRITICAL: These files are over CRITICAL Size: $critfiles These files are over WARNING Size: $warnfiles \n"; exit $retval; }; exit $retval; sub hashValueDescendingNum { $filesizes{$b} <=> $filesizes{$a}; }