为什么Net :: SNMP :: Dispatcher失败并出现“select()错误[没有子进程]”?

时间:2011-08-05 17:59:08

标签: perl cgi snmp mod-perl sigchld

在轮询SNMP服务时,我需要一些帮助来识别并消除奇怪子进程错误的原因。

在SNMP连接期间,我通过轮询来验证SNMP连接 设备名称,直到超时:

sub snmp_close {
    my $self = shift;

    $self->{SNMP_SESSION}->close if (defined $self->{SNMP_SESSION} && $self->{SNMP_SESSION});
    $self->{SNMP_SESSION} = undef;
}

sub {
    my ($self, $ip, $community) = @_;
    # If there's a leftover session around, make sure it's closed
    $self->snmp_close;

    my ($session, $error) = Net::SNMP->session(
        -hostname => $ip,
        -community => $community,
        -nonblocking => 1,
        -version => 'snmpv2c',
        -translate => [
            -timeticks => 0x0
        ],
    );

    if (!defined $session) {
        $self->_logger->logcluck("Can't create SNMP object, error: '$error'");
        return;
    }

    $self->{SNMP_SESSION} = $session;

    my $end = time() + 90;
    while (time < $end) {
        $self->_logger->debug("Probing for SNMP connectivity, giving up in " . int($end - time()) . " seconds");
        my %sysName = $self->get_bulk('1.3.6.1.2.1.1.5');
        if(scalar keys %sysName >= 1) { # try polling sysName..
            return 1;
        }
        else {
            sleep 5;
        }
    }
    # if we've made it this far there's no hope for snmp...
    $self->_logger->warn("No SNMP connectivity after 90 seconds");
    $self->{SNMP_SESSION} = 0;
    return;
}

sub get_bulk { # return a hash of oid keys and values
    my ($self, $oid) = @_;

    $self->_logger->logdie("Not connected; call snmp_connect") if not defined $self->{SNMP_SESSION};
    $self->_logger->logdie("Connection failed") if not $self->{SNMP_SESSION};

    my %table;

    my $result = $self->snmp->get_bulk_request(
        -varbindlist => [ $oid ],
        -maxrepetitions => 20,
        -callback => [\&_table_callback, $self, \%table, $oid],
    );

    if (!defined $result) {
        $self->_logger->warn("SNMP error: '" . $self->snmp->error() . "'");
        return;
    }

    snmp_dispatcher();

    use Data::Dumper; my %_table = map {s/\Q$oid.\E//; $_} %table; $self->_logger->debug("SNMP Debug, OID polled: '$oid', response is: " . Dumper(\%_table));
    return %table;
}

大多数时候,这种方式完美无缺,但有一定比例 我从Net :: SNMP :: Dispatcher中得到一个致命错误:

FATAL: select() error [No child processes] at perl/lib/perl5.8/Net/SNMP/Dispatcher.pm line 635.
  at perl/lib/perl5.8/Net/SNMP/Dispatcher.pm line 635
        Net::SNMP::Dispatcher::_event_select('Net::SNMP::Dispatcher=HASH(0xaca5ce0)', 4.99994683265686) called at perl/lib/perl5.8/Net/SNMP/Dispatcher.pm line 601
        Net::SNMP::Dispatcher::_event_handle('Net::SNMP::Dispatcher=HASH(0xaca5ce0)') called at perl/lib/perl5.8/Net/SNMP/Dispatcher.pm line 80
        Net::SNMP::Dispatcher::activate('Net::SNMP::Dispatcher=HASH(0xaca5ce0)') called at perl/lib/perl5.8/Net/SNMP.pm line 611
        Net::SNMP::snmp_dispatcher() called at perl/lib/perl5.8/Device.pm line 857
        Device::get_bulk('Device::Class=HASH(0xb1e405c)', 1.3.6.1.2.1.1.5) called at perl/lib/perl5.8/Device.pm line 824
        Device::snmp_connect('Device::Class=HASH(0xb1e405c)', 10.0.0.1, 'COMMUNITY_STRING') called at perl/lib/perl5.8/Device.pm line 912

(第857行是上面get_bulk中的snmp_dispatcher

我足够新的perl - 而且对SNMP来说是全新的 - 我并不是真的 知道如何解决这个问题。有问题的方法在a中执行 mod_perl CGI调用,如果这有助于隔离问题。

1 个答案:

答案 0 :(得分:0)

一些当地调查发现,这是多核机器的副作用;子进程在另一个核心上运行,其管道文件句柄不可用于父进程。