Thursday, July 28, 2011

Shell script to monitor Server Space

Below is the script for monitor the disk space information of a server. You can place this script as
" space. sh " and run it as " sh space.sh " .

###### starting of space.sh #####

hostname=`hostname`
date=`date`
df -h > space.txt
threshold=50
#nooflines=`df -h | wc -l`
echo "      DISK SPACE INFORMATION"
echo "=================================="
for i in `grep -n ' ' space.txt|grep -v hi | cut -d':' -f1`
do
lno=`expr $i + 1`
if [ $lno -eq 16 ]; then
#here 16 is no of lines from o/p of "df -h"
rm space.txt
exit;
fi
usep=`cat space.txt |awk '{print $5 " " $6}'|awk '{print $1}'|sed -n $lno'p'|cut -d'%' -f1`
#echo $usep
partition=`cat space.txt |awk '{print $5 " " $6}' |awk '{print $2}'|sed -n $lno'p'`
#echo $partition
pr=`cat space.txt |sed -n $lno'p'`
if [ $usep -ge $threshold ]; then
  # echo "Running out of space \"$partition ($usep%)\" on $hostname as on $date"
  # echo $pr
  echo "Server Name     : `hostname`"
  echo "Date            : `date`"
  cat space.txt |sed -n $lno'p'|awk '{print "Space Status    : file system " $6 " is "$5" full"}'
  cat space.txt |sed -n $lno'p'|awk '{print "Total Size      : " $2}'
  cat space.txt |sed -n $lno'p'|awk '{print "Used Space      : " $3}'
  cat space.txt |sed -n $lno'p'|awk '{print "Free Space      : " $4}'
  echo "Threshold Limit : $threshold%"
  echo " -------------------------------------"
fi
done

##### end of space.sh #######

Sample output:
===============
bash-3.00$ sh space.sh
     
DISK SPACE INFORMATION
==================================
Server Name     : appikonda
Date            : Wed Jun 15 09:40:32 IST 2011
Space Status    : file system /export/home is 63% full
Total Size      : 43G
Used Space      : 27G
Free Space      : 16G
Threshold Limit : 50%
 -------------------------------------
Server Name     : appikonda
Date            : Wed Jun 15 09:40:32 IST 2011
Space Status    : file system /data/export/home is 97% full
Total Size      : 58G
Used Space      : 55G
Free Space      : 2.0G
Threshold Limit : 50%
 -------------------------------------

No comments:

Post a Comment