boto 相当于 aws 客户端命令

问题描述 投票:0回答:2

我的这个命令按预期工作。 但我想用 boto 代替。

aws cloudwatch get-metric-statistics \
    --region ap-south-1 \
    --namespace AWS/RDS \
    --metric-name DBLoad  \
    --period 60 \
    --statistics Average \
    --start-time 2023-06-12T21:00:00Z \
    --end-time 2023-06-12T23:59:00Z \
    --dimensions Name=DBInstanceIdentifier,Value=sql-rds

boto支持所有参数吗?

boto3 aws-sdk amazon-cloudwatch boto
2个回答
2
投票

Boto3 中提供了 AWS CloudWatch 功能,包括 get_metric_statistics 操作

请参阅此处:Boto3 文档 - CloudWatch 客户端 - get_metric_statistics

使用 Boto3 - 代码为:

import boto3
from datetime import datetime, timedelta

client = boto3.client('cloudwatch', region_name='ap-south-1')

response = client.get_metric_statistics(
    Namespace='AWS/RDS',
    MetricName='DBLoad',
    Dimensions=[
        {
            'Name': 'DBInstanceIdentifier',
            'Value': 'sql-rds'
        },
    ],
    StartTime=datetime(2023, 6, 12, 21, 0, 0),
    EndTime=datetime(2023, 6, 12, 23, 59, 0),
    Period=60,
    Statistics=['Average']
)

print(response)

注意。
Boto 不再由 AWS 官方维护
Boto 文档可能不是最新的
它不提供与 AWS 相同级别的支持 服务为 Boto3。

但是,如果您需要使用 Boto,文档位于:boto.ec2.cloudwatch

使用 Boto - 代码为:

import boto

conn = boto.connect_cloudwatch()
region = 'ap-south-1'

namespace = 'AWS/RDS'
metric_name = 'DBLoad'
dimensions = {'DBInstanceIdentifier': 'sql-rds'}

start_time = '2023-06-12T21:00:00Z'
end_time = '2023-06-12T23:59:00Z'

period = 60
statistics = ['Average']

response = conn.get_metric_statistics(
    period=period,
    start_time=start_time,
    end_time=end_time,
    metric_name=metric_name,
    namespace=namespace,
    statistics=statistics,
    dimensions=dimensions,
    region=region
)

print(response)

1
投票

您可以使用 boto3-

这样做
import boto3
import matplotlib.pyplot as plt
from tabulate import tabulate
from unskript.enums.aws_k8s_enums import StatisticsType
from datetime import datetime, timedelta

timestamps = []
values = []
cloudwatchClient = boto3.client('cloudwatch', region_name='ap-south-1')
start_time = datetime(2023, 6, 12, 21, 0, 0)
end_time = datetime(2023, 6, 12, 23, 59, 0)
dimensions = [{'Name': 'name-1', ... }]
metric_name = 'DBLoad' # in your case
period = 60 #The granularity, in seconds, of the returned data points.
statistics = ['Average'] #in your case

res = cloudwatchClient.get_metric_data(
      MetricDataQueries=[
        {
            'Id': metric_name.lower(),
            'MetricStat': {
                'Metric': {
                    'Namespace': 'AWS/RDS',
                    'MetricName': metric_name,
                    'Dimensions': dimensions
                },
                'Period': period,
                'Stat': statistics,
            },
        },
    ],
    StartTime= start_time,
    EndTime=end_time,
    ScanBy='TimestampAscending'
)

for timestamp in res['MetricDataResults'][0]['Timestamps']:
    timestamps.append(timestamp)
for value in res['MetricDataResults'][0]['Values']:
    values.append(value)

timestamps.sort()
values.sort()

plt.plot_date(timestamps, values, "-o")

data = []
for dt, val in zip(
    res['MetricDataResults'][0]['Timestamps'],
    res['MetricDataResults'][0]['Values']
    ):
    data.append([dt.strftime('%Y-%m-%d::%H-%M'), val])
head = ["Timestamp", "Value"]
table = tabulate(data, headers=head, tablefmt="grid")

print(table)
© www.soinside.com 2019 - 2024. All rights reserved.