my nagios check_ftp plugin

I use a modified version of a simple perl plugin check_ftp.pl from nagios exchange.
Here is my version:

#!/usr/bin/perl -w 
#
# Version 0.0.3
# # modified by Thierry Randrianiriana
# # used /dev/null
# # added missing exit
# # typos
# 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);

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 = "30";
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}
);

# creating connection
my $ftp = Net::FTP->new("$hostname", Debug => $verbose, Port => $port, Timeout => $timeout, Passive => $passive );
my $err = $@;

if (! defined $ftp) {
    print "ERROR: ", $err;
    exit 2;
}

if (!$ftp->login("$username","$password")) { 
  print "ERROR: server says: ", $ftp->message;
  exit 2;
}

if ($file eq "") {
  if (!$ftp->ls("$directory")) { 
    print "WARNING: server says: " , $ftp->message;
    exit 1;
  } else  {
    print $ftp->message;
    exit 0;
  }
} else {
  if (!$ftp->ls("$file")) { 
    print "WARNING: server says: " , $ftp->message;
    exit 1;
  } else {
    if (!$ftp->get("$file","/dev/null")) {
      print "WARNING: server says: " , $ftp->message;
      exit 1;
    } else {
      my @message = $ftp->message;
      chomp @message;
      print "OK: ", "$message[0] $message[1]\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;
}

Configurer un serveur FTP anonyme avec vsftpd

Ceci est l’installation d’un serveur FTP anonyme avec vsftpd en une minute sur un serveur debian.

# aptitude install vsftpd

# cp /etc/vsftpd.conf /etc/vsftpd.conf.orig

# mkdir  /var/www/ftp

Éditer le fichier /etc/vsftpd.conf et modifier comme suit :

Avec un grep -v '^#' /etc/vsftpd.conf | sed -e '/^$/d' sur mon fichier de config j’ai :


listen=YES
anonymous_enable=YES
local_enable=NO
write_enable=NO
dirmessage_enable=YES
use_localtime=YES
xferlog_enable=YES
connect_from_port_20=NO
ftpd_banner=Welcome to my FTP service.
chroot_local_user=YES
secure_chroot_dir=/var/run/vsftpd/empty
pam_service_name=vsftpd
rsa_cert_file=/etc/ssl/private/vsftpd.pem
max_clients=10
max_per_ip=2
anon_root=/var/www/ftp
anon_upload_enable=NO
anon_mkdir_write_enable=NO
anon_other_write_enable=NO
data_connection_timeout=120
idle_session_timeout=120
connect_timeout=30

La liste complète est sur le site de vsftpd ici . Dans ma config j’ai utilisé le répertoire /var/www/ftp pour mettre mes fichiers à partager, il faut que les répertoires ou les fichiers n’appartiennent pas à l’utilisateur ftp sinon vous allez avoir l’erreur :

Response: 500 OOPS: vsftpd: refusing to run with writable anonymous root

Comment monter un répertoire FTP distant en local

La solution est simple :


# aptitude install curlftpfs
# mkdir /home/thierry/remote_dir
# curlftpfs ftp://myusername:mypass@ftp.ohatra.mg/ /home/thierry/remote_dir
# ls -al /home/thierry/remote_dir/

Et voilà, on peut vérifier :


# df -h
Filesystem Size Used Avail Use% Mounted on
...
curlftpfs 7.5T 0 7.5T 0% /home/thierry/remote_dir

Pour démonter la commande est simple :

fusermount -u /home/thierry/remote_dir