#!/bin/bash
# Copyrights (c) 2009-2012 Shoretel, Inc.
# set -x

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Function definitions
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function verbose() {
#	logger "$*"
       	echo $*
}
verbose "Starting detect hard drive failure script"
harddisk_failed="0"
if [ -n "$(grep sd[ab] /cf/shorelinedata/Logs/kmessages* | grep -i error )" ] ; then 
verbose "Hard disk errors detected"
grep sd[ab] /cf/shorelinedata/Logs/kmessages* | grep -i error | grep -i "I/O" > /etc/ubootenv/harddisk_errors
lines=$(cat /etc/ubootenv/harddisk_errors | wc -l)
if [ $lines -gt 15 ] ; then
echo "1" >/etc/ubootenv/harddisk_status
verbose "Real disk write errors"
harddisk_failed="1"
fi
else

if [ -n "$(grep sd[ab] /etc/ubootenv/harddisk_errors | grep -i error )" ] ; then 
verbose "Hard disk errors detected"
lines=$(cat /etc/ubootenv/harddisk_errors | wc -l)
if [ $lines -gt 15 ] ; then
echo "1" >/etc/ubootenv/harddisk_status
verbose "Real disk write errors"
harddisk_failed="1"
fi
fi
fi




if [ -n "$(grep -i scsi /cf/shorelinedata/Logs/kmessages* | grep -i error )" ] ; then 
verbose "Hard disk scsi errors detected"
grep -i scsi /cf/shorelinedata/Logs/kmessages* | grep -i "error" > /etc/ubootenv/hardscsi_errors 
lines=$(cat /etc/ubootenv/hardscsi_errors | wc -l)
if [ $lines -gt 15 ] ; then
echo "1" >/etc/ubootenv/harddisk_status
harddisk_failed="1"
verbose "scsi write errors"
fi
fi
#if [ -n "$(cat /proc/mdstat | grep F)" ] ; then
#cat /proc/mdstat | grep "F" >/etc/ubootenv/raid_harddisk_errors
#echo "1" >/etc/ubootenv/harddisk_status
#harddisk_failed="1"
#echo "Hardisk Raid Error detected"
#fi

if [ "$harddisk_failed" == "0" ] ; then
echo "0" >/etc/ubootenv/harddisk_status
verbose " Hard disk running OK"
fi


if [ -n "$(cat /proc/mdstat | grep recovery)" ] ; then
	echo "1" >/etc/ubootenv/recoveryin_process
else
	echo "0" >/etc/ubootenv/recoveryin_process
fi

verbose "Exiting detect hard drive failure script"
