我正在运行下面的脚本来查询监控系统,我得到的结果是哈希数组。访问各个哈希及其元素的最佳方法是什么?
use strict;
use warnings;
use YMS::Client::MonStatus;
use Getopt::Long;
use DBI::Dumper;
use Data::Dumper;
use YMS::Client::MonStatus::Filter;
my $username = "username";
my $password = "passwd";
my $cluster = "aso";
#my $filter = YMS::Client::MonStatus::Filter();
my $client = YMS::Client::MonStatus->new($cluster);
$client->verbose("on");
$client->byauth({username => $username, password => "$password"});
$client->yms_rotation("bot.ops.com:4080");
$client->debug("on");
my $filter = YMS::Client::MonStatus::Filter->new();
$filter->add("cluster", "=", "aso");
$filter->add("host", "in", "<hostname>");
#$filter->add("service", "=", "Prod-Deploy");
$filter->add("status", "=", "OK");
my %args = (
"filters" => $filter,
"times" => {
"start" => "now - 4 hours",
"end" => "now"
},
);
$client->filters($filter);
#$client->saved_list();
$client->status_list(\%args);
my @result = $client->output;
my $field = scalar (@result);
#print Dumper (@result);
#my $ref = $result[0][0]->[0];
#print Dumper ($result[0][0]);
print $field;
for ( my $i = 0; $i < $field; $i++ ) {
print $result[$i]{'notification_enabled'} ."\n";
}
我查询API的输出如下所示。
$VAR1 = [
{
'acknowledged' => 'N/A',
'check_type' => 'ACTIVE',
'current_attempt' => 1,
'deleted' => 0,
'dimensions' => {
'cluster' => '<cluster>',
'service' => '<servicename>',
'host' => 'hostfoobaar'
},
'flapping' => 'N/A',
'frequency' => 0,
'hidden' => 0,
'last_ack_time' => 0,
'last_check_time' => 1432875229,
'last_comment_time' => 0,
'last_delete_time' => 1432875229,
'last_downtime_time' => 0,
'last_notif_time' => 0,
'last_state_change' => 1432875094,
'last_status_change' => 1432875094,
'max_attempt' => 1,
'msg_length' => 10,
'notification_enabled' => 'ENABLED',
'num_comments' => 0,
'quality_metric' => 0,
's_0' => 0,
's_1' => 0,
's_2' => 0,
's_3' => 0,
's_4' => 0,
's_5' => 0,
's_6' => 0,
's_7' => 0,
's_8' => 0,
's_9' => 0,
'scheduled_downtime' => 'NO',
'source' => '',
'state' => 'HARD',
'status' => 'OK',
'status_msg' => 'status ok',
'trap' => 1,
'ts_0' => 1432875229,
'ts_1' => 1432875094,
'ts_2' => 0,
'ts_3' => 0,
'ts_4' => 0,
'ts_5' => 0,
'ts_6' => 0,
'ts_7' => 0,
'ts_8' => 0,
'ts_9' => 0
}
];`
这里是print Dumper \ @results
的输出$VAR1 = [
[
{
'acknowledged' => 'N/A',
'check_type' => 'ACTIVE',
'current_attempt' => 1,
'deleted' => 0,
'dimensions' => {
'cluster' => 'aso',
'service' => 'system_launch',
'host' => 'hostfoobar'
},
'flapping' => 'N/A',
'frequency' => 0,
'hidden' => 0,
'last_ack_time' => 0,
'last_check_time' => 1432875317,
'last_comment_time' => 0,
'last_delete_time' => 0,
'last_downtime_time' => 0,
'last_notif_time' => 0,
'last_state_change' => 1432875317,
'last_status_change' => 1432875317,
'max_attempt' => 1,
'msg_length' => 10,
'notification_enabled' => 'ENABLED',
'num_comments' => 0,
'quality_metric' => 0,
's_0' => 0,
's_1' => 0,
's_2' => 0,
's_3' => 0,
's_4' => 0,
's_5' => 0,
's_6' => 0,
's_7' => 0,
's_8' => 0,
's_9' => 0,
'scheduled_downtime' => 'NO',
'source' => '',
'state' => 'HARD',
'status' => 'OK',
'status_msg' => 'status ok',
'trap' => 1,
'ts_0' => 1432875317,
'ts_1' => 0,
'ts_2' => 0,
'ts_3' => 0,
'ts_4' => 0,
'ts_5' => 0,
'ts_6' => 0,
'ts_7' => 0,
'ts_8' => 0,
'ts_9' => 0
},
{
'acknowledged' => 'N/A',
'check_type' => 'ACTIVE',
'current_attempt' => 1,
'deleted' => 0,
'dimensions' => {
'cluster' => 'cluster',
'service' => 'Prod-Deploy',
'host' => 'hostfoobar'
},
'flapping' => 'N/A',
'frequency' => 0,
'hidden' => 0,
'last_ack_time' => 0,
'last_check_time' => 1432875229,
'last_comment_time' => 0,
'last_delete_time' => 1432875229,
'last_downtime_time' => 0,
'last_notif_time' => 0,
'last_state_change' => 1432875094,
'last_status_change' => 1432875094,
'max_attempt' => 1,
'msg_length' => 10,
'notification_enabled' => 'ENABLED',
'num_comments' => 0,
'quality_metric' => 0,
's_0' => 0,
's_1' => 0,
's_2' => 0,
's_3' => 0,
's_4' => 0,
's_5' => 0,
's_6' => 0,
's_7' => 0,
's_8' => 0,
's_9' => 0,
'scheduled_downtime' => 'NO',
'source' => '',
'state' => 'HARD',
'status' => 'OK',
'status_msg' => 'status ok',
'trap' => 1,
'ts_0' => 1432875229,
'ts_1' => 1432875094,
'ts_2' => 0,
'ts_3' => 0,
'ts_4' => 0,
'ts_5' => 0,
'ts_6' => 0,
'ts_7' => 0,
'ts_8' => 0,
'ts_9' => 0
},
{
'acknowledged' => 'N/A',
'check_type' => 'ACTIVE',
'current_attempt' => 1,
'deleted' => 0,
'dimensions' => {
'cluster' => 'cluster',
'query' => 'foobar',
'service' => 'foobar',
'host' => 'hostfoobar'
},
'flapping' => 'N/A',
'frequency' => 60,
'hidden' => 0,
'last_ack_time' => 1433949747,
'last_check_time' => 1433949867,
'last_comment_time' => 1421368592,
'last_delete_time' => 1433949867,
'last_downtime_time' => 1421376467,
'last_notif_time' => 1427825899,
'last_state_change' => 1407310478,
'last_status_change' => 1433949867,
'max_attempt' => 1,
'msg_length' => 172,
'notification_enabled' => 'ENABLED',
'num_comments' => 8,
'quality_metric' => 0,
's_0' => 0,
's_1' => 1,
's_2' => 0,
's_3' => 0,
's_4' => 0,
's_5' => 0,
's_6' => 0,
's_7' => 0,
's_8' => 0,
's_9' => 0,
'scheduled_downtime' => 'NO',
'source' => '',
'state' => 'HARD',
'status' => 'OK',
'status_msg' => 'UNKNOWN.',
'trap' => 0,
'ts_0' => 1433949867,
'ts_1' => 1433949747,
'ts_2' => 1433949687,
'ts_3' => 1433949627,
'ts_4' => 1433949567,
'ts_5' => 1433949507,
'ts_6' => 1433949447,
'ts_7' => 1433949387,
'ts_8' => 1433949327,
'ts_9' => 1433949267
},
{
'acknowledged' => 'N/A',
'check_type' => 'ACTIVE',
'current_attempt' => 1,
'deleted' => 0,
'dimensions' => {
'cluster' => 'cluster',
'service' => 'Production_Deployment',
'host' => 'hostfoobar'
},
'flapping' => 'N/A',
'frequency' => 0,
'hidden' => 0,
'last_ack_time' => 0,
'last_check_time' => 1432875270,
'last_comment_time' => 0,
'last_delete_time' => 0,
'last_downtime_time' => 0,
'last_notif_time' => 0,
'last_state_change' => 1432875270,
'last_status_change' => 1432875270,
'max_attempt' => 1,
'msg_length' => 10,
'notification_enabled' => 'ENABLED',
'num_comments' => 0,
'quality_metric' => 0,
's_0' => 0,
's_1' => 0,
's_2' => 0,
's_3' => 0,
's_4' => 0,
's_5' => 0,
's_6' => 0,
's_7' => 0,
's_8' => 0,
's_9' => 0,
'scheduled_downtime' => 'NO',
'source' => '',
'state' => 'HARD',
'status' => 'OK',
'status_msg' => 'status ok',
'trap' => 1,
'ts_0' => 1432875270,
'ts_1' => 0,
'ts_2' => 0,
'ts_3' => 0,
'ts_4' => 0,
'ts_5' => 0,
'ts_6' => 0,
'ts_7' => 0,
'ts_8' => 0,
'ts_9' => 0
}
]
];
答案 0 :(得分:1)
下面是一个脚本,显示了如何以Perl惯用方式迭代循环。它并没有比第三级更深入,但它应该足以给你一些好主意。在while()循环中,它显示了如何取消引用整个内部哈希而不需要增量器,并显示如何正确地取消引用和访问结构中的各个项目。
编辑:我们发现OP实际上有一系列哈希数组。代码已更新。
#!/usr/bin/perl
use warnings;
use strict;
my @aoh = (
[
{
1 => 'a',
2 => 'b',
3 => 'c',
},
{
'z' => 26,
'd' => {
100 => '3rd level',
},
'y' => 25,
'x' => 24,
},
],
);
for my $element (@aoh){
for my $level_2_element (@{ $element }){
while (my ($key, $value) = (each %{ $level_2_element })){
if (ref($value) eq 'HASH'){
while (my ($k, $v) = (each %{$value})){
print "$key: -- $k: $v\n";
}
}
else {
print "$key: $value\n";
}
}
}
}
print "Element 0 0, key 1: $aoh[0]->[0]{1}\n";
print "Element 0 1, key x: $aoh[0]->[1]{x}\n";