#! /usr/bin/perl -wT

# (c)2001 Patrick Greenwell, Stealthgeeks, LLC. (patrick@stealthgeeks.net)
# Licensed under the GNU GPL
# http://www.gnu.org/licenses/gpl.html
#
# check_dl_size: Attempts to download a specified file via FTP and verify
# it is a specified size. 
# Requires Net::FTP

# Version 1.0
# Last Updated: 8/31/01


require 5.004;
use strict;
use lib utils.pm ;
use Plugin;
use Plugin::Parameter qw($helpparameterlist $versionparameterlist $hostnameparameter
			 $directoryparameter $filenameparameter $filesizeparameter
			 $userparameter $passwordparameter
			 $timeoutparameter $verboseparameter);

use vars qw($opt_H $opt_f $opt_s $opt_t $opt_d $opt_u $opt_p $opt_v $PROGNAME);

use utils qw(%ERRORS &usage);
use Net::FTP;

sub display_res($$);

$directoryparameter->description("Directory to receive download file - don't use /tmp or /var/tmp");
$userparameter->default("anonymous");
$passwordparameter->default("nagios.filecheck\@example.com");
$timeoutparameter->default(120);

my @commandparameterlist = ( $hostnameparameter,
			     $filenameparameter,
			     $directoryparameter,
			     $filesizeparameter,
			     $userparameter,
			     $passwordparameter,
			     $timeoutparameter,
			     $verboseparameter );

my $plugin = new Plugin(-revision => '$Revision: 1.1 $',
			-copyright => " 2001 Patrick Greenwell, Stealthgeeks, LLC, 2004 Howard Wilkinson <howard\@cohtech.com>",
			-shortcomment => "Download a file using FTP and check its size",
			-parameterlists => [ \@commandparameterlist, $helpparameterlist, $versionparameterlist ]);

$plugin->init();

my ($ftpfile, $ftpdir, $filesize) = ();

my $state = $ERRORS{'UNKNOWN'};

delete @ENV{'PATH', 'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

$opt_f =~ m/^(.*?)[\/\\]([^\/\\]+)$/;
$ftpdir = $1;
$ftpfile = $2;

# Make certain temporary directory exists
if ( ! -e "$opt_d" ) {
  display_res("Temporary directory $opt_d does not exist.", "CRITICAL");
}

# Remove existing file if any
if ( -e "$opt_d/$ftpfile") {
  unlink "$opt_d/$ftpfile"
    or display_res("Can't remove existing file $opt_d/$ftpfile.", "CRITICAL");
}

# Snarf file
$plugin->start_timeout($opt_t, "No response from ftp server (alarm)");

my $ftp = Net::FTP->new("$opt_H", Passive => 1, Timeout => $opt_t)
  or display_res("Can't connect to $opt_H.\n", "CRITICAL");
$ftp->login("$opt_u","$opt_p")
  or display_res("Login to $opt_H failed", "CRITICAL");
$ftp->cwd("$ftpdir")
  or display_res("Can't change to directory $ftpdir.", "CRITICAL");
$ftp->get($ftpfile, "$opt_d/$ftpfile")
  or display_res("Can't retrieve file $ftpfile.", "CRITICAL");
$ftp->quit;

$plugin->stop_timeout();

# If file exists and is correct size we are happy.
if (( -e "$opt_d/$ftpfile" ) && (($filesize = -s "$opt_d/$ftpfile") eq $opt_s)) {
  display_res("File $ftpfile size OK: $filesize bytes.", "OK");        
} else {
  # Otherwise we are not happy.
  display_res("File $ftpfile size incorrect: $filesize bytes", "CRITICAL");
}

exit;

sub display_res ($$) {
  my ($answer, $state) = @_;
  print "$PROGNAME $state: $answer\n";;
  exit $ERRORS{$state};
}
