#!/usr/local/groundwork/perl/bin/perl # Nagios Check Plugin # Title: Check FTP Advanced # # Description: This FTP Check is designed to check the age of a specific File, threshold is provided in minutes. # Author: Chrysanth Sulzberger | NextiraOne (Schweiz) GmbH # Version: 1.0 # # Usage: # You can use this plugin to simply check if it's possible to connect successfully to a FTP Server, to be more specific you can additionaly check if a specific file exists. If a file e.g. image export of a we bcam changes periodically you can set a treshold in minutes. use Date::Parse; use Net::FTP; use Getopt::Std; use vars qw($opt_H $opt_u $opt_p $opt_d $opt_f $opt_w $opt_c); getopts("H:u:p:d:f:w:c:"); # Initialise Vars my $host = $opt_H || die "usage: check_ftp.pl -H host [<-u user> <-p pass> <-d dir> <-f file> <-w age in min.> -c age in min.]\n"; my $username = $opt_u || 'anonymous'; my $pass = $opt_p || "$ENV{'LOGNAME'}\@$ENV{'HOSTNAME'}" ; my $dir = $opt_d || '.'; my $file = $opt_f || ''; my $status = 0; my $problem; my $output = "connected successfully as $username"; my $min = 60; my $warning = $opt_w || 0; my $critical = $opt_c || 0; my $time ="00:00"; my $year = ((localtime)[5] +1900); my @line; my $ftp; # Open FTP Connection $ftp = Net::FTP->new($host) or &crit("connect"); # Login with provided Username / Password $ftp->login($username,$pass) or &crit("login"); # Change Directory to provided Directory $ftp->cwd($dir) or &crit("dir"); if ( $file ne '' ) { # Load Line listing in an array @line=$ftp->dir($file) or &crit("list"); # Close Connection $ftp->quit; # Cut away the unwanted parts of the string $line=substr($line[0],42); $line=~ s/^\s+//; $line=~ s/\s+/ /g; # Split the string @date = split(/ /, $line); # Convert the last part of the sting if ($date[2] =~ m/:/) { $time=$date[2]; $date[2]=$year; } # Convert the date into Unix Time $t1 = str2time("$date[0] $date[1] $date[2] $time"); # Actual time $now=time(); # Caculate the time difference in minutes $mins = ($now - $t1) / $min; # Round the Result $mins = sprintf ("%.0f", $mins); } # sub crit() { $problem = $_[0]; $status = 2; if ( $problem eq 'connect' ) { $output = " can't connect to $host"; } elsif ( $problem eq 'login' ) { $output = "can't log as $username"; } elsif ( $problem eq 'dir' ) { $output = "can't open $dir"; } elsif ( $problem eq 'list' ) { $output = "$file does not exist"; } } if (( $critical != 0 ) && ( $critical < $mins )) { $status = 2; $output = "$file changed $mins minutes ago"; } elsif (( $warning!= 0 ) && ( $warning < $mins )) { $status = 1; $output = "$file changed $mins minutes ago"; } elsif (( $critical != 0 ) || ( $warning !=0 ) && ( $file ne '' )) { $status = 0; $output = "$file changed $mins minutes ago"; } elsif ( $file ne '' ) { $status = 0; $output = "$file exists"; } if($status == 0 ) { $info="OK:"; } elsif ($status == 1 ) { $info="WARNING:"; } elsif ( $status == 2 ) { $info="CRITICAL:"; } print "$info $output\n"; exit $status;