Views

Friday, October 28, 2016

Shell - Script to Monitor Disk Usage

Linux Shell script to monitor disk usage.
==========================
On different system the script might need slight modification of positional parameters.

#!/bin/sh
# Shell script to monitor disk Usage
# Script name: diskmon.sh
# Usage: ./diskmon.sh
export logdir=/home/oracle/logs
export TIMESTAMP=`date +%T-%m-%d-%Y`
export logfile=$logdir/${TIMESTAMP}.log
{
   echo "HOSTNAME - $(hostname)"
   echo `date`
}>>$logfile

df -H | grep -vE '^Filesystem|tmpfs|cdrom|mapper|sda|media' | awk '{ print $3 " " $4 " " $5 " " $6 }' | while read result;
do
if [ $(echo $result | awk '{ print $3}') = '/' ]; then
{
   perc=$(echo $result | awk '{ print $2}' | cut -d'%' -f1  )
   mountp=$(echo $result | awk '{ print $3 }' )
}
else
{
   perc=$(echo $result | awk '{ print $3}' | cut -d'%' -f1  )
   mountp=$(echo $result | awk '{ print $4 }' )
}
fi
if [ $perc -ge 40 ]; then
{
   echo "Threshold limit reached  \"$mountp ($perc%)\" on $(hostname)"
}>>$logfile
fi
done
{
   echo "#######################################"
   echo "CURRENT MOUNT POINT INFORMATION"
   echo "#######################################"
   df -h
}>>$logfile

lines=`cat $logfile | wc -l | awk '{print $1}'`
if [[ $lines > 2 ]]; then
     /bin/mailx -s "Alert: Almost out of disk space on `hostname` " "abc@xmail.com" < $logfile
else
     rm $logfile
fi

Threshold value is set to 40 in above script to capture the output.

We will get result something like this.

[oracle@testing logs]$ cat 14\:15\:40-10-28-2016.log
HOSTNAME - testing.localdomain
Fri Oct 28 14:15:40 EDT 2016
Threshold limit reached  "/ (79%)" on testing.localdomain
Threshold limit reached  "/u02 (44%)" on testing.localdomain
#######################################
CURRENT MOUNT POINT INFORMATION
#######################################
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                       11G  8.0G  2.2G  79% /
/dev/sda1              99M   13M   82M  13% /boot
tmpfs                 2.0G  636M  1.4G  33% /dev/shm
/dev/sdb1              30G   13G   16G  44% /u02
/dev/sdc1              30G  173M   28G   1% /u03
IBM_DB2               459G  253G  207G  56% /media/sf_IBM_DB2
Oracle_Setup          459G  253G  207G  56% /media/sf_Oracle_Setup
/dev/hda               56M   56M     0 100% /media/VBOXADDITIONS_5.1.0_108711

No comments:

Post a Comment

Leave a Reply...