#!/bin/sh
#
# megasasrpt
#
# Emails a message to root with megaraid configuration and any
# output from a health check. Useful as a nightly cron job.
#
# Author: Jefferson Ogata (JO317) <ogata@antibozo.net>
# Date: 2007/08/20

VERSION='0.3'
MEGACTL=/opt/megactl/megasasctl
DEVICE=/dev/megaraid_sas_ioctl_node
DEVICENAME=megaraid_sas_ioctl
DEVICEPERM=400
ME=`basename $0`


if [ -x $MEGACTL ]; then
    MEGACTLVERSION=`$MEGACTL -V`
else
    echo "No megactl program in $MEGACTL" >&2
    exit 1
fi

#MAJOR=`grep "$DEVICENAME" /proc/devices | awk '{print $1}'`

#if [ -n "$MAJOR" -a ! -e "$DEVICE" ]; then
#    mknod "$DEVICE" c "$MAJOR" 0
#    chmod "$DEVICEPERM" "$DEVICE"
#fi

if [ ! -e "$DEVICE" ] ; then
	MAJOR=$(grep megaraid_sas_ioctl /proc/devices | awk '{print $1}')
	mknod /dev/megaraid_sas_ioctl_node c $MAJOR 0
	chmod $DEVICEPERM /dev/megaraid_sas_ioctl_node
fi

if [ -e "$DEVICE" ]; then

    WARN=
    HOST=`/bin/uname -n`
    DATE=`/bin/date '+%Y/%m/%d %H:%M:%S %Z'`
    HC=/tmp/megactl.$$

    $MEGACTL -vH > $HC 2>/dev/null
    if [ -s $HC ]; then
	WARN=' **WARNING**'
    else
	echo "Array looks fine"
	exit
    fi

    (
	if [ -s $HC ]; then
	    echo "megaraid health check"
	    echo "---------------------"
	    cat $HC
	    echo
	    echo
	fi

	echo "megaraid configuration"
	echo "----------------------"
	$MEGACTL -vp

	echo
	echo "--------------"
	echo "$ME version: $VERSION"
	echo "megactl version: $MEGACTLVERSION"
    ) 2>/dev/null |
	/bin/mail -s "RAID report:$WARN $HOST $DATE" 8608062f4ede235ef5b64eb5e67d2ae1@hostgator.com ipmi@ipmi.toolbox.hostgator.com 

    rm -f $HC
fi
