Question

Using the Ruby SDK for AWS/CloudWatch. I'm trying to simply get the average CPU utilization of a specific RDS instance.

metric = AWS::CloudWatch::Metric.new('AWS/RDS', 'CPUUtilization', :dimensions => {[ :name => 'DBInstanceIdentifier'], [:value => 'my_db_instance' ] })
puts metric.metric_name # shows the right name
puts metric.namespace   # shows the right namespace

stats = metric.statistics(:start_time => Time.now - 300, :statistics => ['Average'])

The last line fails with:

/Library/Ruby/Gems/1.8/gems/aws-sdk-1.12.0/lib/aws/core/option_grammar.rb:337:in `validate': expected hash value for member 1 of option dimensions (AWS::Core::OptionGrammar::FormatError)

I've also tried:

stats = metric.statistics(:options => {[:start_time => Time.now - 300], [:statistics => ['Average']]})

And got the same error.

stats = metric.statistics({[:start_time => Time.now - 300], [:statistics => ['Average']]})

Gives:

/Library/Ruby/Gems/1.8/gems/aws-sdk-1.12.0/lib/aws/core/option_grammar.rb:588:in `validate': unexpected option start_timeSat Jul 20 11:27:14 -0400 2013 (ArgumentError)

Following the AWS documentation, I was able to get data back from code like this:

metric = CloudWatch::Metric.new('AWS/RDS', 'CPUUtilization')

stats = metric.statistics(:start_time => Time.now - 3600, :end_time => Time.now, :statistics => ['Average'])

stats.label #=> 'some-label'
stats.each do |datapoint|
  puts datapoint 
end

But I don't see how to narrow this to the specific instance I'm interested in, as the datapoint hash does not say what instance it's from.

I'm pretty new to Ruby, but have been having success using the EC2, ELB, AutoScaling parts of the SDK. CloudWatch is stumping me.

I appreciate any help anyone has to offer.

Peter

Was it helpful?

Solution

Your :dimensions option hash needs to be formatted like this:

metric = AWS::CloudWatch::Metric.new(
  'AWS/RDS',
  'CPUUtilization',
  :dimensions => [{ :name => 'DBInstanceIdentifier', :value => 'my_db_instance' }]
)

Amazon's SDK doc tries to explain this as "An array of hashes". Quite uncommon in Ruby and hard to figure out, at least for me.

OTHER TIPS

For Version 2 of aws-sdk

cloudwatch_metric = Aws::CloudWatch::Metric.new(
  'AWS/RDS',
  'CPUUtilization'
)

To get the statistics,

metrics = cloudwatch_metric.get_statistics({
  dimensions: [
    {
      name: "DBInstanceIdentifier", # required
      value: "my_db_instance", # required
    },
  ],
  start_time: Time.now - 30000, # required
  end_time: Time.now, # required
  period: 1, # required
  statistics: ["Average"], # accepts SampleCount, Average, Sum, Minimum, Maximum
  # unit: "Seconds", # accepts Seconds, Microseconds, Milliseconds, Bytes, Kilobytes, Megabytes, Gigabytes, Terabytes, Bits, Kilobits, Megabits, Gigabits, Terabits, Percent, Count, Bytes/Second, Kilobytes/Second, Megabytes/Second, Gigabytes/Second, Terabytes/Second, Bits/Second, Kilobits/Second, Megabits/Second, Gigabits/Second, Terabits/Second, Count/Second, None
})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top