#!/bin/bash
# $Copyright$
# Copyright (C) Fujitsu Technology Solutions 2012
# All rights reserved
#
# This script get all relevant log from all installed MegaRAID controller
#
#History
# 07/07/10      created by HS DE5
# 10/12/11      update to new commands to get more information (HS HW5)


WORKPATH=.      # path to script folder 
LOGFILE="MegaRAIDlog.txt"
TMPFILE="mrtmp.txt"




echo "MRGetlog started at `date +%c`" > $LOGFILE
#check for installed MegaCli
if rpm -qa | grep Mega
        then
                uname -a > systemtype
                        if grep "x86_64" systemtype
                                then
                                        MEGACLI=/opt/MegaRAID/MegaCli/MegaCli64
                                else
                                        MEGACLI=/opt/MegaRAID/MegaCli/MegaCli
                        fi
        else
                echo "No MegaCli tool installed. Please install latest version" >> $LOGFILE
                echo "No MegaCli tool installed. Please install latest version"
                exit 2
fi
#find number of controllers in system
CTRL=`$MEGACLI -adpcount|grep "Controller Count:"|cut -d ":" -f 2|cut -c 2`
echo "Found $CTRL MegaRAID controller in system" >> $LOGFILE
echo "Found $CTRL MegaRAID controller in system"
$MEGACLI -v >> $LOGFILE
CURCTRL=0
while test $CURCTRL -lt $CTRL
        do
	echo "********** begin log for controller $CURCTRL **********" >> $LOGFILE
        $MEGACLI -AdpAllInfo -a$CURCTRL >> $LOGFILE
	sleep 1
#        $MEGACLI -AdpAliLog -a$CURCTRL >> $LOGFILE
# 	 This function has problem with older hard disk drive
	sleep 1
	$MEGACLI -AdpGetPciInfo -a$CURCTRL >> $LOGFILE
	sleep 1
	if $MEGACLI -adpallinfo -a$CURCTRL | grep "BBU              : Absent"
		then
		echo "No BBU installed, skip get BBU status" >> $LOGFILE
		else
        	$MEGACLI -AdpBBUCmd -a$CURCTRL >> $LOGFILE
	fi
	sleep 1
	$MEGACLI -adpeventlog -getevents -f events.tmp -a$CURCTRL
	cat $LOGFILE events.tmp > MRlog.tmp
	mv MRlog.tmp $LOGFILE
	sleep 1
	$MEGACLI -fwtermlog -dsply -a$CURCTRL >> $LOGFILE
	sleep 1
	echo "********** end log for controller $CURCTRL **********" >> $LOGFILE
        CURCTRL=`expr $CURCTRL + 1`
        done
rm *.tmp
sleep 1
echo "Get log done. Please forward the file $LOGFILE to the IO-Lab"
