实例平均 CPU 利用率未显示,但其他所有内容都显示? [英] Instance Average CPU Utilization not displaying but everything else is?
问题描述
目前我有一个函数可以记录正在运行的实例的平均 CPUUtilization.但问题是这个 cloudwatch 功能即使在设置了 time.sleep 之后,它也会在配置 CPU 利用率时为实例开始运行提供时间,因为它仍然没有显示平均 CPU 利用率,如下面的错误消息所示.
Currently I have a function that records the average CPUUtilization of a running Instance. But the problem is this cloudwatch function even after having a time.sleep put within to give time for the Instance to start running while it configures the CPU Utilization is that it still doesn't display the Average CPU Utilization as shown below in the error message.
#!/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
推荐答案
Period
通常会设置为 5 分钟,除非您为实例启用了详细监控.然后也可以设置为1分钟.
Period
usually would be set to 5 minutes, unless you have enabled detailed monitoring for the instance. Then it can be set to 1 minute as well.
您的 StartTime
和 EndTime
仅相隔 5 分钟.这么短的时间跨度里可能没有指标点.
Your StartTime
and EndTime
is only 5 minute apart. There maybe no metric points in such a short time span.
这篇关于实例平均 CPU 利用率未显示,但其他所有内容都显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!