我绝不是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';
最后两行是预期的,但第一部分不适用于我尝试调试的脚本。有没有人有任何关于我如何从我得到的对象中获取所需字符串的建议,或者是另一种方法来处理它?</ p>
答案 0 :(得分:0)
谢谢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) . "|";
}
}