#!/usr/bin/perl -wT
## Written 12/5/00 Jeremy Hanmer 
# $Id: check_ftpget.pl,v 1.1.1.1 2002/02/28 06:42:53 egalstad Exp $

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

use Net::FTP;

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

use utils qw(%ERRORS &usage);

$userparameter->default('anonymous');
$passwordparameter->default("$ENV{'LOGNAME'}\@$ENV{'HOSTNAME'}");
$filenameparameter->optional("yes");
$directoryparameter->default("/var/tmp/nagios");
$directoryparameter->optional("yes");
my $plugin = new Plugin(-revision => '$Revision: 1.1.1.1',
			-copyright => "2000 Jeremy Hanmer, 2004 Howard Wilkinson <howard\@cohtech.com>",
			-shortcomment => "Retrieve a file using FTP",
			-parameterlists => [ [ $hostnameparameter,
					       $userparameter,
					       $passwordparameter,
					       $filenameparameter,
					       $directoryparameter,
					       $timeoutparameter,
					       $verboseparameter ],
					     $helpparameterlist,
					     $versionparameterlist ]);

$plugin->init();

$plugin->start_timeout($opt_t, "FTP request timed out");

my $status = 'OK';
my $output = "ftp ok";

my $ftp = Net::FTP->new("$opt_H") || &crit("connect");

$ftp->login("$opt_u", "$opt_p") || &crit("login");

if ($opt_f) {
  if (! -d $opt_d) {
    mkdir $opt_d;
  }
  chdir $opt_d;
  $ftp->get($opt_f, "$$.tmp") || &crit("get");
}

print "$PROGNAME $status: $output\n";
exit $ERRORS{$status};

sub crit() 
{
  my $problem = $_[0];
  $status = 'CRITICAL';
  if ( $problem eq 'connect' ) {
    $output = "can't connect";
  } elsif ( $problem eq 'login' ) {
    $output = "can't log in";
  } elsif ( $problem eq 'get' ) {
    $output = "can't get $opt_f";
  }
}

