从Nagios XI后端执行脚本时,Perl子例程未运行

时间:2017-07-17 15:08:18

标签: perl nagiosxi

我有一个从 Nagios XI 执行的Perl脚本。

它有两个子程序:SendEmailSendTraps

通过传递所需参数手动执行脚本时,脚本工作正常,但从Nagios触发时它不起作用。脚本被执行但子例程被跳过。

echo正在运行,但即使符合条件,这两个子程序也无法正常工作。

if ( ( $hoststatetype =~ m/HARD/ ) && ( $hoststate =~ m/DOWN/ ) ) {

    `echo "HOST::$prihost $hostoutput">>/tmp/failover_log.txt`;

    sendMail();
    send_trap();
}

完整的脚本:

use strict;
use warnings;

use Text::CSV;

# Declared all the variables here

# Parsing input arguments

if ( $#ARGV > -1 ) {

   if ( $ARGV[0] eq "-nagiosxi_trigger" ) {

       $prihost       = $ARGV[1];
       $hoststate     = $ARGV[2];
       $hoststatetype = $ARGV[3];
       $hostoutput    = $ARGV[4];
    }
    elsif ( $ARGV[0] eq "-manual_trigger" ) {

        $comment = $ARGV[1];
        $userid  = $ARGV[2];
        $flag    = "Failover-Trigger_Manual";

        print "Maunal Failover triggered with comment: $comment by $userid\n";

        $error_desc = "Maunal Failover triggered with comment: $comment by $userid";

        send_trap();
        sendMail();

        exit 0;
    }
    else {

        print STDERR "Invalid parameter $ARGV[0] \n";

        exit 1;
    }
}
else {

    print STDERR "ERROR:No Arguments Passed.\n";

    exit 1
}

# Check if Host or Service is in Hard/down state

if ( ( $hoststatetype =~ m/HARD/ ) && ( $hoststate =~ m/DOWN/ ) ) {

    `echo "HOST::$prihost $hostoutput">>/tmp/failover_log.txt`;

    sendMail();
    send_trap();
}
elsif ( ( $hoststatetype =~ m/SOFT/ ) && ( $hoststate =~ m/DOWN/ ) ) {

    `echo "HOST::$prihost $hostoutput">>/tmp/failover_log.txt`;
}
else {

    `echo "HOST Good, $prihost $hostoutput">>/tmp/failover_log.txt`;
}

# Sub-Routines

sub failover {

    my $csv  = Text::CSV->new({ sep_char => ',' }) or die "Cannot use CSV: ".Text::CSV->error_diag ();;
    my $file = "myxilist";
    my $primary;
    my $secondary;
    #my $xienv;

    my $host = `hostname`;
    chomp $host;
    open( my $data, '<', $file ) or die "Could not open '$file' $!\n";

    while ( my $xi = <$data> ) {
        chomp $xi;

        if ( $csv->parse($xi) ) {

            my @fields = $csv->fields();

            if ( $fields[0] =~ m/$host/ ) {
                $primary   = $fields[1];
                $secondary = $fields[0];
                $xienv     = $fields[2];
            }
            elsif ( $fields[1] =~ m/$host/ ) {
                $primary   = $fields[0];
                $secondary = $fields[1];
                $xienv     = $fields[2];
           }
        }
        else {
            warn "Line could not be parsed: $xi\n";
            exit 1;
        }
    }
my $failovermsg="failover successful from $primary to $secondary server";
return $failovermsg;
}


sub sendMail {

    #  Build the list for mailing out results

    my $mailSubject;
    my $mailID  = "test\@mail.com";
    my @results = failover();

    $mailSubject = "Failover Successful on $xienv instance";

    print "Sending email to $mailID \n";
    `echo "sending Email">>/tmp/failover_log.txt`;

    open MAILX, "|/usr/bin/mailx -s \"$mailSubject\"        $mailID " or die $!;
    print MAILX "@results";
    close MAILX;

    return;
}

sub send_trap {

    # Sending SNMP traps 

    my @results = failover();
    my $trap    = `/usr/bin/snmptrap -v 2c -c public tcp:server:1010 '' MIB::Event Hostname s "$xienv" nSvcDesc s "$flag" nSvcStateID i 2 nSvcOutput s "@results"`;
    return;
}

有什么想法可以遗漏?

1 个答案:

答案 0 :(得分:0)

问题发生在故障转移()SubRoutine中。我正在调用与脚本位于同一目录中的文件“myxilist”。

因此,手动调用时脚本运行正常,但是当它从应用程序触发时,脚本将从其他目录执行,并且故障转移子程序退出,因为它无法打开文件。

我提供了文件的完整路径,脚本运行正常。

谢谢大家的帮助。