Skip to content

Instantly share code, notes, and snippets.

@almir
Last active May 23, 2016 16:31
Show Gist options
  • Save almir/75264c93785b403f8831 to your computer and use it in GitHub Desktop.
Save almir/75264c93785b403f8831 to your computer and use it in GitHub Desktop.
Shell script to monitor or watch the disk space
#!/bin/sh
#
# Shell script to monitor or watch the disk space
# It will send an email to $ADMINEMAIL, if the (free avilable) percentage
# of space is >= 90%
#
# Linux shell script to watch disk space (should work on other UNIX OSes)
# SEE URL: http://www.cyberciti.biz/tips/shell-script-to-watch-the-disk-space.html
#
# set admin email so that you can get email
ADMINEMAIL="username@emailserver.com"
# set from email address (optional)
FROMEMAIL=""
# set alert level 90% is default
ALERT=90
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
usep=$(echo "${output}" | awk '{ print $1}' | cut -d'%' -f1)
partition=$(echo "${output}" | awk '{ print $2 }')
if [ "${usep}" -ge ${ALERT} ]; then
if [ -z ${FROMEMAIL} ]; then
echo "Running out of space \"$partition (${usep}%)\" on $(hostname) as on $(date)" |
mail -s "Alert: Almost out of disk space - ${usep}%" ${ADMINEMAIL}
else
echo "Running out of space \"$partition (${usep}%)\" on $(hostname) as on $(date)" |
mail -r ${FROMEMAIL} -s "Alert: Almost out of disk space - ${usep}%" ${ADMINEMAIL}
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment