こんな感じで取得できます。
与える値と返ってくる値の中で、手に入れたい値(例えば対象のメトリクスの最新の数値)
を取り出すまでに苦労しました。
get_cloudwatch_rds.rb
#!/usr/bin/env ruby
require 'aws-sdk'
ACCESS_KEY_ID = "YOUR_AWS_KEY_ID"
SECRET_ACCESS_KEY = "YOUR_AWS_SECRET_KEY"
ENDPOINT = "YOURENDPOINT"
cw = AWS::CloudWatch.new(
:access_key_id => ACCESS_KEY_ID,
:secret_access_key => SECRET_ACCESS_KEY,
:cloud_watch_endpoint => ENDPOINT
).client
statistics = cw.get_metric_statistics({
:namespace => 'AWS/RDS',
:metric_name => 'CPUUtilization',
# default RDS metric
# CPUUtilization, FreeStorageSpace, DiskQueueDepth, FreeableMemory, SwapUsage, ReadIOPS, ReadLatency, ReadThroughput, WriteIOPS, WriteLatency, WriteThroughput
dimensions_name DBInstanceIdentifier
:statistics => ['Average'],
:dimensions => [
{ :name => "DBInstanceIdentifier", :value => "RDSNAME" },
],
:start_time => (Time.now - 120).iso8601,
:end_time => Time.now.iso8601,
:period => 60
})
# p statistics[:datapoints][0][:average]
p statistics
get_cloudwatch_ec2.rb
#!/usr/bin/env ruby
require 'aws-sdk'
ACCESS_KEY_ID = "YOUR_AWS_KEY_ID"
SECRET_ACCESS_KEY = "YOUR_AWS_SECRET_KEY"
ENDPOINT = "YOURENDPOINT"
cw = AWS::CloudWatch.new(
:access_key_id => ACCESS_KEY_ID,
:secret_access_key => SECRET_ACCESS_KEY,
:cloud_watch_endpoint => ENDPOINT
).client
statistics = cw.get_metric_statistics({
:namespace => 'AWS/EC2',
:metric_name => 'CPUUtilization',
# default EC2 metric
# CPUUtilization, FreeStorageSpace, DiskQueueDepth, FreeableMemory, SwapUsage, ReadIOPS, ReadLatency, ReadThroughput, WriteIOPS, WriteLatency, WriteThroughput
:statistics => ['Average'],
:dimensions => [
{ :name => "InstanceId", :value => "YOURINSTANCEID" },
],
:start_time => (Time.now - 120).iso8601,
:end_time => Time.now.iso8601,
:period => 60
})
p statistics[:datapoints][0][:average]
# p statistics