Amazon web services 在AWS中为EC2实例创建报警
如何创建报警时 1) EC2实例运行时间过长(例如1小时)时发出警报。 2) EC2实例数达到阈值时发出警报(例如一次5个实例) 还有一个假设是,这些EC2实例是特定的。假设这些警报适用于实例名称以“test”开头的EC2实例 当我尝试创建警报时,我在Metrics中没有看到这种逻辑。标准指标包括CPU利用率、网络输入、网络输出等Amazon web services 在AWS中为EC2实例创建报警,amazon-web-services,amazon-ec2,alarm,Amazon Web Services,Amazon Ec2,Alarm,如何创建报警时 1) EC2实例运行时间过长(例如1小时)时发出警报。 2) EC2实例数达到阈值时发出警报(例如一次5个实例) 还有一个假设是,这些EC2实例是特定的。假设这些警报适用于实例名称以“test”开头的EC2实例 当我尝试创建警报时,我在Metrics中没有看到这种逻辑。标准指标包括CPU利用率、网络输入、网络输出等 有没有办法通过定义自定义指标或其他选项来创建此警报?对于自动部署的实例,无法设置CloudWatch警报,因为您不知道实例ID。 设置警报的唯一方法是创建AWS La
有没有办法通过定义自定义指标或其他选项来创建此警报?对于自动部署的实例,无法设置CloudWatch警报,因为您不知道实例ID。 设置警报的唯一方法是创建AWS Lambda函数,该函数对所有正在运行的实例进行极点设置,并将其启动时间与指定的超时进行比较 lambda函数由一个函数周期性地触发 用于指定不同机器的不同运行持续时间。例如,您的启动工具应该使用键值“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
您可以使用自定义度量在CloudWatch中发布事件,然后使用该事件设置警报。您浏览过CloudWatch文档吗?它们是允许您发布的APImetrics@Shibashis查看了publich度量。但我不确定度量的逻辑定义在哪里。例如,我只定义了度量名称、statiscal输出和单位。但是我想要的是,假设度量名称是EC2InstanceHealthDuration(表示EC2实例在启动之前运行的时间)。应该有一些关于度量名称所执行的逻辑的unix脚本。请让我知道在哪里可以找到相同的。cloudwatch只收集统计数据,并允许您在这些数据上创建警报,您需要创建脚本来监视实例的运行时间,然后将该指标推送到cloudwatch。如果您不想开发这样的逻辑,当EC2实例运行过长(比如说1小时)时,您可能需要考虑其他软件,如DATADOG、New Eric、NAGIOS ETCYORY @ Shibashis,以满足上述要求1)。2) 当EC2实例数达到阈值(例如一次5个实例)无法使用标准度量时发出警报。如果我们编写一个自定义度量,那么我们必须编写脚本的逻辑。我看到在put metric data()中,没有放置脚本的选项。如果你对此有所了解,那就太好了。要推动度量,你有很多选择,我建议以下三个选项之一:1>将脚本放在实例本身上2>创建另一个ec2,脚本将在其中运行3>使用AWS lambda计划事件推动度量。