简体   繁体   English

为多个实例设置CloudWatch Alarm

[英]Setting CloudWatch Alarm for multiple instances

I'm configuring an alarm for an instance from the Amazon CLI. 我正在从Amazon CLI配置实例的警报。 for example, to trigger a notification when the cpu is idle for 5min. 例如,当CPU闲置5分钟时触发通知。 but I want to set this alarm for a lot of Instances. 但我想为很多实例设置此警报。

With this Bash Script I created one alarm for one instance : 使用此Bash脚本,我为一个实例创建了一个警报:

aws cloudwatch put-metric-alarm --alarm-name cpu-mon --alarm-description "Alarm when CPU exceeds 70 percent" --metric-name CPUUtilization --namespace AWS/EC2 --statistic Average --period 300 --threshold 70 --comparison-operator GreaterThanThreshold  --dimensions "Name=InstanceId,Value=i-12345678" --evaluation-periods 2 --alarm-actions arn:aws:sns:us-east-1:111122223333:MyTopic --unit Percent

So, I don't see how can I use this script to choose another instances, or eventually loop on that script, in order to choose another instances. 因此,我看不到如何使用此脚本来选择另一个实例,或者最终在该脚本上循环以选择另一个实例。

If you have a list of instance IDs you want to create alarms for you could do something like: 如果您有实例ID的列表,则要为其创建警报,可以执行以下操作:

#!/bin/bash
instances=(instanceId1 instanceId2 etc)
for i in "${instances[@]}"; do
    aws cloudwatch put-metric-alarm \
        --alarm-name cpu-mon-${i} \
        --alarm-description "Alarm when CPU exceeds 70 percent" \
        --metric-name CPUUtilization \
        --namespace AWS/EC2 \
        --statistic Average \
        --period 300 \
        --threshold 70 \
        --comparison-operator GreaterThanThreshold  \
        --dimensions "Name=InstanceId,Value=${i}" \
        --evaluation-periods 2 \
        --alarm-actions arn:aws:sns:us-east-1:111122223333:MyTopic \
        --unit Percent
done

You could also initially use the AWS CLI to grab instance IDs based on tags, instance names etc and then use those to create the alarms along the same lines. 您还可以最初使用AWS CLI根据标签,实例名称等获取实例ID,然后使用它们沿相同的行创建警报。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM