Simple SNMP Trap Testing

Start a non forking snmptrapd daemon accepting all incoming traps and logging them to standard output:
snmptrapd -CdfLo --disableAuthorization=yes
Send a testing trap to the host running snmptrapd daemon (in this example 10.0.0.1):
snmptrap -v 1 -c public 10.0.0.1 .1.3.6.1.6.3 "" 0 0 coldStart.0
Also make sure the host firewall is not blocking incoming UDP/162 traffic.

4 comments:

  1. Exactly what I was looking for. Thank you

    ReplyDelete
  2. Thank you! This did just what I needed

    ReplyDelete
  3. :) Many time talk is cheap ,but little talk worth

    ReplyDelete
  4. Thanks for this. This was the first SNMP trap I successfully sent. As partial payback, here's a script for others who are facing the same problem. it works on RHEL 7.2, I haven't tested anywhere else, but it should work anywhere with net-SNMP mibs installed.

    Dan Baker

    #!/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.
    COMMUNITY='public'
    COLLECTOR_IP='localhost'


    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

    # Now that we have a MIB and a Trap, we can construct an OID
    OID="${MIB}::$TYPE"


    # 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 <-c Community string> ...

    # Drop a log entry just to be nice
    logger -p user.notice "sending snmp trap"

    # send a trap
    case $TYPE in
    linkUp ) snmptrap -v2c -c $COMMUNITY $COLLECTOR_IP '' ${OID} ifIndex i 1 ifAdminStatus i 1 ifOperStatus i 1 ;;
    linkDown) snmptrap -v2c -c $COMMUNITY $COLLECTOR_IP '' ${OID} ifIndex i 1 ifAdminStatus i 2 ifOperStatus i 2;;
    coldStart|warmStart ) snmptrap -v2c -c $COMMUNITY $COLLECTOR_IP '' ${OID} ;;
    * ) echo "Unpossible error did not occur.";;
    esac

    # drop another log to say it's been sent
    logger -p user.notice "${OID} trap sent"

    # pull the log messages to get a time stamp, if needed
    tail /var/log/messages |grep ${OID} |tail -2

    ReplyDelete