0

使用适用于 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 难倒我。

我感谢任何人提供的任何帮助。

彼得

4

2 回答 2

3

您的 :dimensions 选项哈希需要像这样格式化:

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

亚马逊的SDK 文档试图将其解释为“哈希数组”。在 Ruby 中相当少见,而且很难弄清楚,至少对我来说是这样。

于 2013-07-21T14:08:40.187 回答
0

对于第 2 版aws-sdk

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

要获得统计数据,

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
})
于 2017-03-03T12:28:34.380 回答