Linux shell script to monitor disk space

Problem:  Hard drives can fill up, and when they do, no good can come of it.  Wouldn’t you like to be notified before impending disaster so you can correct the situation?

Solution:  Here is a simple script that I put in /etc/cron.hourly (would probably be better in cron.daily, but I’m overly paranoid)

#!/bin/sh
# set -x
# Shell script to monitor or watch the disk space
# It will send an email to $ADMIN, if the (free available) percentage of space is >= 90%.
# ————————————————————————-
# Set admin email so that you can get email.
ADMIN=”[email protected]
# set alert level 90% is default
ALERT=80
# Exclude list of unwanted monitoring, if several partions then use “|” to separate the partitions.
# An example: EXCLUDE_LIST=”/dev/hdd1|/dev/hdc5″
EXCLUDE_LIST=”/boot”
#
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#
function main_prog() {
while read output;
do
#echo $output
usep=$(echo $output | awk ‘{ print $1}’ | cut -d’%’ -f1)
partition=$(echo $output | awk ‘{print $2}’)
if [ $usep -ge $ALERT ] ; then
echo “Running out of space \”$partition ($usep%)\” on server $(hostname), $(date)” | \
mail -s “Alert: Almost out of disk space $usep%” $ADMIN
fi
done
}

if [ “$EXCLUDE_LIST” != “” ] ; then
df -H | grep -vE “^Filesystem|tmpfs|cdrom|${EXCLUDE_LIST}” | awk ‘{print $5 ” ” $6}’ | main_prog
else
df -H | grep -vE “^Filesystem|tmpfs|cdrom” | awk ‘{print $5 ” ” $6}’ | main_prog
fi

Download:  diskalert.sh

Credit:  http://www.cyberciti.biz/tips/shell-script-to-watch-the-disk-space.html

2 thoughts on “Linux shell script to monitor disk space

  1. Pamala

    Hi! Quick question that’s entirely off topic. Do you know
    how to make your site mobile friendly? My website looks weird when browsing from my iphone4.
    I’m trying to find a template or plugin that might be able to correct this issue.

    If you have any recommendations, please share. Many thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *