I Can Haz Admin Scriptzz?

February 15th, 2008

As promised, here is the first admin script. It's not fancy, but it solves the disk space warning problem.

#!/bin/sh

MAILTO=you@yourdomain.com

df -h | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
  consumed=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $output | awk '{ print $2 }' )
  if [ $consumed -ge 95 ]; then
    echo "Running out of disk space on \"$partition [$consumed% consumed]\" on $(hostname)" |
        mail -s "[ALERT]: $(hostname) is almost out of disk space" $MAILTO
  fi
done
This will take a look at your disk usage and send you an email warning if you hit or cross your threshold. Obviously tailor to your liking and system specific implementations. Once you are satisfied, simply chmod +x the file, and add it to a cron task so that it runs as much as you need it to. The following example will run this script every 30 minutes.

*/30 * * * * cd /path/to/script && sh script
This script was created and tested on OpenBSD 4.1. I have also put this script up on the Relevance Open Source Repository. You can check it and eventually others at https://opensource.thinkrelevance.com/svn/admin_scripts

Sorry, comments are closed for this article.