使用适用于 AWS/CloudWatch 的 Ruby 开发工具包。我试图简单地获取特定 RDS 实例的平均 CPU 利用率。
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'])
最后一行失败:
/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)
我也试过:
stats = metric.statistics(:options => {[:start_time => Time.now - 300], [:statistics => ['Average']]})
并得到同样的错误。
stats = metric.statistics({[:start_time => Time.now - 300], [:statistics => ['Average']]})
给出:
/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)
按照 AWS 文档,我能够从如下代码中获取数据:
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
但我不知道如何将其缩小到我感兴趣的特定实例,因为数据点哈希没有说明它来自哪个实例。
我对 Ruby 还很陌生,但使用 SDK 的 EC2、ELB、AutoScaling 部分已经取得了成功。CloudWatch 难倒我。
我感谢任何人提供的任何帮助。
彼得