It's been necessary from time to time for me to send a test SNMP trap to test host monitoring, fault management and alerting systems. It's always been an ad hoc affair, full of errors, as it seems it is for a lot of Admins.
This is an occasional, but common need. SNMP is complex enough that it's not simple to generate a test trap, particularly if you have no background. Even worse, all of the websites that discuss the matter want to teach you SNMP, when all you want is the command to send a test trap to someone who cares.
Tired of not understanding the process, I invested some time and effort the latest time I needed to conduct this sort of test. Then, I scripted it so I never have to get that stuff on my hands again.
The below is tested on RedHat Linux 7.2, but should work on any Linux host with net-SNMP installed. It knows how to send a linkUp linkDown, warmStart and coldStart trap.
#!/bin/bash
# this script just sends a test snmp trap. It uses a real trap,
# so don't use it with automated systems unless you can handle
# the alarm when it comes in
# User details
# I enclose the community string in single quotes because they
# often contain characters the shell considers special.
COLLECTOR_IP='localhost'
COMMUNITY='public'
HELP_MSG="This script sends a simple snmp trap.
you MUST edit it to set the correct destination
and Community string. It's too tricky handling
possible special characters to attempt that here.
By default it goes to localhost with the default
"public" string. This will work if you first run
a local trap collector like this:
snmptrapd -CdfLo --disableAuthorization="yes"
Otherwise;
USAGE: $0 [linkUp|linkDown|coldStart|warmStart]"
# parse the parameters to get a TYPE
# default TYPE to linkup
case $1 in
up|linkup|linkUp|ifup ) MIB="IF-MIB" ; TYPE="linkUp";;
down|linkdown|linkDown|ifdown ) MIB="IF-MIB" ; TYPE="linkDown" ;;
cold|coldstart|coldStart ) MIB=SNMPv2-MIB ; TYPE="coldStart" ;;
warm|warmstart|warmStart ) MIB=SNMPv2-MIB ; TYPE="warmStart";;
help ) echo $HELP_MSG ; exit 0;;
* ) echo "No valid type given, sending linkUp trap"
echo "For usage, try $0 help"
MIB="IF-MIB" ; TYPE="linkUp";;
esac
# Drop a log entry just to be nice
logger -p user.notice "sending ${MIB}::$TYPE snmp trap"
# Specific to net-SNMP and IF-MIB::linkUp|linkDown
# Values: type meaning:
# ifIndex i interface number starting from 1, not zero. 2=eth1
# ifAdminStatus i 1=up 2=down
# ifOperStatus i 1=up 2=down
#syntax snmptrap <v> <-c Community string> <uptime, defaults to system uptime> <OID><OID_var_1 type value>...<OID_var_n type value>
echo MIB $MIB
echo TYPE $TYPE
echo OID ${MIB}::$TYPE
case $TYPE in
linkUp ) snmptrap -v2c -c $COMMUNITY $COLLECTOR_IP '' ${MIB}::$TYPE ifIndex i 1 ifAdminStatus i 1 ifOperStatus i 1 ;;
linkDown) snmptrap -v2c -c $COMMUNITY $COLLECTOR_IP '' ${MIB}::$TYPE ifIndex i 1 ifAdminStatus i 2 ifOperStatus i 2;;
coldStart|warmStart ) snmptrap -v2c -c $COMMUNITY $COLLECTOR_IP '' ${MIB}::$TYPE ;;
* ) echo "Unpossible error did not occur.";;
esac
# drop another log to say it's been sent
logger -p user.notice "${MIB}::$TYPE trap sent"
# pull the log messages to get a time stamp, if needed
tail /var/log/messages |grep ${MIB}::$TYPE |tail -2
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment