Disk Space Monitoring Script

Posted: July 5, 2010 in Linux-Scripts
Tags: , ,

This will monitor the Disk space of a Linux system and send an email to ADMIN . Admin , and Thresold is a variable , so that it can be change accordingly.

===================================================

#!/bin/bash
#       This script determines the percentage of disk usage.
#       If that percentage is greater than 90% a user is emailed
#       a report notifying them of the usage.
#

emailUser=”it.support@au.specsavers.com,brandont@au.specsavers.com”
typeset -i error=”70″
if [ -e temp.txt ]; then
rm temp.txt
fi
for disc in `mount| egrep ‘^/dev’ | egrep -iv ‘cdrom|proc|sys|pts’ |awk ‘{print $3}’`
do
typeset -i discUsage=`df -h $disc|cut -c40-42|grep -i [^a-z]`
if [ “$discUsage” -ge “$error” ]; then
echo “Disc usage for $disc is at $discUsage. Need your attention !!%” >> temp.txt
fi
done
if [ -e temp.txt ]; then
message=`cat temp.txt`
fi
if [ ${#message} -gt 0 ]; then
cat temp.txt | mail  -s “Disc Usage exceed 70% for : $HOSTNAME” $emailUser
fi

===================================================

Comments
  1. Mohamamd Alam says:

    This is modified to have option to exclude Pertion to Monitor.

    #!/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=”compiere@au.specsavers.com,it.support@au.specsavers.com”
    # set alert level 90% is default
    ALERT=90

    # Exclude list of unwanted monitoring, if several partions then use “|” to separate the partitions.
    # An example: EXCLUDE_LIST=”/dev/hdd1|/dev/hdc5″

    EXCLUDE_LIST=”/mnt/minosShared|/archive”

    #
    #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    #
    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: Out of disk space $usep% on $(hostname)” $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

Leave a reply to Mohamamd Alam Cancel reply