#!/bin/ksh
#########################################################
#
#	MODULE:	dsk_space.sh
#
#	VERSION:	
#
#	LONG NAME: Disk Space Checker	
#
#	SUBSYSTEM:	
#
#	AUTHOR:	Vinod Das (vrd54@hotmail.com)
#	SUBSTITUTE:	
#
#	PURPOSE: Check the disk space of all mount 
#		points in current machine sends a mail
#		to the specified people, in case the
#		the space in use in any of the mount
#		points is above 90%
#
#	USAGE:  dsk.sh
#               ----No arguments---
#
#	DEPENDENCIES:  Put the list of email ids in a  
#		file named dsk_ids.txt;The entry should
#		be like To: vrd54@hotmail.com
#		Set the variable lpat to dir of script
#
#	CHANGE HISTORY:
#		
#		1) Added Header 20/12/01    -Vinod
#		2) Script cane be run fron SunOS,Linux
#		  or HP-UX machine 16/3/02  -Vinod
#		3) Added the feature to mailids from file
#			19/03/02            -Vinod
#
##########################################################

init()
{
  ###############################
  #
  # Variables used in the program 
  # Modify the variables mailp and lpat accordingly
  #

  day=`date |tr -s " " "-"|tr -s ":" "."`
  lpat=/home/cmabct/vinod
  mach=`uname -s`
  myn=`uname -n`
  mailp=/usr/sbin/sendmail
  ids=${lpat}/dsk_ids.txt

  logs=${lpat}/LOGS/dsk_${day}.txt
  limit=${lpat}/LOGS/tmp_dsk.txt

  ##############################
  #
  #  Assign the list of mail ids to which the logs need to be send
  #	to add1 variable
  #

 # add1="To: Vinod.Das@blr.spcnl.co.in"
 
  sub="Subject: Disk Space Status($myn) on $day"
  header="Filesystem          kbytes    used   avail %used Mounted on"


  if [ ! -d ${lpat}/LOGS ]
	then
		mkdir -p ${lpat}/LOGS
  fi
  if [ ! -f $ids ]
	then
		return 1
  fi

 >$logs
 >$limit
}

cmds()
{
  case $mach in
	HP-UX) bdf | egrep "9[0-9]%|100%" > $limit;;
        SunOS) df -lkF ufs > $logs
	       df -lkF tmpfs >> $logs
	       cat $logs | egrep "9[0-9]%|100%" > $limit
	       mailp=/usr/lib/sendmail;;
	Linux) df -h | egrep "9[0-9]%|100%" > $limit;;
	    *) rm $logs $limit
	       return 1
  esac
}
  
creatfile()
{
  if [ -s $limit ]
      then
         	> $logs
 		cat $ids >$logs
		echo "$sub\n\n">>$logs
		echo "$header">>$logs
		cat $limit >>$logs
		sleep 1
		$mailp -t < $logs
     else
		rm $logs $limit
		return 1
  fi
}


mast()
{
  init
  if [ $? -ne 1 ]
    then
	  cmds
  fi

  if [ $? -ne 1 ]
    then
 	 creatfile
  fi
  if [ $? -ne 1 ]
    then
  	rm $limit
  fi
}

mast

