Amazon web services 状态检查的CloudWatch警报失败isn';没有被创造
这个代码示例类似于我在CloudWatch中创建CPU、磁盘和内存警报时使用的代码。我想为CloudWatch StatusCheckFailed的所有实例创建警报。有人能告诉我为什么没有创建StatusCheckFailed报警吗Amazon web services 状态检查的CloudWatch警报失败isn';没有被创造,amazon-web-services,boto3,amazon-cloudwatch,Amazon Web Services,Boto3,Amazon Cloudwatch,这个代码示例类似于我在CloudWatch中创建CPU、磁盘和内存警报时使用的代码。我想为CloudWatch StatusCheckFailed的所有实例创建警报。有人能告诉我为什么没有创建StatusCheckFailed报警吗 import boto3, sys # Create EC2 resourcestr(sys.argv) ec2 = boto3.resource('ec2') # Getting all running instances #instances = ec2.i
import boto3, sys
# Create EC2 resourcestr(sys.argv)
ec2 = boto3.resource('ec2')
# Getting all running instances
#instances = ec2.instances.all()
# Create CloudWatch client
cw = boto3.client('cloudwatch')
# Initialize i_name and i_div variables with instance Name and Costcenter tags
for i in instances:
i_name = "unnamed"
for tag in i.tags:
if tag['Key'] == "Name":
i_name = tag['Value']
if tag["Key"] == 'Costcenter':
i_div = tag['Value']
# List all metrics with namespace as CWAgent, metric-name as Memory % Committed Bytes In Use and partial dimension as instanceid
listOfMetrics = cw.list_metrics(
Namespace = '"AWS/EC2"',
MetricName = 'StatusCheckFailed',
Dimensions = [
{
'Name': 'InstanceId',
'Value': i.id
}
]
)
# Check if StatusCheckFailed metrics exists
if listOfMetrics['Metrics'] != []:
# Check the state of the instance to see if it is running
if i.state["Name"] == "running" :
# Create the alarm
for metric in listOfMetrics['Metrics']:
putAlarm = cw.put_metric_alarm (
AlarmName = i_div + "-" + i_name + "-(" + i.id + ")-" + "StatusCheckFailed",
AlarmDescription = 'Status check for %s %s' % (i.id, i_name),
ComparisonOperator = "GreaterThanOrEqualToThreshold",
ActionsEnabled = True,
AlarmActions = [alarm],
Statistic = "Maximum",
EvaluationPeriods = 2,
Threshold = 1.0,
Period = 60,
Dimensions = [{
'Name': 'InstanceId',
'Value': i.id
}
]
)
名称空间='AWS/EC2'中有双引号,名称空间='AWS/EC2''在使代码统一时,我将所有字符串设置为单引号,替换双引号。对于名称空间字符串,我忽略了删除双引号。此脚本正在按预期运行。在
AlarmActions=[alarm]
中什么是alarm
?“报警”变量表示SNS主题ARN。我删除了这段代码,因为在if,elsif,elsif语句中嵌入了ARN。我有一个if-elif语句,它根据Costcenter标记或I_div变量值将SNS主题分配给报警变量。