#!/bin/sh
#
#    Copyright (c) 2010 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    Check the kernel messages from syslogd and handle them accordingly.
#

#
# Finds out the pid of wdtd
#
wdtd_pid() {
	/bin/ps -C wdtd -o pid=
}

#
# Handle the CF card error messages by setting a bootenv to indicate the
# error and killing the wdtd to trigger a reboot. It doesn't rely on the
# reboot command to reboot because it requires access to the CF card.
#
handle_badcf() {
        echo "A fatal CF card failure has been detected. The system will" \
           "be rebooted. Please contact your service provider for CF card" \
	   "replacement." 
        echo "Rebooting in 30 seconds!!!"
        kill -n 9 $wdtd_pid
        /sbin/bootenv CFCardState bad
}

wdtd_pid=$(wdtd_pid);
while read line
do
        case "$line" in
        (*hda*UncorrectableError*)
                echo ---$line;
                handle_badcf;;
        (*hda*SectorIdNotFound*)
                echo ---$line;
                handle_badcf;;
        (*hda*AddrMarkNotFound*)
                echo ---$line;
                handle_badcf;;
        esac
done < /var/log/kmsg

