Skip to content

Instantly share code, notes, and snippets.

@a-maumau
Last active January 18, 2021 07:05
Show Gist options
  • Save a-maumau/698b988bcf10aca2d1db452a62ab4fa2 to your computer and use it in GitHub Desktop.
Save a-maumau/698b988bcf10aca2d1db452a62ab4fa2 to your computer and use it in GitHub Desktop.
disk space notification script. tested in debian 10
#ref: https://pc.casey.jp/archives/153903095
##!/bin/sh
#host_name="<your host>"
#message_tag="[WARNING]"
#disk_usage=`/bin/df / | /usr/bin/tail -1 | /bin/sed 's/^.* \([0-9]*\)%.*$/\1/'`
#slack_msg_head="${message_tag} ${host_name}"
#slack_msg_body="\`\`\`Disk usage ${disk_usage}%\`\`\`"
#slack_msg="\`${slack_msg_head}\`\n${slack_msg_body}"
#if [ $disk_usage -gt 90 ]; then
# # mail notification
# echo "Disk usage alert: $disk_usage %" | /usr/bin/mail -s "[WARNING][`hostname`] disk space alert" <target e-mail address>
#
# # slack notification
# curl -X POST -H 'Content-type: application/json' --data '{"text": "'"${slack_msg}"'"}' <slack webhook url>
#fi
# new one, this allows to specify which disk to watch
#!/bin/sh
HOST_NAME="your host, or maybe you can fetch from your local"
TARGET_PARTITION=/dev/sda
NOTE_THRESHOLD=95
SLACK_WEBHOOK_API_URL='https://hooks.slack.com/services/****'
# get used persent
USAGE_VAL=`df -h | grep $TARGET_PARTITION | awk '{ print $5 }' | sed -e '$s/.$//'`
if [ $USAGE_VAL -gt $NOTE_THRESHOLD ]
then
#echo "Disk:$TARGET_PARTITION usage: $USAGE_VAL %" | /bin/mail -s "[ALERT][hostname] disk space alert" YOUR_EMAIL_ADDRESS
JSON="{ \"text\": \"\`[ALERT][$HOST_NAME]\`\n\`\`\`Disk:$TARGET_PARTITION usage: $USAGE_VAL%\`\`\`\" }"
RET=`curl -s -X POST -H 'Content-Type: application/json' -d "$JSON" $SLACK_WEBHOOK_API_URL`
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment