如何创建闹钟 1)EC2实例运行时间过长时发出警报(说1小时)。 2)当EC2实例的数量达到阈值时(每次说5个实例)发出警报
还有一个假设是,这些EC2实例是特定的。这些警报适用于实例名称以" test"开头的EC2实例。
当我尝试创建闹钟时,我还没有在Metrics中看到这种逻辑。标准指标包括CPU利用率,网络输入,网络输出等
有没有办法通过定义我们的自定义指标或其他一些选项来创建此警报?
答案 0 :(得分:0)
对于自动部署的实例,由于您不知道实例ID,因此无法设置CloudWatch Alarm。 设置警报的唯一方法是创建一个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
答案 1 :(得分:0)
您可以使用自定义指标在CloudWatch中发布事件,然后您可以使用该事件设置警报。