Skip to content

Instantly share code, notes, and snippets.

@deviationist
Last active November 26, 2023 03:41
Show Gist options
  • Save deviationist/19fcd102c537491a0278712918806f3d to your computer and use it in GitHub Desktop.
Save deviationist/19fcd102c537491a0278712918806f3d to your computer and use it in GitHub Desktop.
Disk usage monitor for a single disk that posts to Slack if the usage percent surpasses a configurable threshold.
#!/bin/bash
PERCENT_THRESHOLD=95
DISK_NAME=/dev/sda1
DISK_PERCENTAGE=$(df -hl $DISK_NAME | sed 1d | awk 'BEGIN{print "Use%"} {percent+=$5;} END{print percent}' | column -t | sed 1d);
if (( $PERCENT_THRESHOLD <= $DISK_PERCENTAGE )); then
curl -X POST -H 'Content-type: application/json' --data "{\"text\":\"Disk is almost full - $DISK_PERCENTAGE% used on $DISK_NAME. Threshold set to $PERCENT_THRESHOLD%.\"}" https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXXX
fi
@deviationist
Copy link
Author

deviationist commented Feb 23, 2023

  1. Set the disk usage percent threshold (line 2)
  2. Set disk path (run command "df" to find the correct disk path) (line 3)
  3. Insert your own Slack cURL call / update the Slack URL (line 6)
  4. Add the path of this script to your crontab at desired interval. This example will run every 3rd hour: "0 */3 * * * /path/to/your/disk-monitor.sh". Use crontab.guru to generate an interval that suits you.
  5. Et voilà!

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