简体   繁体   English

python connect_cloudwatch不同的区域

[英]python connect_cloudwatch different region

I am trying to connect and dynamically get ALL metrics for ALL services available in AWS CloudWatch in a particular AWS Region: 我正在尝试连接并动态获取特定AWS区域中AWS CloudWatch中可用的所有服务的所有指标:

try:
    print "Connecting to region %s" % (args.region)
    conn = boto.connect_cloudwatch()
    #conn = boto.ec2.cloudwatch.connect_to_region(args.region)
    print "Retrieving all CloudWatch metrics"
    metrics = conn.list_metrics()
    print "Collected %d metrics" % (len(metrics))
except boto.exception.BotoServerError, error:
    print "Failed to connect to AWS\n  ->%s" % (error)
    sys.exit(1)

Unfortunately boto.ec2.cloudwatch.connect_to_region(args.region) give me only EC2 metrics from a region, and I couldn't figure out from the documentation how to specify a region to conn = boto.connect_cloudwatch() . 不幸的是, boto.ec2.cloudwatch.connect_to_region(args.region)仅给我一个区域的EC2指标,并且我无法从文档中找出如何将区域指定为conn = boto.connect_cloudwatch() Please help! 请帮忙!

You can choose the Cloudwatch region in the .boto config file. 您可以在.boto配置文件中选择Cloudwatch区域。 For example: 例如:

[Credentials]
aws_access_key_id = <your aws access key>
aws_secret_access_key = <your aws secret access key>

[Boto]
cloudwatch_region_name = us-west-2
cloudwatch_region_endpoint = monitoring.us-west-2.amazonaws.com

The above will connect you to us-west-2 when you run: 上面的代码将在您运行时将您连接到us-west-2:

import boto

conn = boto.connect_cloudwatch()

Hope it helps. 希望能帮助到你。

The code you show above looks fine. 您上面显示的代码看起来不错。 The list_metrics method will return all of the metrics which actually have data associated with them in the current account. list_metrics方法将返回当前帐户中实际上具有与之相关联的数据的所有指标。 If you are only seeing EC2 metrics then that means that your account only has EC2 data collected in CloudWatch for that region. 如果您仅看到EC2指标,则意味着您的帐户仅在CloudWatch中收集了该区域的EC2数据。

boto.connect_cloudwatch() will do exactly the same as boto.ec2.cloudwatch.connect_region('eu-west-1') but without asking for the region. boto.connect_cloudwatch()的功能与boto.ec2.cloudwatch.connect_region('eu-west-1')完全相同,但无需询问该区域。 The region is used from the configuration in ~/.boto . ~/.boto的配置使用该区域。 The common way to specify the region is to use connect_region('myregion') or just set it in the configuration if you do not work across multiple regions. 指定区域的常用方法是使用connect_region('myregion')或者如果您不能跨多个区域工作,则只需在配置中进行设置即可。

So if you don't find your metrics in the connection object created by boto.ec2.cloudwatch.connect_region('eu-west-1') you won't find it in the other one. 因此,如果在由boto.ec2.cloudwatch.connect_region('eu-west-1')创建的连接对象中找不到指标,则不会在另一个对象中找到它。 Both cases creates a boto.ec2.cloudwatch.CloudWatchConnection object. 两种情况都创建一个boto.ec2.cloudwatch.CloudWatchConnection对象。 ( Boto documentation ) Boto文档

As far as I understand the documentation boto currently does not support other metrics than EC2-metrics. 据我了解,文档boto当前不支持EC2指标以外的其他指标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM