Dan-Dev Dan-Dev - 1 month ago 17
Perl Question

Nagios::Object::Config it is behaving unexpectedly

I am by no means an expert in Perl but I am trying to debug a script nagios_reader_to_centreon_clapi.pl from https://github.com/centreon/nagiosToCentreon/blob/master/nagios_reader_to_centreon_clapi.pl it is behaving unexpectedly on line 656 giving output like

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


I have stripped down the code to the bare minimum in order to reproduce the expected results and the unexpected results.

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


new-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
}


new-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
}


new-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
}


running the script the output starts:

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,


and ends with:

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


$VAR1 = 'Group4';


$VAR1 = 'Group3';


The last two lines are as expected but the first part does not work with the script I am trying to debug. Has anyone got any suggestions as to how I can get the desired strings from the object I am getting or else another way to handle it?

Answer

Thanks stevieb you put me on the right path. This seems to work:

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";
         }
    }
}

In the original file:

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) . "|";
        }   
    }

and

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) . "|";
         }
    }