using get_metric_statistics in simple_aws using ruby

2.3k Views Asked by At

I am trying to explore the simple_aws gem. When I connect to cloudwatch to get the metric statistics I get an error as follows:

cw.get_metric_statistics(
 :metric_name => metric_name,
 :period => period,
 :start_time => start_time,
 :end_time => end_time,
 :statistics => "Average",
 :namespace => "AWS/EC2"
)

SimpleAWS::UnsuccessfulResponse: MissingParameter (400):

   The parameter Namespace is required.
   The parameter MetricName is required.  
   The parameter StartTime is required.  
   The parameter EndTime is required.  
   The parameter Period is required.  
   The parameter Statistics is required. 

Later, I tried this:

cw.get_metric_statistics(
options => [
  {:metric_name=>"CPUUtilization",
   :period=>60,
   :start_time => Time.now()-86400,
   :end_time => Time.now()-3600,
   :statistics => "Average"
  }
]
)

But got the following error:

URI::InvalidComponentError: bad component(expected query component): 

Action=GetMetricStatistics&{:metric_name=>"CPUUtilization"}.1.metric_name=CPUUtilization&{:metric_name=>"CPUUtilization"}.1.period=60&{:metric_name=>"CPUUtilization"}.1.start_time=2012-05-06%2014%3A25%3A28%20%2B0530&{:metric_name=>"CPUUtilization"}.1.end_time=2012-05-07%2013%3A25%3A28%20%2B0530&{:metric_name=>"CPUUtilization"}.1.statistics=Average&AWSAccessKeyId=AccessKey&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2012-05-07T08%3A55%3A28Z&Version=2010-08-01&Signature=Signature

one more try:

cw.get_metric_statistics(
namespace: 'AWS/EC2',
measure_name: 'CPUUtilization',
statistics: 'Average',
start_time: time-1000,
dimensions: "InstanceId=#{instance_id}"

)

ArgumentError: comparison of Array with Array failed

Can anybody please help find the correct syntax for issuing this command.

2

There are 2 best solutions below

0
On

I found that this works;

lat = cw.get_metric_statistics(
  'MetricName' => 'Latency',
  'Period' => 60,
  'StartTime' => (Time.now() - 3600).iso8601,
  'EndTime' => Time.now().iso8601,
  'Statistics.member.1' => "Average",
  'Namespace' => "AWS/ELB",
  'Unit' => 'Seconds'
)

Firstly that the datetime is required in ISO8601 format, secondly that the parameters need to be cased correctly, thirdly the Unit parameter is required, and finally that Statistics needed a namespace(?) after it.

Hope this helps, even if it is a little late.

0
On
result = cw.get_metric_statistics(step,
      start_time,
      end_time,
      metric,
      'AWS/RDS',
      'Average',
      dimensions={'DBInstanceIdentifier': [indentifier]})

This also worked for me