开发者

How do I pull EC2 stats through Cloudwatch with the amazon-ec2 gem?

开发者 https://www.devze.com 2023-04-03 07:02 出处:网络
I\'m not sure what I\'m doing wrong, but I\'m having trou开发者_开发技巧ble using this gem to pull EC2 statistics: https://github.com/grempe/amazon-ec2

I'm not sure what I'm doing wrong, but I'm having trou开发者_开发技巧ble using this gem to pull EC2 statistics: https://github.com/grempe/amazon-ec2

I'm able to connect to my EC2 instances through Cloudwatch:

@cw = AWS::Cloudwatch::Base.new(:access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_KEY_ID)

I can see all the metrics available to me:

@cw.list_metrics

But when I try and use the get_metric_statistics method, I can't figure out what option parameters reference the actual metric fields.

# Fails
@cw.get_metric_statistics(namespace: 'AWS/EC2', measure_name: 'CPUUtilization', statistics: "Average")

I get a generic "NoMethodError: undefined method `elements' for nil:NilClass" error and I can't find out how to properly use get_metric_statistics(). Does anyone have any example code they have used to do similar things? It's the 'statistics' and 'dimensions' parameters that I'm confused about.

If I can supply any further information, let me know.


select server base on your instance in which zone

Amazon CloudWatch Endpoints
US-East (Northern Virginia)         monitoring.us-east-1.amazonaws.com
US-West (Northern California)       monitoring.us-west-1.amazonaws.com
EU (Ireland)                        monitoring.eu-west-1.amazonaws.com
Asia Pacific (Singapore)            monitoring.ap-southeast-1.amazonaws.com

in your example u miss to set server zone that why its not working 



 @cw = AWS::Cloudwatch::Base.new(:access_key_id => 'key', :secret_access_key => 'key',:server => 'monitoring.eu-west-1.amazonaws.com')
    instance_id ='instanceid'
    time = Time.new
    time.gmtime
    @result = @cw.get_metric_statistics(namespace: 'AWS/EC2', measure_name: 'CPUUtilization',  statistics: 'Average', start_time: time-1000, dimensions: "InstanceId=#{instance_id}")

try with this code and share your output. this code working for me


So I figured this one out after inspecting the gem source, it should look like this.

@cw.get_metric_statistics(namespace: 'AWS/EC2', measure_name: 'CPUUtilization',  statistics: 'Average', start_time: 1.minute.ago.to_time, dimensions: "InstanceId=#{instance_id}")
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号