#!/usr/bin/perl 
## $Id: filesize.pl 230 2006-09-30 20:11:05Z touche $
## julien.touche@touche.fr.st
##
## Added use strict, warning, treshold for min size
##
## from http://www.nagiosexchange.org/NRPE_Plugins.66.0.html?&tx_netnagext_pi1[p_view]=81
#################################################
# Small quick and dirty perl example from Larry #
#################################################
# 2013-04-23 shun@wsfong.com
# updated to handle directories
# input filesizes are handled in MB as unit of measurement
# added performance stats
#
# 2013-07-23 shun@wsfong.com
# version 3
# updated to use perl Getopt
# 
#################################################

use strict;
use File::Find;
use Getopt::Long;
my $version=3;
my $opt_help = 0;
my $opt_warn = 0;
my $opt_critical = 0;
my $opt_file = "";
my $exit=0;
my ($maxwarn,$maxcrit,$minwarn,$mincrit);
my $options = GetOptions (
                "help+"      => \$opt_help,      # flag, multiple use allowed
                "warn=s"     => \$opt_warn,      # string (need to handle 1.1:2.3)
                "critical=s" => \$opt_critical,  # string (same as above)
                "file=s"     => \$opt_file,      # string
                );
if ($opt_help != 0) { usage(); }

# check that the file or directory exists otherwise bail
if ( ($opt_file eq "") || (! -f $opt_file && ! -d $opt_file) )
{ 
    print "No valid file or directory: $opt_file\n";
    usage();
}
# check critical numbers
# start duplicate TODO
# can be reworked into a sub
$maxcrit = $opt_critical;
# first lets check for the : to indicate a range of numbers
if ($maxcrit =~ m/([0-9].+):([0-9].+)/)
{
    $mincrit = $1;
    $maxcrit = $2;
    # now lets check both numbers are valid
    # we need to handle whole and real numbers
    if ( ($mincrit !~ m/(\d+)\.(\d+)/) || ($mincrit !~ m/(\d+)/) )
    {
        print "Only whole or real numbers allowed: $mincrit\n";
        usage();
    }
}
if ( ($maxcrit !~ m/(\d+)\.(\d+)/) || ($maxcrit !~ m/(\d+)/) )
{
    print "Only whole or real numbers allowed: $maxcrit\n";
    usage();
}
# check warning numbers
$maxwarn = $opt_warn;
if ($maxwarn =~ m/([0-9].+):([0-9].+)/)
{
    $minwarn = $1;
    $maxwarn = $2;
    # now lets check both numbers are valid
    # we need to handle whole and real numbers
    if ( ($minwarn !~ m/(\d+)\.(\d+)/) || ($minwarn !~ m/(\d+)/) )
    {
        print "Only whole or real numbers allowed: $minwarn\n";
        usage();
    }
}
if ( ($maxwarn !~ m/(\d+)\.(\d+)/) || ($maxwarn !~ m/(\d+)/) )
{
    print "Only whole or real numbers allowed: $maxwarn\n";
    usage();
}
# end duplicate rework TODO

my $size;
if ( -f $opt_file )
{ $size= (-s $opt_file); }
elsif ( -d $opt_file )
{ find(sub{ -f and ( $size += -s ) }, $opt_file ); }
$size = sprintf("%.02f",$size / 1024 / 1024);

my $outputstr;
if ($size>$maxcrit) {
    $outputstr="Critical: Filesize of '$opt_file' too large $size > $maxcrit";$exit=2;
} elsif (defined($mincrit) && $size < $mincrit) {
    $outputstr="Critical: Filesize of '$opt_file' too small $size < $mincrit";$exit=2;
} elsif ($size>$maxwarn) {
    $outputstr="Warning: Filesize of '$opt_file' $size > $maxwarn";$exit=1;
} elsif (defined($minwarn) && $size < $minwarn) {
    $outputstr="Warning: Filesize of '$opt_file' $size < $minwarn";$exit=1;
} else {
    $outputstr="OK: Filesize of '$opt_file' $size"; $exit = 0;
}
# add performance string here
$outputstr="$outputstr MB|size=$size"."MB;$maxwarn;$maxcrit;$minwarn;$mincrit";
$outputstr=$outputstr."\n";
print $outputstr;
exit($exit);
#
sub usage
{
    # usage here
    print "$0 version $version\n";
    print "\nCheck's the size of a file or directory\n";
    print "Examples:\n";
    print "    Usage: $0 --warn=5 --critical=20 --file=c:\\somefile.exe\n";
    print "    Usage: $0 --warn=5 --critical=20 --file=c:\\somedir\n";
    print "    Usage: $0 --warn=0.5:1 --critical=1.1:2.5 --file=c:\\somedir\n";
    print "warn and critical is in MB\n";
    print "file Can be a single file or a directory\n";
    print "Unix paths also supported\n";
    exit(3);
}
