#!/usr/bin/ksh
#####################################################################
#
#    Author : Adam Radage 
#
#
#    Date : 20/02/12
#
#    Usage : . /hds_device_assignments.ksh -s <Site>
#       or : . /hds_device_assignments.ksh -h
#
#    Description : A script to report on the HDS Device Assignments
#
#
#####################################################################
# Version History
# ~~~~~~~~~~~~~~~
#
# Date        Version  Author           Description
# -------------------------------------------------------------------
# 20.02.2012   1.1      Adam Radage      Initial version

#####################################################################
#
#####################################################################
# Variables
#####################################################################
# Variables for date & time to append to filenames to make them unique
 DATENOW=`date +"%d%m%y"`
 MONTH=`date +"%b%y"`

# General variables
 SYNTAX_NO=0
 PARAM_NO=$#
 SCRIPT=$(basename $0)
 LOGFILE="./logs/${SCRIPT}_${MONTH}.log"
 HORCMINSTANCE=0
 VERB=0

# Setup environment
 HDSBIN="/export/home/san/hds/"
 HORCMBIN="/opt/HORCM/usr/bin/"
 HORCMDIR="/export/home/san/hds/horcm/"
 HORCMBKUP="/export/home/san/hds/horcm/backup/"
 HICOMMANDCLI="/opt/HiCommandCLI/HiCommandCLI "
 SOEID=`/usr/ucb/whoami`
 UNAME=`/bin/uname -n`

 PBENV="/opt/pb/bin/pbrun"
 PBRUN="${PBENV} "

#####################################################################
# Key HDS commands
###################################################################### List of HDS Arrays
# /opt/pb/bin/pbrun /opt/HiCommandCLI/HiCommandCLI GetStorageArray -u <user> -p <password>|egrep "serialNumber=|name="|sed 's/=/ /'|awk '{print $1, $2}'

# List of all LDEVs
# /opt/pb/bin/pbrun /opt/HiCommandCLI/HiCommandCLI GetStorageArray subtarget=LDEV serialnum=<arrayid> -u <user> -p <password>|grep "displayName="|sed 's/=/ /'|awk '{print $2}'

# List of Hosts & WWNs
# /opt/pb/bin/pbrun /opt/HiCommandCLI/HiCommandCLI GetHost -u <user> -p <password>|grep "name="|sed 's/=/ /'|awk '{print $2}'

# List of Hosts, LDEVs & LUN
# /opt/pb/bin/pbrun /opt/HiCommandCLI/HiCommandCLI GetStorageArray subtarget=HostStorageDomain hsdsubinfo=path serialnum=<arrayid> -u <user> -p <password>|egrep "nickname=|displayDevNum=|LUN="|grep -v "          nickname="|sed 's/=/ /'|awk '{print $1, $2}'

#####################################################################
# Powerbroker & CLI path
#####################################################################
 # Check if Powerbroker is present, otherwise use full command path
 if [[ ! -e $PBENV ]]
  then
    PREFIX=$HICOMMANDCLI
    echo "Powerbroker is not present, using path: ${PREFIX}"
  else
    PREFIX=$PBRUN$HICOMMANDCLI
    echo "Powerbroker is present, using path: ${PREFIX}"
 fi

#####################################################################
# Functions
#####################################################################
function exit_error
{
  EXIT_CODE=$1
  EXIT_MESSAGE=$(echo $@ | cut -f 2- -d " ")

  echo "$(date):$SCRIPT:$EXIT_MESSAGE" 2>&1 >>./logs/${HDS}log.${DATENOW}.log
  exit $EXIT_CODE
}

#####################################################################
function check_parameters
{

# Check to see whether the number of parameters is correct.

case $PARAM_NO in
  1 ) if [ $1 = "-h" ]				# Help
      then
        help_info
        VERB=0
      else
        echo "Incorrect parameters have been issued. For help type       ./hds_device_assignments.ksh -h"
        exit_error 1 " Incorrect parameters have been issued"
      fi;;
  2) if [ $1 = "-s" ]				# Site flag
      then
         SITE=$2
         VERB=1
         HDS="HDS_"
         echo "Site is: $SITE"
         echo "Investigating HDS arrays..."
      else
        echo "Incorrect parameters have been issued. For help type ./hds_device_assignments.ksh -h"
        exit_error 1 " Incorrect parameters have been issued"
      fi;;
  * ) echo "Incorrect parameters have been issued. For help type ./hds_device_assignments.ksh -h"
      exit_error 1 " Incorrect parameters have been issued";;
esac
}

#####################################################################
function create_file
{

# Create empty headed results file for output file
  echo "Site SAN_Type Device_Key Array_Name LUN Hostname">${SITE}_${HDS}Device_Assignments_${DATENOW}.txt;

}

#####################################################################
function get_info
{

 # Prompt for username
 user_prompt

 # Prompt for password
 password_prompt

 # Retrieve host allocations using HDS command

 echo "`date`. Retrieving host allocations ..." | tee -a $LOGFILE

 # List of Hosts, LDEVs & LUN
 ${PREFIX}GetStorageArray subtarget=HostStorageDomain hsdsubinfo=path -u ${USERNAME} -p ${PASSWORD}|egrep "serialNumber=|name=|nickname=|displayDevNum=|LUN="|grep -v "          nickname="|grep -v "    name="|sed 's/=/ /'|awk '{print $1, $2}'>${SITE}_${HDS}hostlist.temp 

    echo "`date`. Data retrieved for host allocations ..." | tee -a $LOGFILE
    echo "`date`. Data processing for host allocations ..." | tee -a $LOGFILE

    # Run through the hostlist and output to file
    while read HOSTLIST
     do
       case `echo ${HOSTLIST}|awk '{print $1}'` in
         name)			HDSALIAS=`echo ${HOSTLIST}|awk '{print $2}'`;;
         serialNumber)		HDSNAME=`echo ${HOSTLIST}|awk '{print $2}'`;;
         nickname)		HOSTNAME=`echo ${HOSTLIST}|awk '{print $2}'`;;
         displayDevNum)		typeset -u DEVICE=`echo ${HOSTLIST}|awk '{print $2}'`;;
         LUN)			LUN=`echo ${HOSTLIST}|awk '{print $2}'`
      				echo "${SITE} HDS ${HDSNAME}_${DEVICE} ${HDSALIAS} ${LUN} ${HOSTNAME}">>${SITE}_${HDS}Device_Assignments_${DATENOW}.txt;;
         *)echo "Not found";;
       esac

     done<${SITE}_${HDS}hostlist.temp 

     echo "`date`. Data processing completed for host allocations ..." | tee -a $LOGFILE

rm ${SITE}_${HDS}hostlist.temp 

}

#####################################################################
function sort_unique
{

  echo "`date`. Removing duplicate entries ..." | tee -a $LOGFILE

  # Remove all duplicates and copy to temp file
  cat ${SITE}_${HDS}Device_Assignments_${DATENOW}.txt>${SITE}_${HDS}Device_Assignments_${DATENOW}.temp

  # Overwrite original file with unique list
  cat ${SITE}_${HDS}Device_Assignments_${DATENOW}.temp|sort -u>${SITE}_${HDS}Device_Assignments_${DATENOW}.txt

  # Remove temp file
  rm ${SITE}_${HDS}Device_Assignments_${DATENOW}.temp

  echo "`date`. Duplicate entries removed." | tee -a $LOGFILE
  echo "`date`. HDS script '${SCRIPT}' complete." | tee -a $LOGFILE

}

#####################################################################
function user_prompt
{

  print "\nPlease enter username: "
  read USERNAME

}

#####################################################################
function password_prompt
{

  print "\nPlease enter password: "
  read PASSWORD

}

#####################################################################
function help_info
{

# Help data
 echo "Help information"
 echo " Specify the site name by typing 
 	. /hds_device_assignments.ksh -s <Site>"
 echo " for help type 
      . /hds_device_assignments.ksh -h"

}

#####################################################################
#####################################################################    
# Start of Main Script
#####################################################################

check_parameters $@

case $VERB in
 1)
   create_file
   get_info
   sort_unique;;
esac

exit 0

#####################################################################
#####################################################################

