Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Paws result how to process?

by Lotus1 (Vicar)
on Jul 31, 2018 at 18:15 UTC ( [id://1219590]=note: print w/replies, xml ) Need Help??


in reply to Paws result how to process?

It looks like in Paws::CloudWatch::DescribeAlarmsOutput it tells you that object contains an attribute that holds an arrayref where each element of the array is an object of the type Paws::CloudWatch::MetricAlarm. So you can deref the arrayref and loop through with foreach and inspect the attributes of MetricAlarm.

Update: Try this to see what you get.

2nd Update: I forgot to access the attribute MetricAlarms. This is all untested but should give you an idea.

3rd Update: Added print AlarmName attribute.

Update: Fixed brackets after @.

sub describe_alarms { my ($alarm_name_prefix) = @_; my $obj = Paws->service( 'CloudWatch', region => 'us-east-1' ); my $result = $obj->DescribeAlarms( AlarmNamePrefix => $alarm_name_prefix, ); #foreach(@$result){ Updated foreach( @{ $result->MetricAlarms } ){ print "-"x79,"\n"; print "**AlarmName** : ", $_->AlarmName,"\n"; print Dumper($_); } print "\n***\n--number of elements: ", scalar @{ $result->MetricAl +arms }, "\n"; return $result; }

Replies are listed 'Best First'.
Re^2: Paws result how to process?
by bigswifty00000 (Beadle) on Aug 01, 2018 at 12:55 UTC

    I must be missing something....

    Thanks again for the help!

    below is the dump from Data::Dumper

    and the modified function produces these errors....

    ERRORS: Scalar found where operator expected at ./describe_alarms.pl line 97, +near "@($result" (#1) (S syntax) The Perl lexer knows whether to expect a term or an ope +rator. If it sees what it knows to be a term when it was expecting to see + an operator, it gives you this warning. Usually it indicates that an operator or delimiter was omitted, such as a semicolon. (Missing operator before $result?) (#2) (S syntax) This is an educated guess made in conjunction with the +message "%s found where operator expected". Often the missing operator is + a comma. syntax error at ./describe_alarms.pl line 97, near "@($result" Execution of ./describe_alarms.pl aborted due to compilation errors (# +3) (F) Probably means you had a syntax error. Common reasons include +: A keyword is misspelled. A semicolon is missing. A comma is missing. An opening or closing parenthesis is missing. An opening or closing brace is missing. A closing quote is missing. Often there will be another error message associated with the synt +ax error giving more information. (Sometimes it helps to turn on -w. +) The error message itself often tells you where it was in the line +when it decided to give up. Sometimes the actual error is several toke +ns before this, because Perl is good at understanding random input. Occasionally the line number may be misleading, and once in a blue + moon the only way to figure out what's triggering the error is to call perl -c repeatedly, chopping away half the program each time to se +e if the error went away. Sort of the cybernetic version of 20 ques +tions. Uncaught exception from user code: syntax error at ./describe_alarms.pl line 97, near "@($result" Execution of ./describe_alarms.pl aborted due to compilation error +s.
    MODIFIED FUNCTION: sub describe_alarms { my ($alarm_name_prefix) = @_; my $result; my $obj = Paws->service( 'CloudWatch', region => 'us-east-1' ); $result = $obj->DescribeAlarms( AlarmNamePrefix => $alarm_name_prefix, ); # print Dumper($result); print Dumper(ref @($result->MetricAlarms)); #print Dumper(@($result->MetricAlarms)); exit; =pod foreach ( @($result->MetricAlarms) ){ print "-"x79,"\n"; print "**AlarmName** : ", $_->AlarmName,"\n"; print Dumper($_); } print "\n***\n--number of elements: ", scalar @($result->MetricAla +rms), "\n"; #return $result; =cut return $result; }
    DUMP: $VAR1 = bless( { '_request_id' => '3eeadc30-9588-11e8-9cf2-e1b5a579da44', 'MetricAlarms' => [ bless( { 'AlarmArn' => 'arn:aws:cloudwatch:us-east-1:xxxxxxxxxxxxx:alarm:pubtech-best-gte-90- +dev', 'StateReason' => 'Threshold Crossed: 1 out of the last 2 datapoints [0.152614819450015 +(01/08/18 11:31:00)] was not greater than or equal to the threshold ( +90.0) (minimum 1 datapoint for ALARM -> OK transition).', 'AlarmActions' => [ 'arn:aws:sns:us-east-1:xxxxxxxx:bestsellers_prd_best-alarm2' ], 'AlarmName' => 'pubtech-best-gte-90-dev', 'AlarmDescription' => 'Alarm when volume is greater than or equal 90 p +ercent', 'Period' => '300', 'ActionsEnabled' => 1, 'Dimensions' => [ bless( { 'Name' => 'Filesystem', 'Value' => '/dev/xvdf' }, 'Paws::CloudWatch::Dimension' ), bless( { 'Name' => 'MountPath', 'Value' => '/best' }, 'Paws::CloudWatch::Dimension' ), bless( { 'Value' => 'xxxxxxxxxxxxxxx', 'Name' => 'InstanceId' }, 'Paws::CloudWatch::Dimension' ) ], 'Statistic' => 'Average', 'Threshold' => '90.0', 'DatapointsToAlarm' => '2', 'Namespace' => 'System/Linux', 'ComparisonOperator' => 'GreaterThanOrEqualToThres +hold', 'StateReasonData' => '{"version":"1.0","queryDate":"2018-08-01T11:36:17.049+0000","startDat +e":"2018-08-01T11:31:00.000+0000","statistic":"Average","period":300, +"recentDatapoints":[0.152614819450015],"threshold":90.0}', 'StateValue' => 'OK', 'StateUpdatedTimestamp' => '2018-08-01T11:36:17.06 +2Z', 'EvaluationPeriods' => '2', 'TreatMissingData' => 'missing', 'MetricName' => 'DiskSpaceUtilization', 'AlarmConfigurationUpdatedTimestamp' => '2018-07-12T14:40:07.798Z' }, 'Paws::CloudWatch::MetricAlarm' ) ] }, 'Paws::CloudWatch::DescribeAlarmsOutput' );

      It was untested code so I messed up the brackets to the right of '@'. It needs to be curly braces '{}' in that case.

      foreach( @{$result->MetricAlarms} ){ print "-"x79,"\n"; print "**AlarmName** : ", $_->AlarmName,"\n"; print Dumper($_); } print "\n***\n--number of elements: ", scalar @{$result->MetricAla +rms}, "\n";

      Alternatively you can use an intermediate variable to hold the array ref before de-referencing it.

      my $ar_ref = $result->MetricAlarms; foreach( @$ar_ref ){ ...

      This article shows how to use the curly braces for dereferencing things. Also have a look at this node and this article in the Perl docs.

        I had replaced curly braces, but still didn't seem to work, but it does now!!

        Thanks Again!

Re^2: Paws result how to process?
by bigswifty00000 (Beadle) on Aug 01, 2018 at 11:34 UTC

    Thanks for the reply, I'll try it today!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1219590]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-24 01:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found