#! /usr/bin/perl -w
#
#
# License Information:
# 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; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
############################################################################

use POSIX;
use strict;
use Getopt::Long;
use vars qw($file $currentdate $time $date $filedate $datestring $host $opt_V $opt_f $opt_h $opt_v $verbose $PROGNAME $opt_w $opt_e $opt_c $opt_t $opt_H $status $state $msg $msg_q $MAILQ $SHELL $device $used $avail $fs $blocks $CMD $RMTOS);
use lib  "/usr/local/nagios/libexec" ;
use utils qw(%ERRORS &print_revision &support &usage );
use Date::Manip;

sub print_help ();
sub print_usage ();
sub process_arguments ();

$ENV{'PATH'}='';
$ENV{'BASH_ENV'}=''; 
$ENV{'ENV'}='';
$PROGNAME = "check_disk_remote";

Getopt::Long::Configure('bundling');
$status = process_arguments();

if ($status){
	print "ERROR: processing arguments\n";
	exit $ERRORS{"UNKNOWN"};
}

$SIG{'ALRM'} = sub {
	print ("ERROR: timed out waiting for $CMD on $host\n");
	exit $ERRORS{"WARNING"};
};

$host = $opt_H;

alarm($opt_t);

$CMD="uname";
my $RMTOS = `$SHELL $host $CMD`;
chomp($RMTOS);

if ( $RMTOS eq "HP-UX" ) {
	$CMD = "ls -l";
} elsif ( $RMTOS eq "SunOS" ) {
	$CMD = "ls -l"; 
} elsif ( $RMTOS eq "Linux" ) {
	$CMD = "ls -l --time-style=+%s"; 
} else {
	$CMD = "ls -l"; 
}

alarm($opt_t);

## get cmd output from remote system

# Get Current Date Time from remote box (in case our clocks are not in sync)
if (open (OUTPUT, "$SHELL $host date +%s| " ) ) {
	while (<OUTPUT>) {
		$currentdate = $_;
	}
}
close (OUTPUT); 

my $perfdata = "";
my $msg = "";
my $state = "-1";

if (open (OUTPUT, "$SHELL $host $CMD $opt_f| " ) ) {
	while (<OUTPUT>) {
		my ($modes, $opt, $owner, $group, $size, $filedate, $file) = split;
		my $val = 0;
		if ( $currentdate > $filedate ) {
			$val = $currentdate-$filedate;
		}

		$perfdata = $file . "\=" . "$val;$opt_w;$opt_c;;";

			if ($val < $opt_w) {
				if ( $state <= $ERRORS{'OK'} ) {
					$state = $ERRORS{'OK'};
					$msg = "OK: File age ($val) is below threshold ($opt_w/$opt_c)";
				}
			} elsif ($val >= $opt_w  && $val < $opt_c) {
                                if ( $state <= $ERRORS{'WARNING'} ) {
					$state = $ERRORS{'WARNING'};
					$msg = "WARNING: $file is $val seconds old (threshold w = $opt_w)";
				}
			} else {
                                if ( $state <= $ERRORS{'CRITICAL'} ) {
					$state = $ERRORS{'CRITICAL'};
					$msg = "CRITICAL: $file is $val seconds (threshold c = $opt_c)";
				}
			}
	}
}
## close cmd
close (OUTPUT); 

# declare an error if we also get a non-zero return code from load
# unless already set to critical

if ( $? ) {
	print "stderr = $? : $! \n" if $verbose;
	$state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"UNKNOWN"}  ;
	print "error: $!\n" if $verbose;
}

# Perfdata support
print $msg . " \| " . $perfdata . "\n";
exit $state;

#####################################
#### subs


sub process_arguments(){

	$verbose = 0;

	GetOptions
		("V"   => \$opt_V, "version"	=> \$opt_V,
		 "v"   => \$opt_v, "verbose"	=> \$opt_v,
		 "h"   => \$opt_h, "help"	=> \$opt_h,
		 "e=s" => \$opt_e, "shell=s"	=> \$opt_e,
		 "f=s" => \$opt_f, "file=s"	=> \$opt_f,
		 "w=f" => \$opt_w, "warning=f"  => \$opt_w,   # warning if above this number
		 "c=f" => \$opt_c, "critical=f" => \$opt_c,   # critical if above this number
		 "t=i" => \$opt_t, "timeout=i"  => \$opt_t,
	         "H=s" => \$opt_H, "hostname=s" => \$opt_H
		 );

	if ($opt_V) {
		print_revision($PROGNAME,'$Revision: 1.0 $ ');
		exit $ERRORS{'OK'};
	}

	if ($opt_h) {
		print_help();
		exit $ERRORS{'OK'};
	}

	$verbose = $opt_v if $opt_v;

        if (defined $opt_e ){
		if ( $opt_e eq "ssh" ) {
		    if (-x "/usr/local/bin/ssh") {
			$SHELL = "/usr/local/bin/ssh";
		    } elsif ( -x "/usr/bin/ssh" ) {
			$SHELL = "/usr/bin/ssh"; 
		    } else {
                        print_usage();
                        exit $ERRORS{'UNKNOWN'};
                    }
	
		} elsif ( $opt_e eq "rsh" ) {
			$SHELL = "/usr/bin/rsh";
		} else {
			print_usage();
                	exit $ERRORS{'UNKNOWN'};
		}
        } else {
	   print_usage();
           exit $ERRORS{'UNKNOWN'};
	}


	unless (defined $opt_t) {
		$opt_t = $utils::TIMEOUT ;	# default timeout
	}

	unless (defined $opt_H) {
		print_usage();
		exit $ERRORS{'UNKNOWN'};
	}

	unless (defined $opt_f) {
		print_usage();
		exit $ERRORS{'UNKNOWN'};
	}

	unless (  defined $opt_w &&  defined $opt_c ) {
		print_usage();
		exit $ERRORS{'UNKNOWN'};
	}

	if ( $opt_w >= $opt_c) {
		print "Warning cannot be greater than Critical!\n";
		exit $ERRORS{'UNKNOWN'};
	}

	return $ERRORS{'OK'};
}

sub print_usage () {
	print "Usage: $PROGNAME -e <shell> -H <hostname> [-w <warn>] [-c <crit>] [-t <timeout>] [-v verbose]\n";
}

sub print_help () {
	print_revision($PROGNAME,'$Revision: 1.1 $');
	print "Copyright (c) 2004 jeff scott\n";
	print "\n";
	print_usage();
	print "\n";
	print "   Checks the Disk Usage on remote system over SSH or RSH\n";
	print "-e (--shell)     = ssh or rsh (required)\n";
	print "-H (--hostname)  = remote server name (required)";
	print "-w (--warning)   = Percent Used to generate warning alert\n";
	print "-c (--critical)  = Percent Used to generate critical alert ( w < c )\n";
	print "-t (--timeout)   = Plugin timeout in seconds (default = $utils::TIMEOUT)\n";
	print "-h (--help)\n";
	print "-V (--version)\n";
	print "-v (--verbose)   = debugging output\n";
	print "\n\n";
	support();
}
