Last active
December 14, 2015 00:59
-
-
Save mechamogera/5003334 to your computer and use it in GitHub Desktop.
CloudWatchにカスタムメトリックスを登録するRubyスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| source 'https://rubygems.org' | |
| gem 'aws-sdk' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'rubygems' | |
| require 'aws-sdk' | |
| require 'optparse' | |
| class PutMetricRequest < Hash | |
| [:metric_name, :timestamp, :value, :unit].each do |metric_uni_data| | |
| define_method("#{metric_uni_data}=") { |v| metric_data[metric_uni_data] = v } | |
| end | |
| def initialize(*args) | |
| super(*args) | |
| end | |
| def namespace=(namespace) | |
| self[:namespace] = namespace | |
| end | |
| def add_dimension(key, value) | |
| metric_data[:dimensions] ||= [] | |
| metric_data[:dimensions] << {:name => key, :value => value} | |
| end | |
| def add_statistic_value(key, value) | |
| metric_data[:statistic_values] ||= {} | |
| metric_data[:statistic_values][key.gsub(/([a-z])([A-Z])/) { |x| "#{$1}_#{$2}" }.downcase] = value | |
| end | |
| private | |
| def metric_data | |
| self[:metric_data] ||= [{}] | |
| self[:metric_data][0] | |
| end | |
| end | |
| request = PutMetricRequest.new | |
| access_key_id = secret_access_key = nil | |
| endpoint = 'monitoring.us-east-1.amazonaws.com' | |
| ENDPOINTS = ["us-east-1", "us-west-2", "us-west-1", "eu-west-1", "ap-southeast-1", "ap-southeast-2", "ap-northeast-1", "sa-east-1"] | |
| UNITS = %w[Seconds Microseconds Milliseconds Bytes Kilobytes Megabytes] + | |
| %w[Gigabytes Terabytes Bits Kilobits Megabits Gigabits Terabits] + | |
| %w[Percent Count Bytes/Second Kilobytes/Second Megabytes/Second] + | |
| %w[Gigabytes/Second Terabytes/Second Bits/Second Kilobits/Second] + | |
| %w[Megabits/Second Gigabits/Second Terabits/Second Count/Second None] | |
| opt = OptionParser.new | |
| opt.on("-d", "--dimensions VALUE", "Form:key1=value1,key2=value2...") do |v| | |
| v.split(",").each { |x| request.add_dimension(*x.split("=", 2)) } | |
| end | |
| opt.on("-m", "--metric-name VALUE", "Required") { |v| request.metric_name = v } | |
| opt.on("-n", "--namespace VALUE", "Required") { |v| request.namespace = v } | |
| opt.on("-s", "--statistic-values VALUE", "Form:SampleCount=value, Sum=value, Maximum=value, Minimum=value") do |v| | |
| v.split(/\s*,\s*/).each do |item| | |
| request.add_statistic_value(*item.split("=")) | |
| end | |
| end | |
| opt.on("-t", "--timestamp VALUE", "Example:2009-11-25T19:00:00+00:00Z") { |v| request.timestamp = v } | |
| opt.on("-u", "--unit VALUE", "Candidate:#{UNITS.join(" ")}") { |v| request.unit = v } | |
| opt.on("-v", "--value VALUE") { |v| request.value = v } | |
| opt.on("--access-key-id VALUE") { |v| access_key_id = v } | |
| opt.on("--secret-access-key VALUE") { |v| secret_access_key = v } | |
| opt.on("-e", '--endpoint=VAL', "Default:us-east-1, Candidate:#{ENDPOINTS.join(" ")}") do |v| | |
| endpoint = "monitoring.#{v}.amazonaws.com" | |
| end | |
| opt.parse!(ARGV) | |
| cw = AWS::CloudWatch.new( | |
| :proxy_uri => ENV['HTTP_PROXY'] || ENV['http_proxy'], | |
| :access_key_id => access_key_id, | |
| :secret_access_key => secret_access_key, | |
| :cloud_watch_endpoint => endpoint | |
| ) | |
| cw.put_metric_data(request) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment