#!/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
USER_ID=$5

# If doing a login trap, then wait 3 secs.  This avoids
# traps being sent out of order if a login trap immediately
# follows a logout trap.
if [ "$GENERICID" = "6" -a "$SPECIFICID" = "1" ]; then
        # Doing login trap.  Wait 3 secs.
        sleep 2
fi

# Wait for other gentraps to finish (give it 20 sec)
WAIT_COUNT=0
while [ -f /tmp/snmp/traplock -a $WAIT_COUNT -lt 20 ]
do
	echo "Waiting for trap lock file to free up"      
        sleep 1
        let WAIT_COUNT+=1
done
touch /tmp/snmp/traplock 

TRAPLOG=/var/log/snmptrap.log

# Check if snmpd is running before doing anything
SNMP_RUNNING=`/etc/rc.d/init.d/snmpd status | grep "running"`
if [ -z "$SNMP_RUNNING" ]; then              
   echo -e "snmpd is not running.\r" > $TRAPLOG 2>&1
   # Release the lock file
   rm -f /tmp/snmp/traplock
   exit 1
fi

# load environment variables
. /etc/rc.d/rc.IBM_NS.env

HOSTNAME=`hostname`

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

trapmask=`sed -n '/^authtrapenable/p' /usr/local/lib/snmp/snmpd.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
        # Release the lock file
        rm -f /tmp/snmp/traplock
        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
                # Release the lock file
                rm -f /tmp/snmp/traplock		
		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
   # Release the lock file
   rm -f /tmp/snmp/traplock
   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
  # Release the lock file
  rm -f /tmp/snmp/traplock
  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

# List of oids to be passed to snmpget
snmpOidList="$mib2OID.1.0 $tcMibOID.58.0 $tcMibOID.73.0 $tcMibOID.68.0 $tcMibOID.51.0 $tcMibOID.85.0 $tcMibOID.99.0 $tcMibOID.100.0 $tcMibOID.112.0 $tcMibOID.119.0 $tcMibOID.113.0"

# Call snmpget and fill in the vars with the oids returned
snmpget -Oq localhost $readCommunityName $snmpOidList 2>/dev/null | awk "{printf(\"export snmpVal%d='%s'\n\",NR,\$0)}" > /tmp/snmp/oidVals

. /tmp/snmp/oidVals

# get system description
sysdescValue=`echo $snmpVal1 | cut -d' ' -f2- | sed -n '1p' | 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=`echo $snmpVal2 | 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
if [ -n "$MACADDRESS" ]; then
        macaddressValue=$MACADDRESS
else
        /IBM/bin/readNvram 74
        macaddressValue=`cat /tmp/snmp/74`
fi
if [ -n "$macaddressValue" ]; then
	macaddress="$tcMibOID.74.0 s '$macaddressValue'"
else
	macaddress=
fi

if [ -n "$USER_ID" ]; then
	loggedInUserValue=$USER_ID
else
        loggedInUserValue=`cat /tmp/snmp/user`
fi                        

if [ -n "$loggedInUserValue" ]; then
	loggedInUser="$tcMibOID.70.0 s '$loggedInUserValue'"
else
	loggedInUser=
fi

# going to have to use snmpget to retrieve this
mibVersionValue=`echo $snmpVal3 | cut -d' ' -f2- | sed 's/\"//g'`


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

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=`echo $snmpVal4 | cut -d' ' -f2- | sed 's/\"//g'`
if [ -n "$serviceUpdateValue" ]; then
	serviceUpdate="$tcMibOID.68.0 i '$serviceUpdateValue'"
else
	serviceUpdate=
fi


nsBootVersionValue=`echo $snmpVal5 | cut -d' ' -f2- | sed -n 's/\([(/)]\)/\\\\\1/pg' | sed 's/\"//g'`
if [ -n "$nsBootVersionValue" ]; then
        nsBootVersion="$tcMibOID.51.0 s \'$nsBootVersionValue\'"
else
        nsBootVersion=
fi

bootSourceValue=`echo $snmpVal6 | cut -d' ' -f2- | sed 's/\"//g'`
if [ -n "$bootSourceValue" ]; then
        bootSource="$tcMibOID.85.0 i $bootSourceValue"
else
        bootSource=
fi

# 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=
if [ -f /usr/local/lib/snmp/snmpd.local.conf ]; then 
        aliasValue=`sed -n '/^aliasname/p' /usr/local/lib/snmp/snmpd.local.conf | cut -d' ' -f2- | sed "s/'//g"`
fi

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

bootModeValue=`echo $snmpVal7 | cut -d' ' -f2- | sed 's/\"//g'`
if [ -n "$bootModeValue" ]; then
        bootMode="$tcMibOID.99.0 i $bootModeValue"
else
        bootMode=
fi

configModeValue=`echo $snmpVal8 | cut -d' ' -f2- | sed 's/\"//g'`
if [ -n "$configModeValue" ]; then
        configMode="$tcMibOID.100.0 i $configModeValue"
else
        configMode=
fi

currentConfigModeValue=`echo $snmpVal9 | cut -d' ' -f2- | sed 's/\"//g'`
if [ -n "$currentConfigModeValue" ]; then
        currentConfigMode="$tcMibOID.112.0 i $currentConfigModeValue"
else
        currentConfigMode=
fi

memSizeValue=`echo $snmpVal10 | cut -d' ' -f2- | sed 's/\"//g'`
if [ -n "$memSizeValue" ]; then
        memSize="$tcMibOID.119.0 i $memSizeValue"
else
        memSize=
fi


bootURLValue=`echo $snmpVal11 | cut -d' ' -f2- | sed -n 's/\([(/)]\)/\\\\\1/pg'  | sed 's/\"//g'`
if [ -n "$bootURLValue" ]; then
        bootURL="$tcMibOID.113.0 s \'$bootURLValue\'"
else
        bootURL=
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 $flashCardSize $distribution $nsBootVersion $bootSource $serviceUpdate $alias $bootURL $memSize $bootMode $configMode $currentConfigMode $TASK_OID/p" /usr/local/lib/snmp/snmpd.conf > /tmp/runtrap.sh

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

# Release the lock file
rm -f /tmp/snmp/traplock

# 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
