Disk Space Monitoring Script And Get Email Alert When CRITICAL

Most of the environments (small environments) they are not effort to buy a monitoring tools in this case, scripting will come handy to monitor your environment. I hope this disk space monitoring script will help to monitor the disk space. Disk Space Monitoring Script And Get Email Alert when CRITICAL, When Disk space is reached more than threshold value.

Disk Space Monitoring Script

#!/bin/bash

# AUTHOR: Ravi Kumar (http://www.arkit.co.in/)
# DATE: 19th December 2015
# Shell script to monitor or watch the low-disk space
# It will send an email to $ADMIN, if the (free available) percentage
# of space is >= 90%
# Disk Space Monitoring Script Get Email Alert
#### Script START here ###
## You can change your threshold value whatever you want ##
THRESHOLD=90
PATHS=/
AWK=/bin/awk
DU=`/usr/bin/du -ks`
GREP=/bin/grep
SED=/bin/sed
CAT=/bin/cat
MAILFILE=/tmp/mailviews$$
MAILER=/bin/mail
## Change ADMIN Mail address as per the requirement ##
mailto=aravikumar48@gmail.com

for path in $PATHS
do
## Validate the Percentage of Disk space ##
DISK_AVAIL=`/bin/df -k / | grep -v Filesystem |awk '{print $5}' |sed 's/%//g'`
if [ $DISK_AVAIL -ge $THRESHOLD ]
then
echo "Please clean up your stuff \n\n" > $MAILFILE
$CAT $MAILFILE | $MAILER -s "Clean up stuff" $mailto
fi
done
## END of the Script ##

Explanation of script: Above script is written using one FOR loop and one IF condition.

/bin/df -k / | grep -v Filesystem |awk ‘{print $5}’ |sed ‘s/%//g’     <<– This line will calculate the disk space in percentage number

if [ $DISK_AVAIL -ge $THRESHOLD ]   <<– This IF condition will validate the space, which is provided by the above command then number greater then or equal to 90 it will trig an alert using mail command to your given ADMIN address.

NOTE: Before you schedule this script please change ADMIN Email address and Threshold value.

Please provide your valuable comments below……

Related Articles

Real Time CPU Utilization Alert Script

Shell Script to Check CPU Utilization and Store Historical Data

Thanks for your wonderful Support and Encouragement

Ankam Ravi Kumar

Working as Linux / Storage Administrator L3. Interested in sharing the knowledge.

16 Responses

  1. Prashant says:

    Keep it up Ravi ………Thanks for sharing!!!!!!!!!!

  2. Ankam Ravi Kumar says:

    you can Add `hostname` in the mailer subject so that you can know from which host you received an alert.

  3. It would hv been better if along with hostname you could get the Filesystem name who’s usage is above your set threshold.

    • File System Usage is based on how much data is copied in partition. It may be used by multiple users in that way script output will be a cluttered.
      find / -type f -size +100000k -exec ls -lh {} \; | awk ‘{ print $9 “: ” $5 }’ #Find the Large Size Files more than 100MB in size

  4. blank revanth gandhi says:

    Thankyou Sir 🙂

  5. blank Dharak Choksi says:

    I want to Setup MySQL Replication in Linux CentOS 7? How should i write Script for MySQL Replication Master – Salve in 2 Different Servers?

  6. blank Manoj says:

    THRESHOLD=90
    PATHS=/
    AWK=/bin/awk
    DU=`/usr/bin/du -ks`
    GREP=/bin/grep
    SED=/bin/sed
    CAT=/bin/cat
    MAILFILE=/tmp/mailviews$$
    MAILER=/bin/mail
    ## Change ADMIN Mail address as per the requirement ##
    mailto=info@natsav.com

    for path in $PATHS
    do
    ## Validate the Percentage of Disk space ##
    DISK_AVAIL=`/bin/df -k / | grep -v Filesystem |awk ‘{print $5}’ |sed ‘s/%//g’`
    if [ $DISK_AVAIL -ge $THRESHOLD ]
    then
    echo “Please clean up your stuff \n\n” > $MAILFILE
    $CAT $MAILFILE | $MAILER -s “Clean up stuff” $mailto
    fi
    done

    but when i execute the script facing below error in CentOS 6.8 and it’s working on CentOS 7.2

    disk-monitor.sh: line 26: [: /: integer expression expected.

  7. blank Suraj says:

    ./diskspacemonitor.sh: line 28: /: is a directory
    why am getting error in the script
    i have copied as you mentioned

  8. blank Thanishkaa says:

    Hi Ravi
    If diskspace is > 80 % reached need to clear the logs automatically and place it in a path we create . Can you please help me how to write this script.

    Thanks & Regards
    Thanishkaa K

  9. blank raju says:

    how the mail will be sent ..? Do we need to install any tool for that. If yes, please help with the steps which tool and steps to install and configure for that.
    Thanks inadvance

  10. blank Raviteja says:

    Hi Ravi,
    I’m trying to monitoring the filesize using shell script.I’ve written the code but i’m facing some issue.

    #!/bin/sh

    value = 70
    df -H | grep -vE ‘^Filesystem|tmpfs|cdrom’ | awk ‘{ print $5 ” ” $1 }’ | while read output;
    do
    echo $output
    usep=$(echo $output | awk ‘{ print $1}’ | cut -d’%’ -f1 )
    partition=$(echo $output | awk ‘{ print $2 }’ )
    if [[ “$usep” -ge “$value” ]]
    then
    echo “The partition \”$partition\” on $(hostname) has used $usep% at $(date)” | mail -s “Disk space alert: $usep% used” xxxx@gmail.com
    else
    echo “Disk Usage is normal”
    fi
    done

    =================
    1.I’ve only 2 file systems(one is 88% & another one is 48%),If i execute my script i should get only one mail for the disk size of 88% (because it is more than 70 as per my code)but i’m getting for 48% also.Could you check & help me on this

    • blank ARK says:

      Can you post me sh -x shellscript output

      • blank polin says:

        hello ARK i’m polin and work Application support engineer
        how the mail will be sent ..? Do we need to install any tool for that. If yes, please help with the steps which tool and steps to install and configure for that.
        and i need to monitor and alert email to desk is low , monitering my server
        i well be create a script to this case
        my laptop is :Thinkpad
        os:ubuntu
        Thanks inadvance

  11. blank polin says:

    hello ARK i’m polin and work Application support engineer
    how the mail will be sent ..? Do we need to install any tool for that. If yes, please help with the steps which tool and steps to install and configure for that.
    and i need to monitor and alert email to desk is low , monitering my server
    i well be create a script to this case
    my laptop is :Thinkpad
    os:ubuntu
    Thanks inadvance

Leave a Reply

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