如何使用AWS Boto3获取Cloudwatch指标统计信息? [英] How can I use AWS Boto3 to get Cloudwatch metric statistics?
问题描述
我正在研究Python 3脚本,该脚本旨在使用Boto3库从AWS CloudFront获取S3空间利用率统计信息.
I'm working on a Python 3 script designed to get S3 space utilization statistics from AWS CloudFront using the Boto3 library.
我从AWS CLI开始,发现使用以下命令可以得到我想要的东西:
I started with the AWS CLI and found I could get what I'm after with a command like this:
aws cloudwatch get-metric-statistics --metric-name BucketSizeBytes --namespace AWS/S3 --start-time 2017-03-06T00:00:00Z --end-time 2017-03-07T00:00:00Z --statistics Average --unit Bytes --region us-west-2 --dimensions Name=BucketName,Value=foo-bar Name=StorageType,Value=StandardStorage --period 86400 --output json
这将返回我期望的数据.现在,我想在Python 3/Boto3中做同样的事情.到目前为止,我的代码是:
This returns the data I would expect. Now I'd like to do the same thing in Python 3 / Boto3. My code thusfar is:
from datetime import datetime, timedelta
import boto3
seconds_in_one_day = 86400 # used for granularity
cloudwatch = boto3.client('cloudwatch')
response = cloudwatch.get_metric_statistics(
Namespace='AWS/S3',
Dimensions=[
{
'Name': 'BucketName',
'Value': 'foo-bar'
},
{
'Name': 'StorageType',
'Value': 'StandardStorage'
}
],
MetricName='BucketSizeBytes',
StartTime=datetime.now() - timedelta(days=7),
EndTime=datetime.now(),
Period=seconds_in_one_day,
Statistics=[
'Average'
],
Unit='Bytes'
)
print(response)
运行此命令时,我得到一个有效的响应,但没有数据点(这是一个空数组).它们似乎是相同的,只是Python方法似乎没有在命令行需要的区域放置该地方.
When I run this, I get a valid response but no datapoints (it's an empty array). They seem to be identical except the Python method doesn't seem to have a place for the region, where the command line requires it.
我尝试的另一件事:我的代码正在计算最后日期的日期,而不是在命令行中对其进行硬编码的日期.我确实尝试对日期进行硬编码,只是为了查看是否可以取回数据,结果是相同的.
One more thing I tried: my code is computing the dates for the last date versus the command line where they are hard coded. I did try hard coding the date just to see if I would get data back, and the result was the same.
所以我的问题是这些
我在Boto/Python中使用的方法是否等同于命令行? 假设它们是,我会丢失什么?
Is the method I'm using in Boto / Python equivalent to the command line? Assuming they are, what could I be missing?
推荐答案
我认为错误是您的命令cloudwatch = boto3.client('cloudwatch')
.默认区域为east-1.因此,您可以使用以下内容:
I think the error is your command cloudwatch = boto3.client('cloudwatch')
. The default region is east-1. So you could use something like this:
from datetime import datetime, timedelta
import boto3
def credentials_AWS (account):
if (account == 'account1'):
aws_access_key_id = "key id east"
aws_secret_access_key = 'east secret_access_key'
region_name = 'us-east-1'
elif (account == 'account2'):
aws_access_key_id = "key id west"
aws_secret_access_key = 'west secret_access_key'
region_name = 'us-west-2'
return aws_access_key_id, aws_secret_access_key, region_name
def connect_service_aws (service, aws_access_key_id, aws_secret_access_key, region_name):
aws_connected = boto3.client (service,
aws_access_key_id = aws_access_key_id,
aws_secret_access_key = aws_secret_access_key,
region_name = region_name)
return aws_connected
def get_metrics(account):
seconds_in_one_day = 86400 # used for granularity
#cloudwatch = boto3.client('cloudwatch')
aws_access_key_id, aws_secret_access_key, region_name = credentials_AWS (account)
cloudwatch = connect_service_aws ('cloudwatch', aws_access_key_id,
aws_secret_access_key, region_name)
response = cloudwatch.get_metric_statistics(
Namespace='AWS/S3',
Dimensions=[
{
'Name': 'BucketName',
'Value': 'foo-bar'
},
{
'Name': 'StorageType',
'Value': 'StandardStorage'
}
],
MetricName='BucketSizeBytes',
StartTime=datetime.now() - timedelta(days=7),
EndTime=datetime.now(),
Period=seconds_in_one_day,
Statistics=[
'Average'
],
Unit='Bytes'
)
print(response)
这篇关于如何使用AWS Boto3获取Cloudwatch指标统计信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!