Skip to main content

lowspace.sh

This script is called from a cron job (scheduled task) and emails me on a daily basis this data so I can track disk space usage. I install it on all my systems (though I should probably do something different, like store the values in a database or something).

Of course, this code may or may not work as well for you. And please see my comments at the end.

#! /bin/bash
#
# lowspace.sh
#
# Script is used to identify disk sizes and email the results to the
# people/email list(s).  The list of users could be converted to a text file
# as some point.
#
#Local Varaibles
#
FILE=/tmp/diskspace$date
HOSTSYSTEM=`hostname | tr a-z A-Z`
MAILFROM="$HOSTSYSTEM@kb0odu.us"
MAILTO="tim@example.com"
TODAY=`date +'%b %d, %Y'`
#
#Program substitutions.
#
AWK=/usr/bin/awk
DF=/bin/df
GREP=/usr/bin/grep
HEAD=/usr/bin/head
MAIL=/usr/bin/mail
RM=/bin/rm
SORT=/usr/bin/sort

$DF -h | $HEAD -1 | $AWK '{printf "%-25s %5s %7s MB   %-25s\n", $1, $5, $4, $6}' > $FILE
echo "-------------------------  ----   --------  ----------------------------------" >> $FILE
$DF -m | $GREP '^\/dev' | $AWK '{printf "%-25s %5.0f %10.0f   %-25s \n", $1, $5, $4, $6}' | $SORT -n +1 -r >> $FILE

#28Feb07 TJB - Removed tim from destinations as I don't want to send locally
#mail -s "disk report `date`" -r "$HOSTSYSTEM@kb0odu.us" "tim@example.com" < $file

$MAIL -s "LINUX - Disk Report $HOSTSYSTEM $TODAY" -r $MAILFROM $MAILTO < $FILE
$RM $FILE

Just sample code from my script.

You will also note that I use substitutions in my code. After being hacked once several years ago, I decided to hard-code the specific (and correct) paths in my shell scripts to ensure that an automated job (like this one can be) would not execute the wrong copy of the script. Of course, that doesn't solve the problem of the correct binaries being replaced. (See my AIDE presentation for some help with that.)