Skip to content

Instantly share code, notes, and snippets.

@acip
Last active August 22, 2022 01:42
Show Gist options
  • Save acip/bd41a702deb4e7d0428995d386ba1f3a to your computer and use it in GitHub Desktop.
Save acip/bd41a702deb4e7d0428995d386ba1f3a to your computer and use it in GitHub Desktop.
Get disk space on Linux and send notification to Slack channel via webhook if above threshold.
#!/bin/bash
# get disk space available and report to Slack if below threshold
SLACK_WEBHOOK_URL=https://hooks.slack.com/services/TXXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
DISK_THRESHOLD=60
DISK_USAGE=$(df -h / | awk '{ print $5 }' | tail -n 1 | sed 's/%//g')
if [ $DISK_USAGE -ge $DISK_THRESHOLD ]; then
echo "Disk usage is $DISK_USAGE% - sending notification to slack"
curl -X POST -H 'Content-type: application/json' --data '{"text":":exclamation: Disk space on Database Server ('$HOSTNAME') is high: *'$DISK_USAGE'%*"}' $SLACK_WEBHOOK_URL
else
echo "Disk usage is $DISK_USAGE% - no notification sent"
fi
exit 0
@acip
Copy link
Author

acip commented Aug 22, 2022

Use cron (crontab -e) to run every minute: */1 * * * * /path/to/monitor-space.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment