#!/usr/bin/perl -w
############### check_filesize.pl #######################
# Version : 1.0
# Date    : 27 December 2011
# Author  : De Bodt Lieven (Lieven.DeBodt at gmail.com)
# Licence : GPL - http://www.fsf.org/licenses/gpl.txt
#############################################################
#
# help : ./check_apachestatus.pl -h

use strict;
use Getopt::Long;

# Nagios specific
my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);

# Globals

my $Version='1.0';
my $Name=$0;

my $o_filename =			undef; 	# filename 
my $o_help =				undef; 	# want some help ?
my $o_version = 			undef;  # print version
my $o_min_warn_level =		undef;  # minimum warning threshold
my $o_min_crit_level =		undef;  # minimum critical threshold
my $o_max_warn_level =		undef;  # maximum warning threshold
my $o_max_crit_level =		undef;  # maximum critical threshold
my $o_critifmissing =		undef;  # what to do if file is missing
my $o_okifmissing =			undef;  # what to do if file is missing
my $o_unknownifmissing = 	undef;  # what to do if file is missing
my $FileSize =				undef;	# file size

########## FUNCTIONS ##########

sub show_versioninfo { print "$Name version : $Version\n"; }

sub print_usage {
  print "Usage: $Name -F <filename> [-w <min_warn_level>] [-c <min_crit_level>] [-W <max_warn_level>] [-C <max_crit_level>] [-m] [-o] [-u] [-V]\n\n";
}

sub help {
	print "File size monitor for Nagios version ",$Version,"\n";
	print "GPL licence, (c)2006-2011 De Bodt Lieven\n\n";
	print_usage();
	print <<EOT;
-h, --help
	Print this help message
-F, --filename=filename
	File to check
-w, --minwarn=size in bytes
	Minimum filesize warning threshold
-c, --mincrit=size in bytes
	Minimum filesize critical threshold
-W, --maxwarn=size in bytes
	Maximum filesize warning threshold
-C, --maxcrit=size in bytes
	Maximum filesize warning threshold
-m, --critifmissing
	Generate a critical alert if the file doesn't exist
-o, --okifmissing
	Don't generate a alert if the file doesn't exist
-u, --unknownifmissing
	Generate a unknown alert if the file doesn't exist
-V, --version
	Prints version number

EOT
}

sub check_options {
	Getopt::Long::Configure ("bundling");
	GetOptions(
	'h'     => \$o_help,        		'help'				=> \$o_help,
	'F:s'   => \$o_filename,    		'filename:s'		=> \$o_filename,
	'V'     => \$o_version,     		'version'			=> \$o_version,
	'w:i'   => \$o_min_warn_level,		'minwarn:i'			=> \$o_min_warn_level,
	'c:i'   => \$o_min_crit_level,		'mincrit:i'			=> \$o_min_crit_level,
	'W:i'   => \$o_max_warn_level,		'maxwarn:i'			=> \$o_max_warn_level,
	'C:i'   => \$o_max_crit_level,		'maxcrit:i'			=> \$o_max_crit_level,
	'm'     => \$o_critifmissing,       'critifmissing'		=> \$o_critifmissing,
	'o'     => \$o_okifmissing,     	'okifmissing'		=> \$o_okifmissing,
	'u'     => \$o_unknownifmissing,	'unknownifmissing'	=> \$o_unknownifmissing
	);

	if (defined($o_help)) { help(); exit $ERRORS{"UNKNOWN"}};
	if (defined($o_version)) { show_versioninfo(); exit $ERRORS{"UNKNOWN"}};

	if (!defined($o_filename)) {print "Please define the file to check!\n\n"; print_usage(); exit $ERRORS{"UNKNOWN"}};
	
	# Check if the option for what to do if the file is missing is correctly specified
	my $CheckOptionOK = 0;
	if (defined($o_critifmissing)) 		{$CheckOptionOK = $CheckOptionOK + 1};
	if (defined($o_okifmissing)) 		{$CheckOptionOK = $CheckOptionOK + 1};
	if (defined($o_unknownifmissing))	{$CheckOptionOK = $CheckOptionOK + 1};
	if ($CheckOptionOK != 1) {print "Please specify what to do if the file is missing!\n\n"; print_usage(); exit $ERRORS{"UNKNOWN"}};
	
	# Check if at least 1 warn or critical level is specified
	if (!defined($o_min_warn_level) && !defined($o_min_crit_level) && !defined($o_max_warn_level) && !defined($o_max_crit_level)) {print "Please define at least a warning or critical level!\n\n"; print_usage(); exit $ERRORS{"UNKNOWN"}};
	
	# Check if the thresholds are correctly filled in
	if ((defined($o_min_warn_level) && defined($o_min_crit_level)) && ($o_min_crit_level > $o_min_warn_level)) {print "Check minimum warning and critical level!\n\n"; print_usage(); exit $ERRORS{"UNKNOWN"}};
	if ((defined($o_max_warn_level) && defined($o_max_crit_level)) && ($o_max_crit_level < $o_max_warn_level)) {print "Check maximum warning and critical level!\n\n"; print_usage(); exit $ERRORS{"UNKNOWN"}};
	if ((defined($o_min_warn_level) && defined($o_max_warn_level)) && ($o_min_warn_level >= $o_max_warn_level)) {print "Check minimum and maximum warning level!\n\n"; print_usage(); exit $ERRORS{"UNKNOWN"}};
	if ((defined($o_min_crit_level) && defined($o_max_crit_level)) && ($o_min_crit_level >= $o_max_crit_level)) {print "Check minimum and maximum critical level!\n\n"; print_usage(); exit $ERRORS{"UNKNOWN"}};
}

########## MAIN ##########

# Get the alarm signal
$SIG{'ALRM'} = sub {
  print ("ERROR: Alarm signal (Nagios time-out)\n");
  exit $ERRORS{"CRITICAL"};
};

check_options();

my $output = "";
my $perfoutput = "";

if (-e $o_filename) {
	# The file exists
	$FileSize = -s "$o_filename";
	$output = "$o_filename: $FileSize Bytes";
	$perfoutput = "\'$o_filename\'=$FileSize" . ";";
	if (defined($o_min_warn_level)) {
		$perfoutput = $perfoutput . $o_min_warn_level . ";";
	}
	else {
		$perfoutput = $perfoutput . "0;";
	}
	if (defined($o_min_crit_level)) {
		$perfoutput = $perfoutput . $o_min_crit_level . ";";
	}
	else {
		$perfoutput = $perfoutput . "0;";
	}
	if (defined($o_max_warn_level)) {
		$perfoutput = $perfoutput . $o_max_warn_level . ";";
	}
	else {
		$perfoutput = $perfoutput . "0;";
	}
	if (defined($o_max_crit_level)) {
		$perfoutput = $perfoutput . $o_max_crit_level;
	}
	else {
		$perfoutput = $perfoutput . "0";
	}
	
	if (defined($o_max_crit_level) && ($FileSize >= $o_max_crit_level)) {
		print $output . " : CRITICAL | " . $perfoutput . "\n";
		exit $ERRORS{"CRITICAL"}
	}
	elsif (defined($o_max_warn_level) && ($FileSize >= $o_max_warn_level)) {
		print $output . " : WARNING | " . $perfoutput . "\n";
		exit $ERRORS{"WARNING"}
	}
	elsif (defined($o_min_crit_level) && ($FileSize <= $o_min_crit_level)) {
		print $output . " : CRITICAL | " . $perfoutput . "\n";
		exit $ERRORS{"CRITICAL"}
	}
	elsif (defined($o_min_warn_level) && ($FileSize <= $o_min_warn_level)) {
		print $output . " : WARNING | " . $perfoutput . "\n";
		exit $ERRORS{"WARNING"}
	}
	else {
		print $output . " : OK | " . $perfoutput . "\n";	
		exit $ERRORS{"OK"}
	}
}
else {
	$output = "The file \"$o_filename\" does not exist!";
	$perfoutput = "\'$o_filename\'=0" . ";";
	if (defined($o_min_warn_level)) {
		$perfoutput = $perfoutput . $o_min_warn_level . ";";
	}
	else {
		$perfoutput = $perfoutput . "0;";
	}
	if (defined($o_min_crit_level)) {
		$perfoutput = $perfoutput . $o_min_crit_level . ";";
	}
	else {
		$perfoutput = $perfoutput . "0;";
	}
	if (defined($o_max_warn_level)) {
		$perfoutput = $perfoutput . $o_max_warn_level . ";";
	}
	else {
		$perfoutput = $perfoutput . "0;";
	}
	if (defined($o_max_crit_level)) {
		$perfoutput = $perfoutput . $o_max_crit_level;
	}
	else {
		$perfoutput = $perfoutput . "0";
	}
	
	if (defined($o_critifmissing)) {
		print $output . " : CRITICAL | " . $perfoutput . "\n";
		exit $ERRORS{"CRITICAL"};
	}
	if (defined($o_okifmissing)) {
		print $output . " : OK | " . $perfoutput . "\n";
		exit $ERRORS{"OK"};
	}
	if (defined($o_unknownifmissing)) {
		print $output . " : UNKNOWN | " . $perfoutput . "\n";
		exit $ERRORS{"UNKNOWN"};
	}
}