#!/usr/local/bin/local_script
# 
# Version 0.0.3
# # added timing
# Version 0.0.2
# # fixed some bugs when using strict
# Version 0.0.1
# # initial version

use Net::FTP;
use Getopt::Long qw(:config no_ignore_case);
use Time::HiRes qw(gettimeofday tv_interval);

my $hostname = 'localhost';
my $username = 'anonymous';
my $password = 'username@domain.com';
my $port = '21';
my $directory = '/';
my $file = '';
my $verbose = '0';
my $passive = '0';
my $timeout = "10";
my $version = "0.0.2";

sub help ();

my $options_okay = GetOptions (
  'hostname|H=s'	=> \$hostname,
  'username|u=s'	=> \$username,
  'password|p=s'	=> \$password,
  'port|P=s'		=> \$port,
  'directory=s'		=> \$directory,
  'file=s'		=> \$file,
  'verbose=i'		=> \$verbose,
  'version'		=> \$version,
  'help|h'		=> sub {help}
);

my $t0 = [ gettimeofday ];
# creating connection
my $ftp = Net::FTP->new("$hostname", Debug => $verbose, Port => $port, Timeout => $timeout, Passive => $passive ) ;
if (!$ftp) {
	print "ERROR: Cannot conect to $hostname\n";
        exit 2;
}
if (!$ftp->login("$username","$password")) { 
  print "ERROR: Sever says: ", $ftp->message;
  exit 2;
}
if ($file eq "") {
  if (!$ftp->ls("$directory")) { 
    print "WARNING: server says: " , $ftp->message;
    exit 1;
  } else  {
    print $ftp->message;
  }
} else {
  if (!$ftp->ls("$file")) { 
    print "CRITICAL: server says: " , $ftp->message;
    exit 2;
  } else {
    #print "DEBUG: Receving file $file\n";
    my $x=$ftp->cwd("$directory");
    $x=$ftp->binary();
    #print "DEBUG: cwd result:", $ftp->message;
    if (!$ftp->get("$file","/tmp/check_ftp")) {
      print "CRITICAL: server says: " , $ftp->message;
      exit 2;
    } else {
      my @message = $ftp->message;
      chomp @message;
	my $t_connect = tv_interval ( $t0, [gettimeofday]);
      print "OK: ", "$message[0] $message[1] (duration: ${t_connect}s)|dl_time=${t_connect}s\n";
      exit 0;
    }
  }
}
$ftp->quit;

sub help () {
        print "Copyright (c) 2005 Joost Veldkamp, nagios at darth dot xs4all dot nl
This plugin checks a ftp server, logins supported. The check downloads a file and returns
OK when the download succeeded.

-H, --hostname=HOST
   Name or IP address of host to check
-u, --username=username
   FTP username
-p --password=password
   FTP Password
-P --port=INTEGER
   TCP port of the FTP server (default 21)
-d --directory=PATH
   Directory on the remote FTP server
-f --file=FILENAME
   Filename of the file on the remote FTP server
-v --verbose=INT
   Print verbose stuff when performing the check, default 0, other for debugging.
-h --help
   Print this message.
-v --version
   Print version number.
";
exit 0;
}

