#!/usr/bin/sh

#    SAM-QFS_notice_begin
#
#      Solaris 2.x Sun Storage & Archiving Management File System
#
#      Copyright (c) 2006 Sun Microsystems, Inc.
#      All Rights Reserved.
#
#      Government Rights Notice
#      Use, duplication, or disclosure by the U.S. Government is
#      subject to restrictions set forth in the Sun Microsystems,
#      Inc. license agreements and as provided in DFARS 227.7202-1(a)
#      and 227.7202-3(a) (1995), DRAS 252.227-7013(c)(ii) (OCT 1988),
#      FAR 12.212(a)(1995), FAR 52.227-19, or FAR 52.227-14 (ALT III),
#      as applicable.  Sun Microsystems, Inc.
#
#    SAM-QFS_notice_end

#
# sendtrap
# 
# This script gets invoked by the sysevent configuration file.
# This is not expected to be run as a stand-alone program
#
#
# CONFIGURATION PARAMETERS:
# -------------------------
# 1) To modify the trap destination host:
# ------------------------------------
# By default, traps are sent to port 161 of the localhost.
# Inorder to change the port number or the hostname of the
# trap destination, modify the variable TRAP_DESTINATION
# TRAP_DESTINATION="hostname:port"
# This trap destination hostname must be declared in NIS on /etc/hosts
#
# Also traps can be sent to multiple hosts. e.g.
# TRAP_DESTINATION="localhost:161 doodle:163 mgmt_station:1162"
# (delimiter is space)
TRAP_DESTINATION=`hostname`
#
#
# 2) To modify the SNMP community string:
# ---------------------------------------
# By default, the community string is set to 'public'. If you want to
# change the community string, modify the value of the variable COMMUNITY
COMMUNITY="public"
#
#
# -----------------------------------------------------
# -------- Do not change anything below this ----------
# -----------------------------------------------------
TRAP_PROGRAM="/usr/sfw/bin/snmptrap"
#
# The mibs are installed in /var/snmp/mib
MIB_DIR="/var/snmp/mib"
#
#
SNMPLOG="/var/opt/SUNWsamfs/sendtrap.log"
# TapeAlert traps are sent with more information such as library name, vsn etc.
CAT_TAPEALERT="TapeAlert"
#
# This script must be invoked with atleast two arguments (describing the OID)
# in order to attempt delivery of a trap
#
PROGRAM=$0
#
[ $# -lt 2 ] && { echo "`date`:USAGE:$PROGRAM must be invoked with atleast 2 arguments" >> $SNMPLOG; exit 1; }
#
# This script expects positional command line arguments
# No validity is done to check if the contents of the arguments are valid
# The arguments are used to create the OID. 
#
CATEGORY=$1
SUBCATEGORY=$2
MIBOID=sunSam$CATEGORY$SUBCATEGORY
#
#
# Initialize integer values otherwise snmptrap
# will fail (if no args received)
#
ERRORTYPE=0
ERRORID=0
#
#
[ $# -gt 2 ] && ERRORTYPE=$3
[ $# -gt 3 ] && ERRORID=$4
#
#
SYSTEMID=$5
MSG=$6
FAULTTIME=$7
#
# parse the list of trap destinations (they are separated by spaces)
for dest in $TRAP_DESTINATION
do
#
echo "`date`:$PROGRAM[$$]:Sending trap $MIBOID \n" >> $SNMPLOG 
#
#
#	If a trap for a tapealert is to be thrown,
#	then the data that is to be sent as a trap
#	is specific to the tapealert
#
	if test "$CATEGORY" = "$CAT_TAPEALERT"
	then
		# To send a tapealert trap with complete
		# information, vendorid, productid, revision,
		# name, vsn and probable cause has to be
		# provided

		TAPEVENDORID=$8
		TAPEPRODUCTID=$9
		[ $# -gt 0 ] && shift
		TAPEREV=$9
		[ $# -gt 0 ] && shift
		TAPENAME=$9
		[ $# -gt 0 ] && shift
		TAPEVSN=$9
		[ $# -gt 0 ] && shift
		PROBABLECAUSE=$9


        	$TRAP_PROGRAM \
		-M $MIB_DIR \
		-m $MIB_DIR/SUN-SAM-MIB.mib \
	        -v 2c -c $COMMUNITY $dest 1 \
		SUN-SAM-MIB::$MIBOID \
		SUN-SAM-MIB::sunSamComponentID s "$CATEGORY" \
		SUN-SAM-MIB::sunSamErrorType i "$ERRORTYPE" \
		SUN-SAM-MIB::sunSamTapeAlertID s "$ERRORID" \
		SUN-SAM-MIB::sunSamSystemID s "$SYSTEMID" \
		SUN-SAM-MIB::sunSamMsg s "$MSG" \
		SUN-SAM-MIB::sunSamFaultTime s "$FAULTTIME" \
		SUN-SAM-MIB::sunSamTapeVendorID s "$TAPEVENDORID" \
		SUN-SAM-MIB::sunSamTapeProductID s "$TAPEPRODUCTID" \
		SUN-SAM-MIB::sunSamTapeRev s "$TAPEREV" \
		SUN-SAM-MIB::sunSamTapeName s "$TAPENAME" \
		SUN-SAM-MIB::sunSamTapeVsn s "$TAPEVSN" \
		SUN-SAM-MIB::sunSamTapeAlertProbableCause s "$PROBABLECAUSE" \
		>> $SNMPLOG 2>&1

		[ $? != 0 ] && { echo "`date`:$PROGRAM[$$]:Failed to send trap $MIBOID" >> $SNMPLOG; exit 1; }

	else
		$TRAP_PROGRAM \
		-M $MIB_DIR \
		-m $MIB_DIR/SUN-SAM-MIB.mib \
                -v 2c -c $COMMUNITY $dest 1 \
                SUN-SAM-MIB::$MIBOID \
                SUN-SAM-MIB::sunSamComponentID s "$CATEGORY" \
		SUN-SAM-MIB::sunSamErrorType i "$ERRORTYPE" \
		SUN-SAM-MIB::sunSamErrorID i "$ERRORID" \
		SUN-SAM-MIB::sunSamSystemID s "$SYSTEMID" \
		SUN-SAM-MIB::sunSamMsg s "$MSG" \
		SUN-SAM-MIB::sunSamFaultTime s "$FAULTTIME" \
		>> $SNMPLOG 2>&1

		[ $? != 0 ] && { echo "`date`:$PROGRAM[$$]:Failed to send trap $MIBOID" >> $SNMPLOG; exit 1; }
	
	fi
done
	exit 0;
