#!/bin/sh

#. /usr/local/bcnu/etc/bcnuenv
. $BCNUHOME/agent/agent_head

#set -x

VMGTOUTFILE=$BCNUTMP/${BCNUAGENT}.out
VMGTERRFILE=$BCNUTMP/${BCNUAGENT}.error

case "$BCNUSERVER" in

'solaris')
	/usr/opt/SUNWmd/sbin/metastat  >$VMGTOUTFILE
	bcnu_check_err "critical error running metastat command"
	grep "Maintenance" $VMGTOUTFILE |
	{
	 while read disk rest
	 do
	   echo "error - disk $disk requires maintenance" >>$VMGTERRFILE
	 done
	} 
	grep "In use" $VMGTOUTFILE |
	{
	 while read disk rest
	 do
	   echo "warning - Hot Spare $disk in use" >>$VMGTERRFILE
	 done
	} 
	;;

'aix')
	/usr/sbin/lsvg -l `/usr/sbin/lsvg`  >$VMGTOUTFILE
	bcnu_check_err "critical error running lsvg command"

	# find out logical volumes which are not open/syncd or closed/syncd
	$AWK 'BEGIN { HEAD=0; } {
  if ( NF != 7 ) { 
    next;
  }
  if ( $6 == "open/syncd" || $6 == "closed/syncd" ) {
    next;
  }
  else {
    if ( HEAD == 0 ) {
      printf "Logical Volume requiring maintenance:\n\n";
      HEAD=1;
    }
    printf "error - Logical Volume %s (state: %s, type: %s, mounted on: %s)\n", $1, $6, $2, $7;
  }
}' $VMGTOUTFILE >$VMGTERRFILE
	bcnu_check_err "critical error running awk command"

	if [ -s $VMGTERRFILE ]
	   then
		{
		printf "\nDetails of Logical Volumes requiring maintenance:\n"
		for LV in `grep "^error -" $VMGTERRFILE | cut -d" " -f5`
		do
			printf "\nLogical Volume $LV:\n"
			/usr/sbin/lslv -l $LV
		done
		echo
		} >>$VMGTERRFILE
	else
		rm -f $VMGTERRFILE
	fi
	;;

'hpux')
	/usr/sbin/vgdisplay -v | grep -e "LV Name" -e "LV Status" >$VMGTOUTFILE
	bcnu_check_err "critical error running vgdisplay command"
##
	$AWK 'BEGIN { HEAD=0; } {

        if (NR==1) { 
		lv=$3
		}
        if (NR==2) { 
		stat=$3
		NR=0

	  if ( stat == "available/syncd" || stat == "closed/syncd" ) {
	    next;
	  }
	  else {
	    if ( HEAD == 0 ) {
	      printf "Logical Volume requiring maintenance:\n\n";
	      HEAD=1;
	    }
	    printf "error - Logical Volume %s (state: %s)\n", lv, stat;
	  }
	}

	}' $VMGTOUTFILE >$VMGTERRFILE
##
	if [ -s $VMGTERRFILE ]
	   then
		{
		printf "\nDetails of Logical Volumes requiring maintenance:\n"
		for LV in `grep "^error -" $VMGTERRFILE | cut -d" " -f5`
		do
			printf "\nLogical Volume $LV:\n"
			/usr/sbin/lvdisplay $LV
		done
		echo
		} >>$VMGTERRFILE
	else
		rm -f $VMGTERRFILE
	fi

	;;

#
## added linux LVM agent 20000225 Volker Pense v.pense@profi-da.de 
#
'linux')
	 cat /proc/lvm | grep -A4 "PVs" > $VMGTOUTFILE
         bcnu_check_err 
         if grep -e "I" -e "C" $VMGTOUTFILE >/dev/null
		then
		cat $VMGTOUTFILE |
		{
		# first two fields are description next is lvname
		 while read one two disk rest
			 do
			  echo "error - disk $disk requires maintenance" >>$VMGTERRFILE
			# throw next line away
			read
		 done
		}
	fi 
	;;

esac

if [ -f $VMGTERRFILE ]
	then
		echo "-----------Full output follows---------" >>$VMGTERRFILE
		cat $VMGTOUTFILE >>$VMGTERRFILE
		bcnu_check_err "critical error creating vmgt.error file"
		BCNUMSG="$BCNU -m 'error - disk volume requires maintenance' -f $VMGTERRFILE -e $BCNU_ERR -t $BCNUHOSTTYPE $BCNUHOST"
		bcnu_err_send
#		rm $VMGTERRFILE
else
		BCNUMSG="$BCNU -m 'ok - disk volumes stable' -e $BCNU_OK -t $BCNUHOSTTYPE $BCNUHOST"
		bcnu_send
fi
