首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >实例平均CPU利用率没有显示,但其他都显示了?

问实例平均CPU利用率没有显示,但其他都显示了?
EN

Stack Overflow用户
提问于 2021-07-17 08:26:11
回答 1查看 55关注 0票数 0

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

代码语言:javascript
复制
#!/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)
代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2021-07-17 11:35:35

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

您的StartTime和EndTime之间只有5分钟的距离。在这么短的时间跨度内可能没有度量点。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68416488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档