#!/bin/sh

# bb-emc
#
# BIG BROTHER - EXTERNAL SCRIPT FOR EMC POWERPATH
# David Levin - Intel MCD IS
# Version 1.0
# October 22 2001
#

#
# This external script will check the status of Powerpath devices.
# Reports red if device not operating.
# Reports clear if tool is not found to run test.
#

#
# SCRIPTS IN THE BBHOME/ext DIRECTORY ARE ONLY RUN IF 
# LISTED IN THE BBEXT VARIABLE OF $BBHOME/runbb.sh
# THIS IS FOR SECURITY.
#

#
# TEST NAME: THIS WILL BECOME A COLUMN ON THE DISPLAY
# IT SHOULD BE AS SHORT AS POSSIBLE TO SAVE SPACE...
# NOTE YOU CAN ALSO CREATE A HELP FILE FOR YOUR TEST
# WHICH SHOULD BE PUT IN www/help/$TEST.html.  IT WILL
# BE LINKED INTO THE DISPLAY AUTOMATICALLY.
#
TEST="emc"

# 
# BBPROG SHOULD JUST CONTAIN THE NAME OF THIS FILE
# USEFUL WHEN YOU GET ENVIRONMENT DUMPS TO LOCATE 
# THE OFFENDING SCRIPT...
#
BBPROG=bb-$TEST.sh; export BBPROG

#
# NOW SET SITE SPECIFIC VARIABLES
# THE PATH WHERE THE POWERPATH EXECUTABLE IS LOCATED
# NOTE: THIS PROGRAM CAN ONLY BE RUN BY ROOT.
# sudo UTILITY IS REQUIRED TO RUN THE POWERPATH BINARY
# Make sure you have configured the BB user in sudo for both commands
#
PMTCMD="/etc/powermt"
PMTDISP1="sudo $PMTCMD display"
PMTDISP2="sudo $PMTCMD display dev=all"

#
# BBHOME CAN BE SET MANUALLY WHEN TESTING.
# OTHERWISE IT SHOULD BE SET FROM THE BB ENVIRONMENT
#
# BBHOME=/home/sean/bb; export BBHOME	# FOR TESTING

if test "$BBHOME" = ""
then
	echo "BBHOME is not set... exiting"
	exit 1
fi

if test ! "$BBTMP"                      # GET DEFINITIONS IF NEEDED
then
	 # echo "*** LOADING BBDEF ***"
        . $BBHOME/etc/bbdef.sh          # INCLUDE STANDARD DEFINITIONS
fi

#
# NOW COLLECT SOME DATA
# 
COLOR="clear"
# Test that the binary exists
if test -x $PMTCMD
then
# Set status for green, if errors are found status will be changed.
  test_color="green"

  $PMTDISP1 > $BBTMP/$MACHINE.$TEST.disp1
# Test result file for error status
  result=`$GREP sbus $BBTMP/$MACHINE.$TEST.disp1 | cut -c38-50`
  for string in $result
  do
    set $string
    string=$1
    if [ $string != "Optimal" ]
    then
      test_color="red"
    fi
  done

# Generate the status line to report back to the BB server.
  LINE="$LINE

&${test_color} EMC PowerPath summary status is ${test_color}.

`cat $BBTMP/$MACHINE.$TEST.disp1`
"

# Run detailed report.

  $PMTDISP2 > $BBTMP/$MACHINE.$TEST.disp2
# Test result file for error status
  result=`$GREP sbus $BBTMP/$MACHINE.$TEST.disp2 | cut -c26-33`
  for string in $result
  do
    set $string
    string=$1
    if [ $string != "Active" ]
    then
      test_color="red"
    fi
  done

# Generate the status line to report back to the BB server.
  LINE="$LINE

&${test_color} EMC PowerPath detailed device status is ${test_color}.

`cat $BBTMP/$MACHINE.$TEST.disp2`
"

# Cleanup, remove temp report files.
$RM $BBTMP/$MACHINE.$TEST.disp*

# Set final result for reporting.
COLOR=$test_color

# Result if the EMC powermt command is not found.
else
	test_color="clear"
	COLOR="clear"
	test_flag=1
	LINE="$LINE

&${test_color} EMC PowerPath status is ${test_color}.

Attention, admin program $PMTCMD not found.  Please re-check paths.
"
fi

#
# AT THIS POINT WE HAVE OUR RESULTS.  NOW WE HAVE TO SEND IT TO
# THE BBDISPLAY TO BE DISPLAYED...
#

#
# THE FIRST LINE IS STATUS INFORMATION... STRUCTURE IMPORANT!
# THE REST IS FREE-FORM - WHATEVER YOU'D LIKE TO SEND...
#
LINE="status $MACHINE.$TEST $COLOR `date`
EMC PowerPath Status
$LINE
"

# NOW USE THE BB COMMAND TO SEND THE DATA ACROSS

# commented out till sure code works.
#
$BB $BBDISP "$LINE"			# SEND IT TO BBDISPLAY
