13
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CloudWatchのAPI叩いてメトリクスを手に入れる

Last updated at Posted at 2013-02-26

こんな感じで取得できます。

与える値と返ってくる値の中で、手に入れたい値(例えば対象のメトリクスの最新の数値)
を取り出すまでに苦労しました。

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
13
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?