#!/usr/bin/ksh93
################################################################
function usagemsg_gethmclic {
  print "
Program: gethmclic

This utility extracts system and power subsystem firmware level
information from a list of HMC's and generates HTML tables 
containing this information.  These HTML tables are intended to be
included (SSI) into other HTML pages are are not themselves complete
HTML documents.

Usage: ${1##*/} [-v] [-V] [-s] [-p] [-u sshuser] \\
                   [-f hmclistFile] | hmcHostName...

              -v = Verbose mode
              -V = Very Verbose Mode
              -s = Only gather and report system firmware information.
              -p = Only gather and report power subsystem firmware information.
      -u sshuser = Specify the SSH user ID to use to gain access to
                   the HMC information.  This user ID must be configured
                   to allow password-less entry to the HMC.  The default
                   user ID is \"hmcroot\".
  -f hmclistFile = Specify a file containing a list of HMC hostname
                   to gather information from.
     hmcHostName = Specify one or more HMC hostnames on the command line
                   from which to gather the system and power subsystem
                   firmware information.

Author: Dana French (dfrench@mtxia.com)
\"AutoContent\" enabled"

}
################################################################
#### 
#### Description:
#### 
#### Assumptions:
#### 
#### Dependencies:
#### 
#### Products:
#### 
#### Configured Usage:
#### 
#### Details:
#### 
################################################################
function gethmclic {
  typeset TRUE="1"
  typeset FALSE="0"
  typeset GETSYS="${TRUE}"
  typeset GETPOW="${TRUE}"
  typeset SSHUSER="hscroot"
  typeset HMCFILE=""
  typeset VERBOSE="${FALSE}"
  typeset VERYVERB="${FALSE}"

  while getopts ":vVspu:f:" OPTION
  do
      case "${OPTION}" in
          'v') VERBOSE="${TRUE}";;
          'V') VERYVERB="${TRUE}";;
          's') GETPOW="${FALSE}";;
          'p') GETSYS="${FALSE}";;
          'u') SSHUSER="${OPTARG}";;
          'f') HMCFILE="${OPTARG}";;
          '?') usagemsg_gethmclic "${0}" && exit 1 ;;
      esac
  done
   
  shift $(( ${OPTIND} - 1 ))
  
  (( VERYVERB == TRUE )) && set -x
  
  trap "usagemsg_gethmclic ${0}" EXIT
      HMCLIST=( ${HMCFILE:+$( cat "${HMCFILE}" )} )
      HMCLIST=( "${HMCLIST[@]:-${@}}" )
      [[ "_${HMCLIST[@]}" = "_" ]] && exit 2

      (( GETSYS == FALSE && GETPOW == FALSE )) &&
          print -u 2 "# ERROR: Specify either -s or -p, but not both" &&
          exit 3
  trap "-" EXIT

################################################################
#### 
#### The "lssyscfg" command arguments used to generate a list of system names from
#### the HMC are "-r sys -F name".  If an HMC requires different arguments to
#### generate a list of system names, create an array element here using the
#### HMC hostname as the associative array index and the complete list of
#### command line arguments for the "lssyscfg" command as the value.
#### 
################################################################  

typeset -A LSSYSCFG
LSSYSCFG[mxflphmc01]="-r sys -F name"
LSSYSCFG[mxdlphmc01]="-r sys -F name"
# LSSYSCFG[mxflahmc01]="-r sys --all -F name"
# LSSYSCFG[mxdlahmc01]="-r sys --all -F name"

################################################################
#### 
#### The "lslic" command arguments used to generate the list of firmware
#### levels from the HMC are "-F activated_level:installed_level:accepted_level". 
#### If an HMC requires different arguments to generate the list of firware
#### levels, create an array element here using the HMC hostname as the
#### associative array index and the list of command line arguments for the
#### "lslic" command as the value.
#### 
################################################################  

typeset -A LSLIC
LSLIC[mxflphmc01]="-F activated_level:installed_level:accepted_level"
LSLIC[mxdlphmc01]="-F activated_level:installed_level:accepted_level"
# LSLIC[mxflahmc01]="-F activated_level:installed_level:accepted_level"
# LSLIC[mxdlahmc01]="-F activated_level:installed_level:accepted_level"

################################################################  
#### Loop through each HMC hostname and generate a list of system names
#### managed by the HMC.
################################################################  

for HMCNAME in "${HMCLIST[@]}"
do

  (( VERBOSE == TRUE )) && print -u 2 "# \n# Retrieving system names from HMC ${HMCNAME}"
  print "


" print "

${HMCNAME}

" NAMES=( $( ssh ${SSHUSER}@${HMCNAME} lssyscfg ${LSSYSCFG[${HMCNAME}]:- -r sys -F name} | sort -n | uniq ) ) print "

" ################################################################ #### If the user selected to gather and report system firmware information, #### then generate the appropriate HTML table headings and loop through each #### system name managed by the HMC to retrieve the system firmware levels. ################################################################ if (( GETSYS == TRUE )) then print "

" ################################################################ #### Gather and report the system firmware levels for each system managed by #### the HMC using the ssh command to execute the "lslic" command on the #### remote HMC. ################################################################ for SYSNAME in "${NAMES[@]}" do (( VERBOSE == TRUE )) && print -u 2 "# Retrieving system firmware level for ${SYSNAME}" RESULT=$( ssh ${SSHUSER}@${HMCNAME} lslic -m "${SYSNAME}" -t sys ${LSLIC[${HMCNAME}]:- -F activated_level:installed_level:accepted_level} ) (( VERBOSE == TRUE )) && print -u 2 "# Reporting system firmware level for ${SYSNAME}" print "

" done fi ################################################################ #### If the user selected to gather and report power subsystem firmware #### information, then generate the appropriate HTML table headings and loop #### through each system name managed by the HMC to retrieve the power #### subsystem firmware levels. ################################################################ if (( GETPOW == TRUE )) then print "

" ################################################################ #### Gather and report the power subsystem firmware levels for each system #### managed by the HMC using the ssh command to execute the "lslic" command #### on the remote HMC. ################################################################ for SYSNAME in "${NAMES[@]}" do (( VERBOSE == TRUE )) && print -u 2 "# Retrieving power subsystem firmware level for ${SYSNAME}" RESULT=$( ssh ${SSHUSER}@${HMCNAME} lslic -m "${SYSNAME}" -t power ${LSLIC[${HMCNAME}]:- -F activated_level:installed_level:accepted_level} ) (( VERBOSE == TRUE )) && print -u 2 "# Reporting power subsystem firmware level for ${SYSNAME}" print "

" done fi print "
System Name Firmware Type Activated Level Installed Level Accepted Level
${SYSNAME} System ${RESULT%%:*} ${RESULT//*:(*):*/\1} ${RESULT##*:}
System Name Firmware Type Activated Level Installed Level Accepted Level
${SYSNAME} Power Subsystem ${RESULT%%:*} ${RESULT//*:(*):*/\1} ${RESULT##*:}

" done } ################################################################ #### #### This utility is coded as a function and may be used from a library of #### functions or from the command line. #### ################################################################ gethmclic "${@}"