Ok, I've re-tooled, using more simplified logic for the purpose of testing the SSH connection. The script now has no logic, it just attempts to ssh to the remote host and run /usr/bin/uname -a.<div><br></div><div>I've also explicitly defined my SSH key.</div>
<div><br></div><div>As before, the script works perfectly when run manually as nagios user via the command line, but fails at the SSH call when executed via event handler from the nagios daemon.</div><div><br></div><div>I'm beginning to wonder if Nagios & Net::SSH::Perl simply do not play nice with each other.</div>
<div><br></div><div>I've attached my simplified script if anyone wants to see the exact code in full context.</div><div><br></div><div><div class="gmail_quote">On Wed, Nov 26, 2008 at 10:26 AM, Jason <span dir="ltr"><<a href="mailto:daelic@gmail.com">daelic@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Yes, I am using public key authentication. It works fine when I run the script manually from the command prompt under the Nagios user, just not when the script is launched via nagios.<div>
<br></div><div>Combining your reply with the reply from Sascha Runschke, I'm wondering if it's not finding $HOME/.ssh due to the environment being stripped down. I'm going to try explicitly defining my private key.</div>

<div><br></div><div>I did put in full paths to my remote commands, and that did not resolve the problem.<div><div></div><div class="Wj3C7c"><br><br><div class="gmail_quote">On Tue, Nov 25, 2008 at 11:34 PM, Badri Pillai <span dir="ltr"><<a href="mailto:badri@diglinks.com" target="_blank">badri@diglinks.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Jason,<br>
<br>
I assume you are using public key authentication.<br>
<br>
If yes,<br>
I don't see ssh key to be used  being passed as argument,<br>
cause the key files are searched by default in "$HOME/.ssh"<br>
may be this directory is not accessible for the server?<br>
<br>
Other options to check -> if you  need a  " pty??  or not.<br>
<br>
<br>
Regards,<br>
<font color="#888888"><br>
BP<br>
</font><div><div></div><div>Jason wrote:<br>
> Hello,<br>
> I've written an Event Handler using Perl. The intent is to verify on the<br>
> remote box that we do need to restart the daemon in question, and then<br>
> perform a restart if needed.<br>
><br>
> The script works perfectly via the command line when I execute it as the<br>
> nagios user. Unfortunately, if the script doesn't work properly when it is<br>
> executed by nagios in response to a state change. It dies before completion.<br>
><br>
> I've put in various hooks (Basicly just made it send me an email at various<br>
> stages of the script) to determine exactly where it dies, and have isolated<br>
> it. Here is the portion that causes the script to die when nagios executes<br>
> it. (Please trust me and assume that $hostname and $username contain proper<br>
> values. I've tested the variables extensively to insure that they are being<br>
> populated.)<br>
><br>
> If I run the below, I'll see a log entry and an email for Step 1, but<br>
> nothing for Step 2 and beyond.<br>
><br>
> #!/usr/bin/perl<br>
> require '/usr/local/scripts/subs.pl';<br>
><br>
> report_findings("Step 1",".");<br>
> my $command = "grep check_tacplus /usr/local/nagios/etc/nrpe.commands";<br>
> my %tac_expected = run_command($command, $hostname, $username);<br>
> report_findings("Step 2",".");<br>
><br>
> sub run_command {<br>
>         my $command = $_[0];<br>
>         my $host = $_[1];<br>
>         my $username = $_[2];<br>
><br>
>         my %result = SSHexec($command, $hostname, $username);<br>
>         if ($result{'err'}) {<br>
>                 my $subject = "Automated refreshtac failed on $hostname";<br>
>                 my $message = "Error when attempting SSH connection to<br>
> $hostname:\n";<br>
>                 $message .= "  ".$result{'err'}."\n";<br>
>                 report_findings($subject, $message, $recipients);<br>
>                 exit;<br>
>         }<br>
>         return %result;<br>
> }<br>
><br>
> sub SSHexec {<br>
>         use Net::SSH::Perl;<br>
><br>
>         my $CMD = $_[0];<br>
>         my $HOST = $_[1];<br>
>         my $USR = $_[2];<br>
>         my %RESULT;<br>
><br>
>         if (pingHost($HOST)) {<br>
>                 %RESULT = ("err", "Connectivity Failure");<br>
>                 return %RESULT;<br>
>         }<br>
><br>
>         my $ssh = Net::SSH::Perl->new($HOST, options => [ "protocol '2,1'"<br>
> ]);<br>
>         unless ($ssh->login($USR)) {<br>
>                 %RESULT = ("err", "Authentication Failure");<br>
>                 return %RESULT;<br>
>         }<br>
>         my ($out,$err,$exit) = $ssh->cmd($CMD);<br>
>         %RESULT = ("out", $out,<br>
>                         "err", $err,<br>
>                         "exit", $exit);<br>
>         return %RESULT;<br>
> }<br>
><br>
> &report_findings is nothing but a wrapper for logging and sending email.<br>
> This works up until the SSH call.<br>
><br>
> &SSHexec is contained in /usr/local/scripts/subs.pl which holds common<br>
> subroutines that I use frequently when writing scripts. I use other subs<br>
> from there, so I know it's getting loaded.<br>
><br>
> I cannot figure out why this is working as an automation, when it works<br>
> perfectly otherwise. Any advice/hints/suggestions?<br>
><br>
><br>
</div></div>> ------------------------------------------------------------------------<br>
<div><div></div><div>><br>
> -------------------------------------------------------------------------<br>
> This SF.Net email is sponsored by the Moblin Your Move Developer's challenge<br>
> Build the coolest Linux based applications with Moblin SDK & win great prizes<br>
> Grand prize is a trip for two to an Open Source event anywhere in the world<br>
> <a href="http://moblin-contest.org/redirect.php?banner_id=100&url=/" target="_blank">http://moblin-contest.org/redirect.php?banner_id=100&url=/</a><br>
> ------------------------------------------------------------------------<br>
><br>
> _______________________________________________<br>
> Nagios-devel mailing list<br>
> <a href="mailto:Nagios-devel@lists.sourceforge.net" target="_blank">Nagios-devel@lists.sourceforge.net</a><br>
> <a href="https://lists.sourceforge.net/lists/listinfo/nagios-devel" target="_blank">https://lists.sourceforge.net/lists/listinfo/nagios-devel</a><br>
><br>
<br>
<br>
-------------------------------------------------------------------------<br>
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge<br>
Build the coolest Linux based applications with Moblin SDK & win great prizes<br>
Grand prize is a trip for two to an Open Source event anywhere in the world<br>
<a href="http://moblin-contest.org/redirect.php?banner_id=100&url=/" target="_blank">http://moblin-contest.org/redirect.php?banner_id=100&url=/</a><br>
_______________________________________________<br>
Nagios-devel mailing list<br>
<a href="mailto:Nagios-devel@lists.sourceforge.net" target="_blank">Nagios-devel@lists.sourceforge.net</a><br>
<a href="https://lists.sourceforge.net/lists/listinfo/nagios-devel" target="_blank">https://lists.sourceforge.net/lists/listinfo/nagios-devel</a><br>
</div></div></blockquote></div><br></div></div></div>
</blockquote></div><br></div>