#!/bin/bash
# $Copyright$     NB: This is an auto-generated comment with copyright note below. DO NOT MODIFY OR REMOVE!
# Copyright 2016-2017 FUJITSU LIMITED
# All rights reserved
#

#
# This script gets all relevant logs from all installed MegaRAID controllers
#
# History:
#
# 07/07/10      created by HS DE5
# 10/12/11      update to new commands to get more information (HS HW5)
# 05/18/15      update to new commands to get more information (HS HW5)
# 06/29/15      controller info will be store in separate file for each ctrl
# 03/14/15		modified to run unter PrimeCollect script (Franz Schlosser, E SW2)
# 09/13/17		added additional prints for automated inspection (RG RAID) 

echo
echo "MRgetLog started"

WORKPATH=.      # path to script folder 
THIS_SCRIPT_DIR=`dirname $0`

if [ "$THIS_SCRIPT_DIR" == "" ]
then
	THIS_SCRIPT_DIR=.
fi

echo "Current dir:  `pwd`"
echo "Script dir:   $THIS_SCRIPT_DIR"

#check for installed StorCli (for standalone installation)
if [ `arch` == "x86_64" ]
then
       	STORCLI_BIN=bin/storcli64
else
	STORCLI_BIN=bin/storcli32
fi
STORCLI_INST=storcli

#first search for storcli as correctly installed (32/64-bit) executable in same path as this script
if [ -f "$THIS_SCRIPT_DIR/$STORCLI_INST" ]; then
	STORCLI=$THIS_SCRIPT_DIR/$STORCLI_INST

#next search for standalone installation in bin sirectory 
elif [ -f "$THIS_SCRIPT_DIR/$STORCLI_BIN" ]; then
	STORCLI=$THIS_SCRIPT_DIR/$STORCLI_BIN

else
	echo
	echo "ERROR! StorCli not found (neither as $THIS_SCRIPT_DIR/$STORCLI_INST nor as $THIS_SCRIPT_DIR/$STORCLI_BIN)"
	echo
	exit 1
fi

echo "StorCli path: $STORCLI"

#cleanup first
rm -f CmdTool.log
rm -f MegaSAS.log

#find number of controllers in system
CTRL=`$STORCLI show ctrlcount | grep "Controller Count =" | cut -d "=" -f 2|cut -c 2`
echo "Found $CTRL MegaRAID controller in system"
CURCTRL=0
while test $CURCTRL -lt $CTRL
        do
	LOGFILE=`echo "MR$CURCTRL.txt"`
	echo "MRGetlog started at `date +%c`" > $LOGFILE
	$STORCLI -v >> $LOGFILE
	echo "********** begin log for controller $CURCTRL **********" >> $LOGFILE
	echo Controller_Eventlog >> $LOGFILE
	$STORCLI /c$CURCTRL show events >> $LOGFILE
	echo Controller_Eventlog\\ >> $LOGFILE
	sleep 1
	echo Controller_Info >> $LOGFILE
	#echo "********** begin log for controller $CURCTRL **********" >> $LOGFILE
	DEVID=`$STORCLI /c$CURCTRL show all | grep -m 1 "Device Id =" | cut -d "=" -f 2 | cut -c 4-6`
        $STORCLI /c$CURCTRL show all >> $LOGFILE
	sleep 1
	echo Controller_Info\\ >> $LOGFILE
	echo Controller_BBU >> $LOGFILE
	case $DEVID in
		5B) $STORCLI /c$CURCTRL /cv show all >> $LOGFILE;;
		5D) $STORCLI /c$CURCTRL /cv show all >> $LOGFILE;;
		79) $STORCLI /c$CURCTRL /bbu show all >> $LOGFILE;;
		60) $STORCLI /c$CURCTRL /bbu show all >> $LOGFILE;;
		411) $STORCLI /c$CURCTRL /bbu show all >> $LOGFILE;;
		413) $STORCLI /c$CURCTRL /bbu show all >> $LOGFILE;;
	esac
	echo Controller_BBU\\ >> $LOGFILE
	sleep 1
	echo Controller_Termlog >> $LOGFILE
	$STORCLI /c$CURCTRL show termlog  >> $LOGFILE
	echo Controller_Termlog\\ >> $LOGFILE
	sleep 1
	echo "********** end log for controller $CURCTRL **********" >> $LOGFILE
        CURCTRL=`expr $CURCTRL + 1`
        done
sleep 1
echo "Get log done. Please forward the file MR*.txt to the service technican"

#cleanup finally
rm -f CmdTool.log
rm -f MegaSAS.log

