"Cannot find file" error on host

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
logic_bomb421
Posts: 43
Joined: Tue Jul 15, 2014 6:58 pm

"Cannot find file" error on host

Post by logic_bomb421 »

So I wrote a perl script that connects to an FTP server, uploads a file, downloads the same file, and checks it for previously written stuff. The whole idea is to make sure people can upload and download. When I tested it via the terminal, I got the correct outputs for OK and CRITICAL, depending on my tests. For some reason, once I added it to nagios, I'm getting the error:

Code: Select all

(No output on stdout) stderr: execvp(/usr/local/nagios/libexec/ftp.pl, ...) failed. errno is 2: No such file or directory
This makes no sense to me because I'm 100% sure the file is there, WITH all the permissions it needs. I even ran the file from the terminal using the path specified in the error and got normal results.

I've posted copies of my command, host, and template files below, along with the script itself. What in the world could be causing this?

Host definition:

Code: Select all

define host{
	use		       transfer-template
	host_name	    FTP Health Check
	alias		     FTP Health Check
	check_command	check_ftp
	}
Template definition:

Code: Select all

define host{
	name			           transfer-template	; The name of this host template
	use			            generic-host	; Inherit default values from the generic-host template
	check_period		      24x7		; By default, Windows servers are monitored round the clock
	check_interval		    1		; Actively check the server every 1 minutes
	retry_interval		    0.5		; Schedule host check retries at 0.5 minute intervals
	max_check_attempts	   2		; Check each server 2 times (max)
	notification_period	  24x7		; Send notification out at any time - day or night
	notification_interval	10		; Resend notifications every 10 minutes
	notification_options	 d,r		; Only send notifications for specific host states
	contact_groups		    admins		; Notifications get sent to the admins by default
	hostgroups		        2		; Host groups that Windows servers should be a member of
	register		          0		; DONT REGISTER THIS - ITS JUST A TEMPLATE
	}
Command definition:

Code: Select all

define command{
        command_name    check_ftp
        command_line    $USER1$/ftp.pl
        }
Perl script:

Code: Select all

#!usr/bin/perl

use Net::FTP;
use lib "PATH TO LIBRARY";
#use Try::Tiny; /depreciated
use Time::Piece;


$host = "FTP SERVER";
$user = "FTP USERNAME";
$pw =  "FTP PASSWORD";
$dir = "Test";
$getFile = "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\n");
print FILE "$date\n";
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 connect to $dir\n";

#Sends to directory, gets from directory
$ftp->put($putFile);
$ftp->get($getFile);

#Reads date from file to make sure it matches $date
open(FILE, "$getFile");
while(<FILE>){
	chomp;
	$fileOut = $_;
}

#Nagios logic
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
}

I even tried rubber duck debugging this, following the cfg files as if I were Nagios, and it should work!
User avatar
eloyd
Cool Title Here
Posts: 2129
Joined: Thu Sep 27, 2012 9:14 am
Location: Rochester, NY
Contact:

Re: "Cannot find file" error on host

Post by eloyd »

Did you actually execute the check as the nagios user? That's a step people commonly forget to try.
Image
Eric Loyd • http://everwatch.global • 844.240.EVER • @EricLoydI'm a Nagios Fanatic!
logic_bomb421
Posts: 43
Joined: Tue Jul 15, 2014 6:58 pm

Re: "Cannot find file" error on host

Post by logic_bomb421 »

What do you mean exactly? When I tested it via the terminal I had full root access.
User avatar
eloyd
Cool Title Here
Posts: 2129
Joined: Thu Sep 27, 2012 9:14 am
Location: Rochester, NY
Contact:

Re: "Cannot find file" error on host

Post by eloyd »

The user called "nagios" runs the commands, not the root user. Sometimes, this makes a big difference. So you will want to become the nagios user and execute your plugin exactly as nagios would, to see if it works:

Code: Select all

su nagios -c "/usr/local/nagios/libexec/ftp.pl"
I'm guessing that something this plugin is doing is causing your error, not the file itself.
Image
Eric Loyd • http://everwatch.global • 844.240.EVER • @EricLoydI'm a Nagios Fanatic!
logic_bomb421
Posts: 43
Joined: Tue Jul 15, 2014 6:58 pm

Re: "Cannot find file" error on host

Post by logic_bomb421 »

So I figured out the problem. The file that the scripts pulls back via FTP is owned by root and everyone else has crap for permissions. For some reason chmod is not working though. The file is located in the root directory, and when I try

Code: Select all

chmod 777 /ftptest
nothing at all changes.
User avatar
eloyd
Cool Title Here
Posts: 2129
Joined: Thu Sep 27, 2012 9:14 am
Location: Rochester, NY
Contact:

Re: "Cannot find file" error on host

Post by eloyd »

Are you saying that the script tries to ftp from the Nagios box to a remote server (which may be itself), puts a file called /ftptest, and then gets the file called /ftptest?

I'm guessing that your FTP server is giving you grief for logging in as root and is manipulating the effective protections. It's looking like this is more of a systems administration issue than a Nagios one at this point, though.
Image
Eric Loyd • http://everwatch.global • 844.240.EVER • @EricLoydI'm a Nagios Fanatic!
logic_bomb421
Posts: 43
Joined: Tue Jul 15, 2014 6:58 pm

Re: "Cannot find file" error on host

Post by logic_bomb421 »

The script takes a file on the local Nagios box, puts it on our FTP server via a login and folder setup just for this, and then gets the same file it just put on the server. There's some other logic in the script that writes a date and time to the file, and then at the end checks to make sure the date and time match what was sent. The reason Nagios is showing an error is because the file that the script gets from the FTP server comes back owned by root, with no other permissions for anyone else, so it's unable to read the date and time.

Normally I'd just chmod 777 the file, but this one doesn't want to change for some reason. I'm guessing it has something to do with being in the root directory (i.e. I'm trying "chmod 777 /ftptest" instead of "chmod 777 /path/to/file"), although I don't know why this would cause this issue.

As for being a systems administration issue at this point, I'd agree. I just need to change the permission on that file and I should be good to go. I was just hoping someone here might see something I'm doing wrong with the chmod command or something, since I'm so new to Linux.

Since it's an SA issue and not Nagios, this can be marked answered though.
User avatar
eloyd
Cool Title Here
Posts: 2129
Joined: Thu Sep 27, 2012 9:14 am
Location: Rochester, NY
Contact:

Re: "Cannot find file" error on host

Post by eloyd »

Are you sure you're root? There's no reason I can think of why root can't chmod 777 /ftptest on any Unix system I've ever been on. However, if that's the case, try moving your file to /tmp and see if that makes a difference.

Are you trying the chmod from within your script or at the command line?
Image
Eric Loyd • http://everwatch.global • 844.240.EVER • @EricLoydI'm a Nagios Fanatic!
logic_bomb421
Posts: 43
Joined: Tue Jul 15, 2014 6:58 pm

Re: "Cannot find file" error on host

Post by logic_bomb421 »

It's possible I did it wrong initially, because I was able to get it to work now.

So the only thing I'm having a problem with is for some reason when I run my script as the Nagios user, I don't see any changes to the files (part of the script writes the current date and time to the file, then later reads it and checks against the $date variable) in the directories specified in the script, but the terminal still returns OK.

Why does everything seem to run fine when I'm root, but get all wonky when running as Nagios? This is the first time I've had this happen and I've written several other plugins in Perl that work fine.

Edit: So I got the script to run fine when I use

Code: Select all

su nagios -c "/usr/local/nagios/libexec/ftp.pl"
meaning it should work with nagios now. I checked all the file permissions and made sure everyone had access to the appropriate files, I even followed the logic of the program when executed as nagios to make sure it was doing exactly what it should do, and it was working fine. However, I'm still getting that same error in my console. I can confirm that, when executing this script manually as the nagios user, it works exactly how I want it to. So what in Nagios is causing it to fail?
sreinhardt
-fno-stack-protector
Posts: 4366
Joined: Mon Nov 19, 2012 12:10 pm

Re: "Cannot find file" error on host

Post by sreinhardt »

Do you output a message to standard out in your plugin? (sorry didnt read through it all as I am by no means a perl guy) It seems to me that the error is one of two things:

more file permissions issues with the ftp script. (not likely as you can run it via su - nagios)
the plugin does not output to stdout, which nagios expects or it will give a very similar message.
Nagios-Plugins maintainer exclusively, unless you have other C language bugs with open-source nagios projects, then I am happy to help! Please pm or use other communication to alert me to issues as I no longer track the forum.
Locked