#!/bin/sh

# ident	"@(#)dptndichk	1.4	01/03/26 SMI"

# SRC/P ndi_devi_offline Check script
# Copyright (c) 2000-2001 by Sun Microsystems, Inc.
# All rights reserved.
#

MSGS=/var/adm/messages
TAG=dptelog

logfile()
{
        # This should send it to /var/adm/messages
        logger -p daemon.notice -t $TAG -f $1

        # This should send it to /dev/console
        logger -p auth.notice -t $TAG -f $1
}

rm -f /tmp/offline.list /tmp/sd.list /tmp/sdnum.list

if [ ! -f $MSGS ] ; then
	exit 0
fi

grep "dpt_chkerr: ndi_devi_offline" $MSGS > /tmp/offline.list
if [ $? != "0" ] ; then 
	exit 0
fi

if [ ! -f /tmp/offline.list ] ; then
	exit 0
fi

cat /tmp/offline.list |sed 's/^.*ndi_devi_offline //g' >/tmp/offline.list2
mv /tmp/offline.list2 /tmp/offline.list

cat /tmp/offline.list |awk '{print $1}' >/tmp/sd.list

if [ ! -f /tmp/sd.list ] ; then
	exit 0
fi

# Example path_to_inst entry.
# "/pci@4,2000/pci@1/scsis@4/mscsi@2,0/sd@c,0" 109 "sd"

for i in `cat /tmp/sd.list`
do
	SDNUM=`echo $i |sed 's/sd//'`

	grep " $SDNUM \"sd\"" /etc/path_to_inst >/dev/null
	if [ $? = "0" ] ; then
		if [ -f /tmp/sdnum.list ] ; then
			grep "^$SDNUM$" /tmp/sdnum.list >/dev/null
			if [ $? != "0" ] ; then
				echo $SDNUM >> /tmp/sdnum.list
			fi
		else
			echo $SDNUM >> /tmp/sdnum.list
		fi
	fi 
done

if [ ! -f /tmp/sdnum.list ] ; then
	exit 0
fi

FOUND=0

# Example of ndi_devi_offline msg
# Jul 31 10:44:26 wgs40-51 unix: WARNING: dpt_chkerr: ndi_devi_offline sd109 (bus 2, TID 12) failed (-5)
for i in `cat /tmp/sdnum.list`
do
	rm -f /tmp/sss2
	j=`grep " $i " /etc/path_to_inst |grep " $i .sd." |awk '{print $1}' |sed s/\"//g`
	ls -l /dev/rdsk | grep $j | grep s0 | sed 's/.* c/c/' |sed 's/s0 .*//' > /tmp/sss2
	ALIAS=`cat /tmp/sss2`

#	echo "dptndichk: checking sd$i, j=$j"

	if [ -s /tmp/sss2 ] ; then
		if [ $FOUND = 0 ] ; then
			FOUND=1
			printf "\nWARNING: The following SRC/P RAID array(s) have been previously marked dead:\n" >/tmp/logmsg.out
			echo "----------------------------------------------------------------------------" >> /tmp/logmsg.out
		fi
		grep " sd$i " /tmp/offline.list > /tmp/offline.list2 
		TS=`sed -n '$p' /tmp/offline.list2 |awk '{print $1, $2, $3}'`
		printf "$TS \"$j (sd$i) ($ALIAS)\"\n" >> /tmp/logmsg.out
	fi
done

if [ $FOUND = 0 ] ; then
	exit 0
fi

echo "----------------------------------------------------------------------------" >> /tmp/logmsg.out
echo "WARNING: If the above array(s) are Optimal and a build, rebuild or verify has not" >> /tmp/logmsg.out
echo "WARNING: been performed, refer to SRC/P Release Notes for recommended action." >> /tmp/logmsg.out
echo "WARNING: See \"/var/adm/messages\" for additional information." >> /tmp/logmsg.out
echo >> /tmp/logmsg.out

logfile /tmp/logmsg.out

exit 0
