Skip to content

Instantly share code, notes, and snippets.

@bretonics
Created February 22, 2019 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bretonics/6feb68eeba7a47ce3d5a01e4a2953213 to your computer and use it in GitHub Desktop.
Save bretonics/6feb68eeba7a47ce3d5a01e4a2953213 to your computer and use it in GitHub Desktop.
Available disk usage alert setup to run weekly on crontab
#!/bin/bash
# /etc/cron.weekly/
LIMIT="85"
MAILTO=""
SUBJECT="Disk Usage Alert!"
BODY="WARNING! $(hostname) disk space is currently at ${USED}% capacity."
# Get usage percentage of root disk space
USED=$(df -h | grep "/dev/mapper" | awk '{ print $5 }' | cut -d'%' -f1)
# If usage is greater than limit
if [ $USED -gt $LIMIT ]; then
# Start file reaper in the backgroud
bash ~/crontab/retention_reaper > ~/crontab/reaper.log 2>&1 &
PSID=$!
# Send email alert
BODY="WARNING! $(hostname) disk space is currently at ${USED}% capacity.
The following directories (older than 15 days) will be removed:
$(find /home/sftpuser/*/ftp/* -type d -mtime +15)
Process (${PSID}) will remove the directories above in 24 hours unless killed [kill ${PSID}].
"
echo -e "$BODY" | mail -s "$SUBJECT" "$MAILTO"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment