#!/bin/sh -f

# This Bourne Shell script polls the Broadcom BASP information from
# the /proc/net/basp file to extract BASP NIC events and send the
# corresponding SNMP trap events to the registered SNMP trap managers.

PATH=$path:/bin:/usr/bin:/usr/ucb

PLACE=".1.3.6.1.4.1.4413.1.2"
REQ=$2
OPTSTR="$1"

if test "$OPTSTR" = "-h"; then
  echo "Usage: genBaspTraps -h (help - print this message) | -d (delay) <delaytime - delaytime in seconds>"
  exit 0
elif test "$OPTSTR" = "-d"; then
   if test "$REQ" = ""; then
      DELAYTIME=2
   elif [[ $REQ  > 999999 ]]; then
     echo "Inavlid delay time $REQ. Delay must be between 1 to 999999"
     exit 0
   else
     DELAYTIME=$REQ
   fi
elif test "$OPTSTR" = ""; then
   DELAYTIME=2
else
   echo "Unrecognized option $OPTSTR"
  echo "Usage: genBaspTraps -h (help - print this message) | -d (delay) <delaytime - delaytime in seconds>"
   exit 0
fi

echo "Using delaytime = $DELAYTIME"
if ! test -f "/proc/net/basp/trap"; then
   echo "BASP Trap file is not present"
   exit 0
fi

if [ -f /usr/share/snmp/snmpd.conf ]; then
  TRAPHOST=`awk '/^trapsink/ { print $2 }' /usr/share/snmp/snmpd.conf`
elif [ -f /etc/snmp/snmpd.conf ]; then
  TRAPHOST=`awk '/^trapsink/ { print $2 }' /etc/snmp/snmpd.conf`
fi

if test "$TRAPHOST" = "";then
  TRAPHOSTS[1]=localhost
  NUMHOSTS=1
else
  NUMHOSTS=`echo $TRAPHOST | awk 'BEGIN { FS = " " } { print NF }'`
  TEMPVAL=`echo $NUMHOSTS`
  while expr $TEMPVAL > 0; do
    TRAPHOSTS[$TEMPVAL]=`echo $TRAPHOST | awk 'BEGIN { FS = " " } { print $'$TEMPVAL' }'`
    TEMPVAL=`expr $TEMPVAL - 1`
  done
fi

while true; 
do
  TRAP_CONT=`cat /proc/net/basp/trap`

  if [ "$TRAP_CONT" = "" ]; then
    sleep $DELAYTIME
    continue
  fi

  j=1
  for i in $TRAP_CONT
  do
    case "$j" in
      1)      TRAP_CAUSE_DIR=$i;; 
      2)      TEAM_ID=$i;;
      3)      TRAP_PHY_INDEX=$i;;
      6)      TRAP_ADAPTER_ACT_CAUSE=$i;;
      *)     
    esac
    let j=$j+1
  done

  # Make it corresponding to the SNMP-MIB file decl.	
  let TRAP_CAUSE_DIR=$TRAP_CAUSE_DIR+1

  # Make it corresponding to the SNMP-MIB file decl.	
  let TRAP_ADAPTER_ACT_CAUSE=TRAP_ADAPTER_ACT_CAUSE+2

  if [ $TRAP_CAUSE_DIR -ne 1 -a $TRAP_CAUSE_DIR -ne 2 ]; then
    TRAP_ADAPTER_ACT_CAUSE=1
  fi

  NUMFILES=`ls /proc/net/basp`
  
  for FILE in $NUMFILES
  do
    if [ $FILE = 'trap' ]; then
      continue
    fi

    if [ $FILE -ne $TEAM_ID ]; then
      echo NO matched file: $FILE
      continue
    fi

    NUMPHYS=`grep "physical" /proc/net/basp/$FILE | awk '{ print $6 }'`

    PHY=1
    while [ $PHY -le $NUMPHYS ]
    do
      NICINDEX=`grep -A $PHY "physical" /proc/net/basp/$FILE | grep '#' -v | awk '{ for (i=0;i<'$PHY'-1;i++) getline } { print $1 }'`

      if [ $NICINDEX = $TRAP_PHY_INDEX ]; then
        PHY_NAME=`grep -A $PHY "physical" /proc/net/basp/$FILE | grep '#' -v | awk '{ for (i=0;i<'$PHY'-1;i++) getline } { print $2 }'`

        TEAM_NAME=`grep  "name" /proc/net/basp/$FILE | awk '{ print $3 }'`

  	if test $NUMHOSTS > 0; then
    	  TEMPVAL1=$NUMHOSTS
    	  while expr $TEMPVAL1 > 0; do
            echo "Broadcom BASP NIC event: Sending SNMP trap to ${TRAPHOSTS[$TEMPVAL1]}."
            snmptrap -c public ${TRAPHOSTS[$TEMPVAL1]} Brcm-BSAPTrap-MIB::baspTrap localhost 6 1 ''\
            Brcm-BSAPTrap-MIB::trapAdapterName s $TEAM_NAME\
            Brcm-BSAPTrap-MIB::trapTeamName s $PHY_NAME\
            Brcm-BSAPTrap-MIB::trapCauseDirection i $TRAP_CAUSE_DIR\
            Brcm-BSAPTrap-MIB::trapAdapterActivityCause i $TRAP_ADAPTER_ACT_CAUSE
            TEMPVAL1=`expr $TEMPVAL1 - 1`
          done
        fi
        break
      fi
      let PHY=$PHY+1
    done  # check with all physical NICs.
  done	  # check all files

  sleep $DELAYTIME 
done



