#!/bin/ksh
##########
#
# MetaChk
#
# Author: Peter Magee - pmdba@aol.com
#
# Check the state of meta devices and present
# the results in an easy to read table. Note
# errors in the system log and mail the results
# to root.
#
##########

PROGRAM=`basename $0`
LOGGER='/usr/bin/logger'
MAIL='/usr/bin/mailx'
METASTAT='/usr/sbin/metastat'
METADB='/usr/sbin/metadb -i'
LINE='0'
OUTFILE=/tmp/metachk$$.log

# Open the report file and print the column headers

echo "\nMeta Device State Check Summary" > $OUTFILE
echo `date` >> /tmp/metachk$$.log

echo "\nMetaDevice  DiskDevice  Mount Point  Size    Used  State       Comment" >> $OUTFILE
echo "==========  ==========  ===========  ======  ====  ==========  ===============" >> $OUTFILE

# Run metastat and parse the outout

eval $METASTAT | while read line
do
  set -f $line

  # Check first character of first column to determine which row of output we are on

  CHKD=`echo $1 | awk '{print substr($1,1,1)}'`

  # If this is a new meta device, then print the previous metadevice and collect new data

  if [ "$CHKD" = "d" ]; then
    if [ $LINE != "0" ]; then
      echo $DEVNAME $DISKDEV $MOUNTPT $SIZE $USED $STATE $COMMENT | \
        awk '{printf"%-11s %-11s %-12s %-3s %-3s %-5s %-11s %s %s %s\n",$1,$2,$3,$4,$5,$6,$7,$8,$9,$10}' | \
        sed s/-/' '/g >> $OUTFILE
    fi

    # Collect device name, mount point, usage, and comment data

    COMMENT=`echo $2 $3 $4`

    DEVNAME=`echo $1 | sed s/://`
    CHKC=`echo $COMMENT | awk '{print substr($1,1,3)}'`
    if [ "$CHKC" != "Sub" ] && [ $LINE != "0" ]; then
      echo " " >> $OUTFILE
    fi

    MOUNTCK=`swap -l | grep $DEVNAME`
    if [ -n "$MOUNTCK" ]; then
      MOUNTPT='swap'
      USED='N/A'
    else
      MOUNTPT=`grep $DEVNAME /etc/vfstab | awk '{print $3}'`
      USED=`df -k $MOUNTPT | grep $DEVNAME | awk '{print $5}'`
    fi

    if [ -n $MOUNTPT ]; then
      COMMENT="$COMMENT of $MOUNTPT"
    fi

    DISKDEV='-'
    SIZE='- -'
    STATE='-'
    LINE='1'
  fi
  
  # Collect device size information

  if [ "$1" = "Size:" ] && [ "$CHKC" != "Sub" ]; then
    SIZE=`echo $4 $5 | sed s/[\(\)]//g`
  fi

  # Collect disk device, state, reloc, and spare data

  if [ "$CHKD" = "c" ]; then
    DISKDEV=`echo $1`
    MOUNTPT='-'
    USED='-'
    STATE=`echo $4`

    # Create 'tree' connections between devices and sub-devices

    CHKN=`echo "$DEVNAME" | awk '{print substr($1,1,1)}'`
    if [ "$CHKC" = "Sub" ] && [ "$CHKN" = "d" ]; then
      DEVNAME=`echo "|__"$DEVNAME`
    fi

    # If state is not Okay or blank, then write alert to system log

    if [ "$STATE" != "Okay" ] && [ "$STATE" != "-" ]; then
      $LOGGER -p daemon.warn "$PROGRAM event: Meta Device $DEVNAME / $DISKDEV is $STATE"
    fi
  fi

  if [ $2 = "Relocation" ]; then
    break;
  fi
done

# Print last device information to output file

echo $DEVNAME $DISKDEV $MOUNTPT $SIZE $USED $STATE $COMMENT | \
  awk '{printf"%-11s %-11s %-12s %-3s %-3s %-5s %-11s %s %s %s\n\n",$1,$2,$3,$4,$5,$6,$7,$8,$9,$10}' | \
  sed s/-/' '/g >> $OUTFILE

echo "Meta DB Status" >> $OUTFILE
echo "==============" >> $OUTFILE
$METADB >> $OUTFILE
echo " " >> $OUTFILE

# Mail output file to root

#cat $OUTFILE | $MAIL -s "MetaChk Report" root
more $OUTFILE

# Delete output file

rm $OUTFILE







##############################################################################
### This script is submitted to BigAdmin by a user of the BigAdmin community.
### Sun Microsystems, Inc. is not responsible for the
### contents or the code enclosed. 
###
###
### Copyright 2006 Sun Microsystems, Inc. ALL RIGHTS RESERVED
### Use of this software is authorized pursuant to the
### terms of the license found at
### http://www.sun.com/bigadmin/common/berkeley_license.html
##############################################################################


