I'm running the script below to query a monitoring system and the results I get back are in array of hashes. What is the best way to access the individual hashes and their elements?
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";
}
The output I get querying the API is as below.
$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
}
];`
Here's the output of 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
}
]
];
Below is a script that shows how to iterate over a loop the Perl idiomatic way. It doesn't go deeper than a 3rd level, but it should be enough to give you some good ideas. In the while() loop, it shows how to dereference an inner hash as a whole and without needing the incrementor, and shows how to properly dereference and access individual items within the structure.
EDIT: We've found OP actually has an array of arrays of hashes. Code updated.