Nagios::Object::Config 它的行为异常

Nagios::Object::Config it is behaving unexpectedly

我绝不是 Perl 专家,但我正在尝试从 https://github.com/centreon/nagiosToCentreon/blob/master/nagios_reader_to_centreon_clapi.pl 调试脚本 nagios_reader_to_centreon_clapi.pl 它在第 656 行出现意外行为,输出类似于

SERVICE;setcontactgroup;server.example.com;mailbox_status_node1;Nagios::ContactGroup=HASH(0xe28550)|Nagios::ContactGroup=HASH(0xe2ae10)

为了重现预期结果和意外结果,我已将代码精简到最低限度。

test.pl

use Nagios::Config;
use Nagios::Object::Config;
use Data::Dumper;
use feature 'say';
use Getopt::Long;

Getopt::Long::Configure('bundling');
GetOptions(
    "C|config=s"    => $OPTION{'config'}
);

my $objects;
$objects = Nagios::Config->new( Filename => $OPTION{'config'} );
my @services_array = $objects->list_services();
foreach my $service ( @services_array ){
        foreach my $item ( @{$service->contact_groups} ) {
            $var = ref $item;
            print "$var \n";
            say Dumper($item);
        }
}

test.cfg

cfg_file=/etc/nagios/new-hosts.cfg
cfg_file=/etc/nagios/new-services.cfg
cfg_file=/etc/nagios/new-contactgroups.cfg

新-hosts.cfg

define host{
        host_name                       server1.example.com
#       use                             generic_ht
        alias                           Server 01
        address                         xxx.xx.xxx.xxx
        hostgroups                      servers
        active_checks_enabled           1
        contact_groups                  Group3
}

新-services.cfg

define service{
        name                            generic_st
        service_description             generic_st
        max_check_attempts              3
        normal_check_interval           5
        retry_check_interval            1
        active_checks_enabled           1
        passive_checks_enabled          1
        check_period                    24x7
        notification_interval           240
        notification_period             24x7
        notification_options            w,c,r
        notifications_enabled           1
        contact_groups                  Group1
        register                        0
}

define service{
        host_name                       server1.example.com
        service_description             service1
        use                             generic_st
        check_command                   check_cmd!10000!12000
        contact_groups                  Group2
}

define service{
        name                        service2
        service_description         service02
        use                         generic_st
        check_command               check_cmd2
        active_checks_enabled       1
        passive_checks_enabled      1
        check_period                24x7
        notifications_enabled       1
        contact_groups              Group4, Group3
        register                    0
}

新-contactgroups.cfg

define contactgroup{
        contactgroup_name               Group1 
        alias                           Group1
        members                         Member1
}
define contactgroup{
        contactgroup_name               Group2
        alias                           Group2
        members                         Member2
}

define contactgroup{
        contactgroup_name               Group3
        alias                           Group3
        members                         Member3
}

define contactgroup{
        contactgroup_name               Group4
        alias                           Group04
        members                         MMember4
}

运行 输出开始的脚本:

 perl test.pl   --config /etc/nagios/test.cfg
Nagios::ContactGroup
$VAR1 = bless( {
             'object_config_object' => bless( {
                                                'hostdependency_list' => [],
                                                'host_index' => {
                                                                     'server1.example.com' => [
                                                                                             bless( {
                                                                                                      'flap_detection_enabled' => undef,
                                                                                                      'check_freshness' => undef,
                                                                                                      'failure_prediction_enabled' => undef,
                                                                                                      'file' => undef,
                                                                                                      'check_period' => undef,
                                                                                                      'initial_state' => undef,
                                                                                                      'freshness_threshold' => undef,
                                                                                                      'notes_url' => undef,
                                                                                                      'first_notification_delay' => undef,

并以:

结尾
 'alias' => 'Group2',
             '_has_been_resolved' => 1
           }, 'Nagios::ContactGroup' );


$VAR1 = 'Group4';


$VAR1 = 'Group3';

最后两行符合预期,但第一部分不适用于我尝试调试的脚本。有没有人对我如何从我正在获取的对象中获取所需的字符串或其他处理方法有任何建议?

谢谢 stevieb,你让我走上了正确的道路。这似乎有效:

use Nagios::Config;
use Nagios::Object::Config;
use Data::Dumper;
use feature 'say';
use Getopt::Long;

Getopt::Long::Configure('bundling');
GetOptions(
    "C|config=s"    => $OPTION{'config'}
);

my $objects;
$objects = Nagios::Config->new( Filename => $OPTION{'config'} );
my @services_array = $objects->list_services();
foreach my $service ( @services_array ){
    foreach my $item ( @{$service->contact_groups} ) {
        $var = ref $item;
        if ($var eq "Nagios::ContactGroup"){
            print $item->name . "\n";
         } else {
             print $item . "\n";
         }
    }
}

原文件中:

if ( scalar @{$host->contact_groups} > 1 ) {
    foreach my $item ( @{$host->contact_groups} ) {
        $item_type = ref $item;
        if ($item_type eq "Nagios::ContactGroup"){
            $contactgroups_list .= sprintf ( "%s", $item->name) . "|";
        } else {
            $contactgroups_list .= sprintf ( "%s", $item) . "|";
        }   
    }

if ( scalar @{$service->contact_groups} > 1 ) {
    foreach my $item ( @{$service->contact_groups} ) {
         $item_type = ref $item;
         if ($item_type eq "Nagios::ContactGroup"){
             $contactgroups_list .= sprintf ( "%s", $item->name) . "|";
         } else {
             $contactgroups_list .= sprintf ( "%s", $item) . "|";
         }
    }