Skip to content

Instantly share code, notes, and snippets.

@adionditsak
Last active December 31, 2015 14:39
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 adionditsak/8001329 to your computer and use it in GitHub Desktop.
Save adionditsak/8001329 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# vars #
disk="/dev/disk0s2" # disk to monitor
current_usage=$(df -h | grep ${disk} | awk {'print $5'}) # get disk usage from monitored disk
max_usage="20%" # max 90% disk usage
mail="aarvik92@gmail.com" # mail to sent alert to
# functions #
function max_exceeded() {
# tell that mail is being sent
echo "Max usage (${max_usage}) exceeded. Your disk usage is it at ${current_usage}. Trying to send mail-alert..."
# check if the mail program exist
type mail > /dev/null 2>&1 || {
echo >&2 "Mail does not exist. Install it and run script again. Aborting script..."; exit;
}
# if the mail program exist we continue to this and send the alert
mailbody="Max usage (${max_usage}) exceeded. Your disk (${disk}) usage is at ${current_usage}."
echo ${mailbody} | mail -s "Disk alert!" "${mail}"
echo "Mail was sent to ${mail}"
}
function no_problems() {
echo "No problems. Disk (${disk}) usage at ${current_usage}. Max is set to ${max_usage}."
}
function main() {
# check if a valid disk is chosen
if [ ${current_usage} ]; then
# check if current disk usage is greater than or equal to max usage.
if [ ${current_usage%?} -ge ${max_usage%?} ]; then
# if it is greater than or equal to max usage we call our max_exceeded function and send mail
max_exceeded
else
# if it is ok we do nothing
no_problems
fi
else
# if the disk is not valid, print valid disks on system
echo "Set a valid disk, and run script again. Your disks:"
df -h
fi
}
# init #
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment