11 Jan

Simple Shell Script to Monitoring Disk Space on a Linux Machine

This article explain a way to get a mail as soon as the disk usage reaches to its critical level to avoid issues later. To set a simple monitor on Linux / Unix, I have two simple scripts:

  • DSAlert.sh : Shell script for retrieving disk space percentages and put in to a cron job.
  • DiskSpace_Alert.pl : Perl script for sending an email.

Following is the disk monitor shell script which will execute the perl file for sending alert email.

#DSAlert.sh
#!/bin/sh
#Retrive disk space info
df=`df -Pl  | grep "^/dev" | awk '{print $5, $6}' | sed "s/%//"`

#Reference: df
echo “$df” | while read percent fs
do
#If Disk Usage increases above 90% send an email.
if [ $percent -ge 90 ] ; then
#Calling a perl file with parameters for sending an email.
`/opt/lampp/bin/perl DiskSpace_Alert.pl $fs is $percent percent full`
fi
done

Perl Script for sending an email:

# DiskSpace_Alert.pl
#!perl
use MIME::Lite;

#Reference : Mime-Lite

# set up email
$to = “my-mail-id\@domain.com”;
$from = “Diskmonitor\@ServerName.com”;
$subject = “Disk_Alert”;
$message = “Disk Space issue.\nActions Required:\n”.”@ARGV”;
# send email
email($to, $from, $subject, $message, $file);

# email function
sub email
{
# get incoming parameters
local ($to, $from, $subject, $message, $file) = @_;
# create a new message
$msg = MIME::Lite->new(
From => $from,
To => $to,
Subject => $subject,
Data => $message);
# send the email

MIME::Lite->send(‘smtp’, ‘localhost’, Timeout => 60);
$msg->send();
}
Setting up a crontab for monitoring:

Crontab –e
* * * * * sh  full/path/to/DSAlert.sh

And we’re done with the Linux/Unix Disk Monitoring alerts.

Instructions for setting up timing for crontab:

+----------------> minute (0 - 59)
|  +-------------> hour (0 - 23)
|  |  +----------> day of month (1 - 31)
|  |  |  +-------> month (1 - 12)
|  |  |  |  +----> day of week (0 - 6) (Sunday=0 or 7)
|  |  |  |  |
*  *  *  *  *

3 thoughts on “Simple Shell Script to Monitoring Disk Space on a Linux Machine

  1. I am using Oracle Apps 11i
    My OS is RHEL 4.6.

    I need to get a email alerts,

    when there is a critical issues like unauthorized access or services being stopped..

    How i can achieve this ..

  2. Hi Vishwa,
    Idea in myscript was “grep”ing disk details from df command and using it in script.
    I’m not into Oracle yet (‘d be interested though) but you may for sure grep the oracle daemon or service information in the script.

    About services being stopped:

    For mysql if you want to check if it’s running or not we have:
    /etc/init.d/mysqld status
    Which will report the service status. I think Oracle must be having something like this.

    Meanwhile I Googled and found that normally people search for ORACLE_SID or pmon:
    ps -ef|grep $ORACLE_SID|grep -v grep|grep -v ora_|wc –l

    You may also grep for “pmon”, if that is not running you can say Oracle is down.

    Check below script:
    #!/bin/sh

    ret=`ps -fu oracle | grep ora_smon_ | grep -v grep | wc -l`
    echo $ret
    if [ $ret -eq 0 ] ; then
    echo “database down”
    # Run script here
    else
    echo “database up”
    fi

    I assume you should find this helpful: orafaq.com/wiki/Scripts.

    About unauthorized access:
    I’m not sure how will you detect that or what you meant by that!? If X enters as root user, how will you define that X is unauthorised? It’s better to stop unauthorized access by securing your OS settings than detect it later.

  3. This works for me on RHEL 5.4
    #! /bin/ksh
    #
    ########################################################################################
    # This scripts monitors the disk space utilization and sends an email to the Admins if #
    # the amount of disk space for any file system reaches 90% or more. This script will #
    # run as an hourly cron job. #
    ########################################################################################
    df -h | grep -iv filesystem | while read LINE; do PERC=`echo $LINE | awk ‘{print $5}’ | sed -e ‘s/%//’`
    if [ $PERC -gt 90 ]; then
    echo “The Filesystem ${LINE} on `hostname`” |mail -s “Disk Space Alert” george.moran@aderas.com
    fi
    done

Leave a Reply

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

*

-- Kedar Vaijanapurkar --