【问题标题】:Instance Average CPU Utilization not displaying but everything else is?实例平均 CPU 利用率未显示,但其他所有内容都显示了吗?
【发布时间】:2026-02-01 22:55:01
【问题描述】:

目前我有一个记录正在运行的实例的平均 CPUUtilization 的函数。 但问题是这个 cloudwatch 功能即使在配置 CPU 利用率时放置了 time.sleep 以让实例有时间开始运行,但它仍然不显示平均 CPU 利用率,如下面的错误消息所示.

#!/usr/bin/env python3
import sys
import boto3
import time
ec2 = boto3.resource('ec2', region_name = 'eu-west-1')
s3 = boto3.resource('s3')
keyname = 'key1.pem'
s3_resource = boto3.resource('s3')
user_data = '''#!/bin/bash
yum update -y
yum install httpd -y
systemctl enable httpd
systemctl start httpd'''

try:
        resp = s3.create_bucket(ACL='private',Bucket='buket2',C$
        print (resp)
except Exception as error:
    print (error)

try:
        s3_resource.Bucket('buket2').upload_file('image.jpg', 'image$

try:
        gg = ec2.create_security_group(GroupName='Server', Description = '$
        print (gg)
except Exception as error:
    print (error)

response = sg.authorize_ingress(
    IpPermissions=[
        {
            "FromPort": 22,
            "ToPort": 22,
            "IpProtocol": "tcp",
            "IpRanges": [
                {"CidrIp": "0.0.0.0/0", "Description": "Server"},
            ],
        },
        {
            "FromPort": 80,
            "ToPort": 80,
            "IpProtocol": "tcp",
            "IpRanges": [
                {"CidrIp": "0.0.0.0/0", "Description": "Server1"},
            ],
        },
    ],
)
instance = ec2.create_instances(
 ImageId='ami-03odd1b743b23e5d2',
 MinCount=1,
 MaxCount=1,
 InstanceType='t2.nano',
 KeyName = 'key1.pem',
 UserData = user_data, 
 SecurityGroupIds=[sg.group_id] 
)


from datetime import datetime, timedelta
time.sleep(390)
client = boto3.client('cloudwatch')
response = client.get_metric_statistics(
            Namespace='AWS/EC2',
            MetricName='CPUUtilization',
            Dimensions=[
                {
                'Name': 'AMIID',
                'Value': 'ami-03odd1b743b23e5d2'
                },
            ],
            StartTime=datetime(2021, 7, 17) - timedelta(seconds=300),
            EndTime=datetime(2021, 7, 17),
            Period=300,
            Statistics=[
                'Average',
            ],
            Unit='Percent'
        )
print(response)

for cpu in response['Datapoints']:
  print(cpu)
s3.Bucket(name='buket2')
ec2.SecurityGroup(id='sg-06b84927ae5rd3ad1')
{'Label': 'CPUUtilization', 'Datapoints': [], 'ResponseMetadata': {'RequestId': 'ba4352d5-67ee-4d51-b03f-d1c532dbfe7', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': 'ba421b45-63dd-4d51-b03f-d14212e2fe7', 'content-type': 'text/xml', 'content-length': '337', 'date': 'Sun, 18 Jul 2021 00:26:57 GMT'}, 'RetryAttempts': 0}}
sg-06b84927ae5rd3ad1

【问题讨论】:

  • 我看到你提出了新问题。但是,如果这个老问题的当前答案有帮助,我们将不胜感激。

标签: amazon-web-services amazon-ec2 boto3 amazon-cloudwatch


【解决方案1】:

Period 通常会设置为 5 分钟,除非您为实例启用了详细监控。然后也可以设置为 1 分钟。

您的 StartTimeEndTime 仅相隔 5 分钟。这么短的时间跨度可能没有指标点。

【讨论】:

  • 问题是我的 create_instance 脚本中有这个函数,所以当我执行它时,我认为它永远没有足够的时间来显示自启动以来的平均 CPU 利用率?基本上我需要使用 CloudWatch 监控实例指标,例如 CPU 利用率,因此当我执行脚本时,我还需要它返回 CPU 利用率百分比或数字。如何在保持自动化的同时实现这一目标?
  • @Decjk 您必须在创建实例和检查指标之间引入延迟。
  • 是否有我可以尝试实施的文档?
  • @Decjk time.sleep() 显示第一个指标需要多少分钟。至少 5 分钟进行基本监控。
  • 是的,我用了 6 分钟,CPU 利用率在 5 分钟开始,并在第 6 分钟完成执行实例,它仍然显示与帖子中相同的消息,它没有t 出于某种原因添加额外的“平均:”?
最近更新 更多