I'll take a swing since I have an unhealthy relationship with perl...
The perl script has a few issues:
Missing leading slash there. Should probably be:
The \n here probably should be removed:
I'd suggest adding an 'or die' too:
Code: Select all
open (FILE,">$putFile") or die "I dont wanna make a file called $putFile\n";
Code: Select all
$ftp->cwd($dir) or die "Can't connect to $dir\n";
I doubt this is the issue, but you may want to explicitly define the full path on the ftp server (and change the error from 'cant connect to $dir' to 'cannot change dir to $dir on remote ftp' or similar.
I will assume the dir 'Test' exists in the home directory your ftp server dumps you in when logging in as $user.
This:
I'm not sure this is entirely proper. You are using == for a string comparison; it probably will work because perl is amusing that way. You probably need to be using either 'eq' for your operator or you should convert the date strings back (I like to just convert to epoch seconds so I can use numeric comparison). I doubt this is related to your current issue since it is probably working anyways.
Mostly you need to be handling the get, puts and change of working directories on the FTP side with 'or die' bits so you can get some output when it craters. I tested with the script supplied at the end of this post as the nagios user and I'm getting your expected results. You may want to temporarily enable the nagios user's shell so you can just be nagios when testing:
Set it to /bin/bash (assuming you like /bin/bash), it'll look like this:
Code: Select all
[root@yourserver somedirectory]# chsh nagios
Changing shell for nagios.
New shell [/sbin/nologin]: /bin/bash
Shell changed.
Here is how to put it back when done testing, but set the shell to /sbin/nologin (really, put it back to nologin).
Code: Select all
[root@yourserver somedirectory]# chsh nagios
Changing shell for nagios.
New shell [/bin/bash]: /sbin/nologin
Shell changed.
Changing the shell for the nagios user would let you then just
to become the nagios user to test your scripts and write permissions. (which you get the same info using the 'su -c' method, which is safer; this just might make a permission/ownership issue easier to spot when testing)
I used this to headcheck the script run, confirm that the date was changing, and the modtime/ownerships of the file were correct:
Code: Select all
/usr/local/nagios/libexec/ftp.pl;cat /usr/local/nagios/Misc/test/ftptest;ls -aslht /usr/local/nagios/Misc/test/ftptest
Code: Select all
bash-4.1$ /usr/local/nagios/libexec/ftp.pl;cat /usr/local/nagios/Misc/test/ftptest;ls -aslht /usr/local/nagios/Misc/test/ftptest
OK - FTP Services Working
08/24/2014 02:09
4.0K -rw-r--r-- 1 nagios nagios 17 Aug 24 02:09 /usr/local/nagios/Misc/test/ftptest
(note the 'nagios nagios' in the ownership - if your file has 'root' there, you've likely got a file from one of your test runs you did as 'root' earlier). You may want to try removing /usr/local/nagios/Misc/test/ftptest and then try running the script as the nagios user to see if the file gets written.
Consider not using 'check_ftp' as the name for your custom ftp command (it's one of the samples I see in my conf files). I'd expect you'd be seeing a conflict with the commands when you do your nagios config check if this is your issue. Just for reference, this is the nagios command for check_ftp that I usually see defined:
Code: Select all
# 'check_ftp' command definition
define command{
command_name check_ftp
command_line $USER1$/check_ftp -H $HOSTADDRESS$ $ARG1$
}
Copy of your script with a few updates that worked for me (and a few suggestions in line):
Code: Select all
#!/usr/bin/perl
use Net::FTP;
use Time::Piece;
$host = "myhost";
$user = "myuser";
$pw = "cpass";
$dir = "Test"; #ftpserver remotedir
$getFile = "ftptest";
$getFilelocal = "/usr/local/nagios/Misc/test/ftptest";
$putFile = "/usr/local/nagios/Misc/ftptest";
$date = localtime->strftime('%m/%d/%Y %H:%M');
#Writes current date to file for nagios checking
open (FILE, ">$putFile") or die "I dont wanna make a file called $putFile\n";
print FILE "$date\n"; #if you kill the \n here, you don't need to chomp when you read the file later
close (FILE);
#Connects to FTP directory
$ftp = Net::FTP->new($host) or die "Can't open $host\n";
$ftp->login($user, $pw) or die "Can't login with $user\n";
$ftp->cwd($dir) or die "Can't changedir to $dir on $host\n"; #make this more accurate
#Sends to directory, gets from directory
$ftp->put($putFile) or die "Cannot put file $putfile on $host ",$ftp->message; # homework - change the 'die' messages to 'warn' and modify your exit to '3' so nagios goes unknown
$ftp->get($getFile, "$getFilelocal") or die "Cannot get file $getfile on $host to $getFilelocal", $ftp->message;
#Reads date from file to make sure it matches $date
open FILE, "$getFilelocal" or die $!;
while(<FILE>){
chomp;
$fileOut = $_;
}
#Nagios logic
# - this comparison should probably be 'eq' since this is a string comparison as currently written (better would be to convert the date to epoch)
if ($fileOut == $date) {
print "OK - FTP Services Working\n";
exit 0; #Nagios OK return code
}
else {
print "CRITICAL - FTP services degraded\n";
exit 2; #Nagios CRITICAL return code
}
Edit1: Believe it or not, some rambling removed.
Edit2: A few typos in varnames