AWS CloudWatch磁盘空间可使用AWSCLI设置警报

时间:2018-07-18 22:47:45

标签: shell amazon-web-services aws-cli amazon-cloudwatch

我已经设置了一个cron作业来将可用磁盘空间发送给cloudwatch指标,但是当我尝试为磁盘空间可用创建cloudwatch警报时,由于我在警报创建中使用的尺寸,我无法使其工作

磁盘空间可用的设置命令

sudo yum install perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https perl-Digest-SHA --enablerepo="rhui-REGION-rhel-server-optional" -y
sudo yum install wget zip unzip -y
mkdir /tmp/cloudwatch
sudo wget -P /tmp/cloudwatch https://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.1.zip
unzip /tmp/cloudwatch/CloudWatchMonitoringScripts-1.2.1.zip
rm -rf /tmp/cloudwatch/CloudWatchMonitoringScripts-1.2.1.zip
cp ~/.aws/credentials /tmp/cloudwatch/aws-scripts-mon/awscreds.conf
sed -i '1d' /tmp/cloudwatch/aws-scripts-mon/awscreds.conf
grep '*/5 * * * * /tmp/cloudwatch/aws-scripts-mon/mon-put-instance-data.pl --disk-path=/ --disk-space-avail --disk-space-used --disk-space-units=gigabytes --from-cron' /etc/crontab || echo '*/5 * * * * /tmp/cloudwatch/aws-scripts-mon/mon-put-instance-data.pl --disk-path=/ --disk-space-avail --disk-space-used --disk-space-units=gigabytes --from-cron' > /etc/crontab

什么是正确的尺寸?

ARN_OF_SNS_TOPIC="arn:aws:sns:us-east-1::cloudwatch"
CPU_USAGE=50
DISKSPACE_AVAILABLE=20
aws cloudwatch put-metric-alarm ${DRYRUN}\
    --alarm-name "${HN}-diskspaceavailable"\
    --alarm-description "Alarm when disk space less that 20GB"\
    --actions-enabled\
    --ok-actions "${ARN_OF_SNS_TOPIC}"\
    --alarm-actions "${ARN_OF_SNS_TOPIC}"\
    --insufficient-data-actions "${ARN_OF_SNS_TOPIC}"\
    --metric-name DiskSpaceAvailable\
    --namespace System/Linux\
    --statistic Average\
    --dimensions  Name=InstanceId,Value=${INSTANCE_ID}, Name=Filesystem\
    --period 300\
    --threshold ${DISKSPACE_AVAILABLE}\
    --comparison-operator LessThanThreshold\
    --evaluation-periods 1\
    --unit Gigabytes

2 个答案:

答案 0 :(得分:1)

我首先看到的是您缺少“文件系统”维度的值。应该是这样的:

--dimensions  Name=InstanceId,Value=${INSTANCE_ID}, Name=Filesystem,Value=SOMETHING\

您可以在CloudWatch Metrics Console中查看维度。将指标添加到图形时,可以将鼠标悬停在“详细信息”列上并查看所有尺寸。

答案 1 :(得分:0)

Filesystem_value=$(df -h / | tail -1 | awk '{print $1}')

aws cloudwatch put-metric-alarm ${DRYRUN}\
    --alarm-name "${HN}-diskspaceavailable"\
    --alarm-description "Alarm when disk space less that 20GB"\
    --actions-enabled\
    --ok-actions "${ARN_OF_SNS_TOPIC}"\
    --alarm-actions "${ARN_OF_SNS_TOPIC}"\
    --insufficient-data-actions "${ARN_OF_SNS_TOPIC}"\
    --metric-name DiskSpaceAvailable\
    --namespace System/Linux\
    --statistic Average\
    --dimensions Name=Filesystem,Value=${Filesystem_value} Name=InstanceId,Value=${INSTANCE_ID} Name=MountPath,Value=/\
    --period 300\
    --threshold ${DISKSPACE_AVAILABLE}\
    --comparison-operator LessThanThreshold\
    --evaluation-periods 1
#    --unit Gigabytes
echo 'cloudwatch disk space available alarm has been created'
echo