LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-07-2014, 12:09 PM   #1
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
perl Net::SFTP::Foreign. How to set return code 3 on timeout?


How can this perl scrippet ...
Code:
my $sftp = Net::SFTP::Foreign->new($host,user=>$user);
$sftp->die_on_error("Unable to establish SFTP connection");
... be made to set return code 3 when it errors?

The reason for wanting to know is we have unreliable network connections and a home-grown nagios plug-in (for amanda) which times out resulting in spurious critical alerts. The developer who wrote it is long gone and we have no perl programmers.

Return code 3 from a plug-in tells nagios that the state is unknown.

It would also be helpful to know how long it takes to time out (but we could find out by testing).

The Net::SFTP::Foreign page is http://search.cpan.org/~salva/Net-SF...FTP/Foreign.pm
 
Old 04-07-2014, 03:19 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,147

Rep: Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264
As it describes on the line above:
Code:
$sftp->error and return 3;
 
1 members found this post helpful.
Old 04-10-2014, 11:40 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578

Original Poster
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Thanks samllpond

It may take a while to test so wanted to say thanks now.
 
Old 04-14-2014, 11:24 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578

Original Poster
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
That worked as expected (timed out, set return code 3) but the nagios emails were unchanged. According to http://nagios.sourceforge.net/docs/3_0/epnplugins.html, a nagios plug-in should always generate some output so I tried to modify the script further but was unable to master the syntax. Here's the relevant scrippet:
Code:
my $timeout = '60';
my $weekly = 0;

# 11 Apr 2014 Charles for NAG-16:
#   * Add timeout, value chosen (above) to be less than service_check_timeout in
#     /etc/nagios3/nagios.cf
#   * Return nagios service state UNKNOWN on error
# 14 Apr 2014 Charles for NAG-16:
#   * Also generate some output
my $sftp = Net::SFTP::Foreign->new($host,timeout=>$timeout,user=>$user);
#$sftp->die_on_error("Unable to establish SFTP connection");
if $sftp->error && {
    print "Unable to establish SFTP connection";
    exit $ERRORS{'UNKNOWN'};
}
$sftp->setcwd($path) or die "unable to change cwd: " . $sftp->error;
And here's the output:
Code:
root@nagios.bluelight:/usr/lib/nagios/plugins# ./check_amanda.pl redearth /var/lib/amanda/earth-daily backup
Scalar found where operator expected at ./check_amanda.pl line 35, near "$sftp"
	(Missing semicolon on previous line?)
syntax error at ./check_amanda.pl line 31, near "if $sftp"
syntax error at ./check_amanda.pl line 34, near "}"
Execution of ./check_amanda.pl aborted due to compilation errors.
I had previously tried
Code:
$sftp->error and {
but the reult was similar.
 
Old 04-19-2014, 01:59 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578

Original Poster
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
The solution to the syntax error was to change ...
Code:
if $sftp->error && {
... to ...
Code:
if ($sftp->error) {
Marking the thread as solved because the original question is answered.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Call to select() does not return after timeout taylor_ma Programming 8 02-20-2012 03:07 AM
SFTP throwing "End of file" when doing SFTP transfer in perl exceed1 Programming 5 03-06-2010 01:34 PM
cannot login into SFTP server using Net::SFTP cccc Programming 1 10-31-2007 06:23 AM
install Net-SFTP perl module on debian etch cccc Debian 8 10-30-2007 09:47 AM
perl and net::ldap how to return dn from a search jonlake Programming 2 02-24-2006 12:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration