-1

我有一个从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;
}

有什么想法可能会丢失吗?

4

1 回答 1

0

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

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

我已经提供了文件的完整路径并且脚本工作正常。

谢谢大家的帮助。

于 2017-07-18T14:54:33.727 回答