gpt4 book ai didi

amazon-web-services - 在 AWS 中为 EC2 实例创建警报

转载 作者:行者123 更新时间:2023-12-04 17:59:10 25 4
gpt4 key购买 nike

如何创建警报1) 当 EC2 实例运行时间过长时发出警报(比如 1 小时)。2)当 EC2 实例数量达到阈值时发出警报(比如一次 5 个实例)

另一个假设是,这些 EC2 实例是特定的。假设这些警报适用于实例名称以“test”开头的 EC2 实例。

当我尝试创建警报时,我没有在 Metrics 中看到此逻辑。标准指标包括 CPU 利用率、网络输入、网络输出等。

有没有办法通过定义我们的自定义指标或其他一些选项来创建此警报?

最佳答案

对于自动部署的实例,无法设置 CloudWatch 警报,因为您不知道实例 ID。设置警报的唯一方法是创建一个 AWS Lambda 函数来收集所有正在运行的实例并将它们的启动时间与指定的超时时间进行比较。

lambda 函数由 CloudWatch - Event – Rule 周期性触发.

使用tags为不同的机器指定不同的运行持续时间。例如,您的启动工具应该使用键值“Test”标记实例

请注意此代码完全保证!这更像是一个例子。

import boto3
import datetime
import json
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

ec2_client = boto3.client('ec2')

INSTANCE_TIMEOUT = 24
MAX_PERMITTED_INSTANCES = 5
MAILING_LIST = "jon.doh@simpsons.duf, drunk@beer.com"

def parse_tag(tags, keyValuePair):
for tag in tags:
if tag['Key'] == keyValuePair[0] and tag['Value'] == keyValuePair[1]:
return True
return False

def runtimeExceeded(instance, timeOutHours):
# Working in to UTC to avoid time-travel during daylight-saving changeover
timeNow = datetime.datetime.utcnow()
instanceRuntime = timeNow - instance.launch_time.replace(tzinfo=None)
print instanceRuntime
if instanceRuntime > datetime.timedelta(hours=timeOutHours):
return True
else:
return False

def sendAlert(instance, message):
msg = MIMEMultipart()
msg['From'] = 'AWS_Notification@sourcevertex.net'
msg['To'] = MAILING_LIST
msg['Subject'] = "AWS Alert: " + message
bodyText = '\n\nThis message was sent by the AWS Monitor ' + \
'Lambda. For details see AwsConsole-Lambdas. \n\nIf you want to ' + \
'exclude an instance from this monitor, tag it ' + \
'with Key=RuntimeMonitor Value=False'

messageBody = MIMEText( message + '\nInstance ID: ' +
str(instance.instance_id) + '\nIn Availability zone: '
+ str(instance.placement['AvailabilityZone']) + bodyText)
msg.attach(messageBody)

ses = boto3.client('ses')
ses.send_raw_email(RawMessage={'Data' : msg.as_string()})

def lambda_handler(event, context):
aws_regions = ec2_client.describe_regions()['Regions']
for region in aws_regions:
runningInstancesCount = 0
try:
ec2 = boto3.client('ec2', region_name=region['RegionName'])
ec2_resource = boto3.resource('ec2',
region_name=region['RegionName'])
aws_region = region['RegionName']

instances = ec2_resource.instances.all()

for i in instances:
if i.state['Name'] == 'running':
runningInstancesCount +=1
if i.tags != None:
if parse_tag(i.tags, ('RuntimeMonitor', 'False')):
# Ignore these instances
pass
else:
if runtimeExceeded(i, INSTANCE_TIMEOUT):
sendAlert(i, "An EC2 instance has been running " + \
"for over {0} hours".format(INSTANCE_TIMEOUT))
else:
print "Untagged instence"
if runtimeExceeded(i, UNKNOWN_INSTANCE_TIMEOUT):
sendAlert(i, "An EC2 instance has been running " + \
"for over {0} hours".format(UNKNOWN_INSTANCE_TIMEOUT))

except Exception as e:
print e
continue

if runningInstancesCount > MAX_PERMITTED_INSTANCES:
sendAlert(i, "Number of running instances exceeded threshold " + \
"{0} running instances".format(runningInstancesCount))

return True

关于amazon-web-services - 在 AWS 中为 EC2 实例创建警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37612602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com