#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lib/nim/methods/bladeOps.sh 1.2 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2005 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)78 1.2 src/bos/usr/lib/nim/methods/bladeOps.sh, cmdnim, bos720 9/26/05 15:38:33

NIMPATH=/usr/lpp/bos.sysmgt/nim
NIM_METHODS="${NIMPATH}/methods"

export NIMPATH NIM_METHODS

# "dot" include (source) the shell lib functions & snmp host definition
. ${NIM_METHODS}/c_sh_lib

# --------------------------- module globals   
REQUIRED_ATTRS="operation host"
OPTIONAL_ATTRS="all name slot power media kvm bootlist bootdevice1 bootdevice2 bootdevice3 macaddr uuid fru serial type id file date community"

rc=0
targetHost=""
dataStructure=""
MIBS="/usr/samples/nim/snmp/mm_mib.defs"
CLSNMP="/usr/sbin/clsnmp -v"
# ---------------------------

while getopts :a:o:qv c
do
	case ${c} in

		a)		# validate the attr ass
				# - set operation requires value
				if [[ $operation = "set" || "${OPTARG}" = +(?)=+(?) ]]; then
					parse_attr_ass "${OPTARG}"
					
					# include the assignment for use in this environment
					eval ${variable}=\"${value}\"
				
				else
					eval ${OPTARG}=1
				fi
		;;

		o)		# operation to perform
				operation="$OPTARG"
				if [[ $operation != "dump" ]]; then
					REQUIRED_ATTRS="operation host slot"
				fi
		;;

		q)		# show attr info
				cmd_what
				exit 0
		;;

		v)		# verbose mode (for debugging)
				set -x
				for i in $(typeset +f)
				do
					typeset -ft $i
				done
		;;

		\?)		# unknown option
				print "Usage bladeOps:  Perform hardware management operations against IBM BladeCenter"
				print "        bladeOps -o get -a host=<winSnmpName> -a slot=<slot#>"
				print "                [-a community=<community_name>]"
				print "                [-a file=<ConfigurationFile>]"
				print "                [-a all] [-a bootlist] [-a macaddr]"
				print "                [-a power] [-a media] [-a kvm]"
				print "                [-a uid] [-a fru] [-a serial]"
				print "                [-a type] [-a id] [-a date] [-v]"
				print ""
				print "        bladeOps -o set -a host=<winSnmpName> -a slot=<slot#>"
				print "                [-a community=<community_name>]"
				print "                [-a file=<ConfigurationFile>]"
				print "                [-a bootdevice1=<0|1|2|3|4|5|6|7>]"
				print "                [-a bootdevice2=<0|1|2|3|4|5|6|7>]"
				print "                [-a bootdevice3=<0|1|2|3|4|5|6|7>]"
				print "                [-a power=<0|1>] [-a media=<0|1>]"
				print "                [-a kvm=<0|1>] [-v]"
				print ""
				print "        bladeOps -o dump -a host=<winSnmpName> [-v]"
				print ""
				print "        bladeOps -q"
				print ""
				print "boot values:"
				print "0-none 1-floppy 2-cdrom 3-hdisk0 4-hdisk1 5-hdisk2 6-hdisk3 7-network"
				print ""
				print "Note:"
				print "The values for community_name and WinSnmpName should be"
				print "defined in /etc/clsnmp.conf or using the file attribute."
				print ""
				print "Files:"
				print "/etc/clsnmp.conf Configuration file for the clsnmp command."
				print "/etc/mib.defs Defines the Management Information Base (MIB)"
				print "variables the SNMP agent and manager should recognize and handle."
				print ""
				exit 1
		;;
	esac
done

# check for missing attrs
ck_attrs

# get blade info from each tree prefix
if [[ $operation = "dump" ]]; then

  for i in \
	bladeName bladeViewMID bladeViewFRUNum bladeViewSerial bladeViewType bladeViewUUID bladeViewMDate \
	bladeViewMAC1 bladeViewMAC2 bladeBootDevice1 bladeBootDevice2 bladeBootDevice3 bladeStatusPower \
	bladeStatusKVM bladeStatusMedia 
  do
	$CLSNMP -f ${file:-/etc/clsnmp.conf} ${community:+-c $community} -h $host walk $i
	let rc=$rc+$?
  done


# get blade info for each slot specified
elif [[ $operation = "get" ]]; then

  for i in ${slot}
  do
	# check for wildcard
	if [[ $all = 1 ]]; then
		power=1;media=1;kvm=1;bootlist=1;macaddr=1;uuid=1;fru=1;serial=1;type=1;id=1;date=1
	fi

	# create data query structure
	dataStructure="${name:+bladeName.$i} \
	${id:+bladeViewMID.$i} ${fru:+bladeViewFRUNum.$i} ${serial:+bladeViewSerial.$i} \
	${type:+bladeViewType.$i} ${uuid:+bladeViewUUID.$i} ${date:+bladeViewMDate.$i} \
	${macaddr:+bladeViewMAC1.$i bladeViewMAC2.$i} \
	${bootlist:+bladeBootDevice1.$i bladeBootDevice2.$i bladeBootDevice3.$i} \
	${power:+bladeStatusPower.$i} ${kvm:+bladeStatusKVM.$i} ${media:+bladeStatusMedia.$i}"

	# make snmp call using oids from mib files
	output=`$CLSNMP -f ${file:-/etc/clsnmp.conf} ${community:+-c $community} -h $host get $dataStructure \
	| $AWK -F" = " '{pos=$2; gsub(":","", pos);print pos}'`
	let rc=$rc+$?

	# colon format snmp output
	colon="slot[$i]:"
	for j in ${output}
	do
		value=`print "$j:\c"`
		colon="${colon}${value}"
	done
	
	#create header w/ colon format
	hdrformat="#slot:${name:+"name:"}${id:+"id:"}${fru:+"fru:"}${serial:+"serial:"}${type:+"type:"}${uuid:+"uuid:"}${date:+"date:"}${macaddr:+"mac0:"}${macaddr:+"mac1:"}${bootlist:+"bootdevice1:"}${bootlist:+"bootdevice2:"}${bootlist:+"bootdevice3:"}${power:+"power:"}${kvm:+"kvm:"}${media:+"media:"}"

	#print snmp data
	print $hdrformat
	print $colon
  done


# set blade info for each slot specified
elif [[ $operation = "set" ]]; then

  for i in ${slot}
  do
	dataStructure="$dataStructure \
	${bootdevice1:+"bladeBootDevice1.$i integer $bootdevice1"} \
	${bootdevice2:+"bladeBootDevice2.$i integer $bootdevice2"} \
	${bootdevice3:+"bladeBootDevice3.$i integer $bootdevice3"} \
	${power:+"bladeChangePower.$i integer $power"} \
	${kvm:+"bladeChangeKVM.$i integer $kvm"} \
	${media:+"bladeChangeMedia.$i integer $media"}"
  done

	# make snmp call using oids from mib files
	$CLSNMP -f ${file:-/etc/clsnmp.conf} ${community:+-c $community} -h $host set $dataStructure
	rc=$?


# incorrect operation specified
else
	print "error:  Undefined operation."
	rc=1
fi

exit $rc
