#!/bin/sh
# IBM NetVista Thin Client Manager - trap generator
# v1.0 by peter van wazer 9.22.00

export PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/local/sbin

GENERICID=$1
SPECIFICID=$2
SENDINGFILE=$3
TASK_ID=$4

TRAPLOG=/var/log/snmptrap.log

HOSTNAME=`hostname`

if [ ! -f /usr/local/lib/snmp/snmpd.local.conf ]; then
   echo -e "Trap config file /usr/local/lib/snmp/snmpd.local.conf does not exist.\r" > $TRAPLOG 2>&1
   exit 1
fi

trapmask=`sed -n '/^authtrapenable/p' /usr/local/lib/snmp/snmpd.local.conf | awk {'print $2'}`

# check deftrap to see if this trap is turned on then exit or get trap name
if [ ! `look "$GENERICID $SPECIFICID" /IBM/bin/deftrap | cut -d' ' -f3` ]
then
        echo -e "snmptrap not active, exiting\r" > $TRAPLOG 2>&1
        exit 1
fi

# get the trap name
trapname=`look "$GENERICID $SPECIFICID" /IBM/bin/deftrap | cut -d' ' -f4-`

# check the trap mask, if it is set
if [ "$trapmask" ]; then
	bit=`/IBM/bin/bitSet $trapmask $1`;

	if [ $bit = "off" ]; then
		# trap not in trap mask
    echo -e "Trap is not in the trap mask.\r" > $TRAPLOG 2>&1		
		exit 1
	fi
fi

# parse /usr/local/share/snmp/snmpd.conf and snmpd.local.conf
if [ -f /usr/local/share/snmp/snmpd.local.conf ]; then
   SNMPDCONF=/usr/local/share/snmp/snmpd.local.conf
elif [ -f /usr/local/share/snmp/snmpd.conf ]; then
   SNMPDCONF=/usr/local/share/snmp/snmpd.conf
else
   echo -e "No snmp config file... could not determine read community name.\r" > $TRAPLOG 2>&1
   exit 1
fi

# check for com2sec, rocommunity and rwcommunity names
# then take the first one since it should matter which one we use
communityList=`sed -n '/^com2sec/p' $SNMPDCONF | awk '{print $4}'`
if [ ! "$communityList" ]; then
  communityList=`sed -n '/^r[wo]community/p' $SNMPDCONF | awk '{print $2}'`
fi

if [ "$communityList" ]; then
   set $communityList
   readCommunityName=$1
else
  echo -e "Could not determine snmp read community name.\r" > $TRAPLOG 2>&1
  exit 1
fi
# echo $readCommunityName

# get <oid> <datatype> <value> from localhost for oid's to append to all traps
tcMibOID=".1.3.6.1.4.1.2.6.169.2"
mib2OID=".1.3.6.1.2.1.1"

# getting an environment variable is faster, perhaps else should try other way to get hostname
if [ -n "$HOSTNAME" ]; then
	sysname="$mib2OID.5.0 s '$HOSTNAME'"
else
	sysName=
fi

# get system description
# sysdesc=`snmpget localhost $readCommunityName $mib2OID.1.0 | sed 's/ = / s /'`
#if ! echo $sysdesc | grep \"Linux  > /dev/null 2>&1
#then
#	tmp=`echo $sysdesc | sed -n 's/Linux/\"Linux/p'`\"
#	sysdesc=$tmp
#fi
sysdescValue=`snmpget -Oq localhost $readCommunityName $mib2OID.1.0 2>/dev/null | cut -d' ' -f2- | sed 's/\"//g'`
if [ -n "$sysdescValue" ]; then
	sysdesc="$mib2OID.1.0 s '$sysdescValue'"
else
	sysdesc=
fi

# if there is a fourth parameter, it is the tcm task id
if [ -n "$TASK_ID" ]; then
	TASK_OID="$tcMibOID.71.0 s '$TASK_ID'"
else
	TASK_OID=
fi

# productModelValue=`snmpget localhost $readCommunityName $tcMibOID.58.0 | awk '{print $3}'`
productModelValue=`snmpget -Oq localhost $readCommunityName $tcMibOID.58.0 2>/dev/null | cut -d' ' -f2- | sed 's/\"//g'`

if [ -n "$productModelValue" ]; then
	productModel="$tcMibOID.58.0 s '$productModelValue'"
else
	productModel=
fi

/IBM/bin/readNvram 76
sysModelIDValue=`cat /tmp/snmp/76`
if [ -n "$sysModelIDValue" ]; then
	sysModelID="$tcMibOID.76.0 s '$sysModelIDValue'"
else
	sysModelID=
fi

# get mac address
/IBM/bin/readNvram 74
macaddressValue=`cat /tmp/snmp/74`
if [ -n "$macaddressValue" ]; then
	macaddress="$tcMibOID.74.0 s '$macaddressValue'"
else
	macaddress=
fi
# macaddress="$tcMibOID.74.0 s `cat /tmp/snmp/74`"

/IBM/bin/getUser
loggedInUserValue=`cat /tmp/snmp/70`
if [ -n "$loggedInUserValue" ]; then
	loggedInUser="$tcMibOID.70.0 s '$loggedInUserValue'"
else
	loggedInUser=
fi
# loggedInUser="$tcMibOID.70.0 s `cat /tmp/snmp/70`"

# going to have to use snmpget to retrieve this
# mibVersionValue=`snmpget localhost $readCommunityName $tcMibOID.73.0 | awk '{print $3}'`
mibVersionValue=`snmpget -Oq localhost $readCommunityName $tcMibOID.73.0 2>/dev/null | cut -d' ' -f2- | sed 's/\"//g'`


if [ -n "$mibVersionValue" ]; then
	mibVersion="$tcMibOID.73.0 s '$mibVersionValue'"
else
	mibVersion=
fi

/IBM/bin/readNvram 33
localPriorityValue=`cat /tmp/snmp/33`
if [ -n "$localPriorityValue" ]; then
	localPriority="$tcMibOID.33.0 i $localPriorityValue"
else
        localPriority=
fi
# localPriority="$tcMibOID.33.0 i `cat /tmp/snmp/33`"

if /IBM/bin/cfInfo
then
   flashCardSize="$tcMibOID.44.0 i `cat /tmp/snmp/44`"
else
   flashCardSize=
fi

if [ -f /IBM/linuxDistribution ];
then
   distribution="$tcMibOID.72.0 s \"`cat /IBM/linuxDistribution | grep Version`\""
else
   distribution=
fi

serviceUpdateValue=`snmpget -Oq localhost $readCommunityName $tcMibOID.68.0 2>/dev/null | cut -d' ' -f2- | sed 's/\"//g'`

if [ -n "$serviceUpdateValue" ]; then
	serviceUpdate="$tcMibOID.68.0 i '$serviceUpdateValue'"
else
	serviceUpdate=
fi


nsBootVersionValue=`snmpget -Oq localhost $readCommunityName $tcMibOID.51.0 2>/dev/null | cut -d' ' -f2- | sed -n 's/\([(/)]\)/\\\\\1/pg' | sed 's/\"//g'`

if [ -n "$nsBootVersionValue" ]; then
        nsBootVersion="$tcMibOID.51.0 s \'$nsBootVersionValue\'"
else
        nsBootVersion=
fi

/IBM/bin/readNvram 32
nvramPriorityValue=`cat /tmp/snmp/32`
if [ -n "$nvramPriorityValue" ]; then
        nvramPriority="$tcMibOID.32.0 i $nvramPriorityValue"
else
        nvramPriority=
fi

/IBM/bin/readNvram 31
dhcpPriorityValue=`cat /tmp/snmp/31`
if [ -n "$dhcpPriorityValue" ]; then
        dhcpPriority="$tcMibOID.31.0 i $dhcpPriorityValue"
else
        dhcpPriority=
fi

/IBM/bin/readNvram 79
flashBootProtocolValue=`cat /tmp/snmp/79`
if [ -n "$flashBootProtocolValue" ]; then
        flashBootProtocol="$tcMibOID.84.0 i $flashBootProtocolValue"
else
        flashBootProtocol=
fi


bootSourceValue=`snmpget -Oq localhost $readCommunityName $tcMibOID.85.0 2>/dev/null | cut -d' ' -f2- | sed 's/\"//g'`

# This is the old way to determine boot source.
#grep "root=nfs" /proc/cmdline > /dev/null
#bootSourceValue=`echo $?`

if [ -n "$bootSourceValue" ]; then
        bootSource="$tcMibOID.85.0 i $bootSourceValue"
else
        bootSource=
fi

# CEC - The sed command strips leading and trailing " (added by snmpget) and removes
# ' (causes errors in runtrap.sh)
#aliasValue=`snmpget -Oq localhost $readCommunityName $tcMibOID.95.0 2>/dev/null | cut -d' ' -f2- | sed -e  's/^\"//g' -e 's/\"$//g' -e "s/'//g" `

# CEC - get the alias from the file, not from snmpget.  This allows extended ascii
# chars to be in the alias (chars > 128, for example the umlaut).  snmpget dumps out
# hex when it sees extended ascii.  Also, remove any ' chars (causes errors in runtrap.sh).
aliasValue=`sed -n '/^aliasname/p' /usr/local/lib/snmp/snmpd.local.conf | cut -d' ' -f2- | sed "s/'//g"`

if [ -n "$aliasValue" ]; then
        alias="$tcMibOID.95.0 s \'$aliasValue\'"
else
        alias=
fi

# write new runtrap file containing all trap commands
sed -n "s/trapsink \([^ ]*\) \([^ ]*\) \([^ ]*\)/snmptrap -v 1 -M \/usr\/local\/share\/snmp\/mibs -p \3 \1 \2 '' '' $GENERICID $SPECIFICID '' $sysname $sysdesc $sysModelID $macaddress $productModel $loggedInUser $mibVersion $localPriority $flashCardSize $distribution $nsBootVersion $nvramPriority $dhcpPriority $flashBootProtocol $bootSource $serviceUpdate $alias $TASK_OID/p" /usr/local/lib/snmp/snmpd.local.conf > /tmp/runtrap.sh

# run the runtrap file to send all the traps
chmod 700 /tmp/runtrap.sh
/tmp/runtrap.sh 2>/dev/null

# record the trap sent
echo -e "sending $trapname snmp trap from $SENDINGFILE \r" > $TRAPLOG 2>&1

# could check the exit code of runtrap.sh and record message
# if [ ! $?=0 ]
# then
#	echo "runtrap exit status = $?"
# fi
