NAME
SNMP::Trapinfo - Reading an SNMP trap from Net-SNMP's snmptrapd
SYNOPSIS
use SNMP::Trapinfo;
$trap = SNMP::Trapinfo->new(*STDIN);
print "From host: ", $trap->hostname, $/;
print "Trapname: ", $trap->trapname, $/;
print "Packet: ", $trap->packet, $/;
print "Message: ", $trap->expand('Interface ${V5} is down'), $/;
DESCRIPTION
This module allows the user to get to the useful parts of an snmptrapd packet. This assumes that you are using the Net-SNMP package from http://www.net-snmp.org and that you are letting snmptrapd do all the OID translations (no -On option for snmptrapd) and that hostnames are resolved itself (no -n option).
The most useful method is expand, which evaluates macros based on the packet, for your custom messages.
METHODS
- SNMP::Trapinfo->new(*STDIN)
-
Reads STDIN, expecting input from snmptrapd, and returns the object holding all the information about this packet. An example packet is:
cisco2611.lon.altinity 192.168.10.20 SNMPv2-MIB::sysUpTime.0 9:16:47:53.80 SNMPv2-MIB::snmpTrapOID.0 IF-MIB::linkUp.1 IF-MIB::ifIndex.2 2 IF-MIB::ifDescr.2 Serial0/0 IF-MIB::ifType.2 ppp SNMPv2-SMI::enterprises.9.2.2.1.1.20.2 "PPP LCP Open" SNMP-COMMUNITY-MIB::snmpTrapAddress.0 192.168.10.20 SNMP-COMMUNITY-MIB::snmpTrapCommunity.0 "public" SNMPv2-MIB::snmpTrapEnterprise.0 SNMPv2-SMI::enterprises.9.1.186
- SNMP::Trapinfo->new(\$data)
-
Instead of a filehandle, can specify a scalar reference that holds the packet data.
- hostname
-
Returns the first line of the packet, which should be the hostname as resolved by snmptrapd.
- hostip
-
Returns the IP address in the 2nd line of the packet, which should be the originating host.
- trapname
-
Returns the value of the parameter SNMPv2-MIB::snmpTrapOID. In the example above, this method would return IF-MIB::linkUp. The .1 is removed, but if it was .1.1, this would not be removed.
- fully_translated
-
Returns 0 if the trapname has more than 1 set of trailing digits (a single .\d+ would be removed automatically) - this would mean that a MIB is missing. Otherwise returns 1.
- data
-
Returns a hash ref where the keys consist of the SNMP parameter and the values are the string values of thos parameters. For the example trap above, a Data::Dumper of $trap->data would give:
$VAR1 = { 'SNMPv2-MIB::snmpTrapEnterprise' => 'SNMPv2-SMI::enterprises.9.1.186', 'SNMP-COMMUNITY-MIB::snmpTrapAddress' => '192.168.10.20', 'IF-MIB::ifType' => 'ppp', 'IF-MIB::ifIndex' => '2', 'SNMPv2-MIB::snmpTrapOID' => 'IF-MIB::linkUp.1', 'IF-MIB::ifDescr' => 'Serial0/0', 'SNMP-COMMUNITY-MIB::snmpTrapCommunity' => '"public"', 'SNMPv2-MIB::sysUpTime' => '9:16:47:53.80', 'SNMPv2-SMI::enterprises.9.2.2.1.1.20.2' => '"PPP LCP Open"' };
- expand($string)
-
Takes $string and expands it so that macros within the string will be expanded out based on the packet details. Available macros are:
${Px} - Returns the parameter for line x
${Vx} - Returns the value for line x
${TRAPNAME} - Returns the trapname (as called from $trap->trapname)
${HOSTIP} - Returns the IP of the originating packet
${IF-MIB::ifType} - Returns the value for the specified parameter.
${DUMP} - Returns all key, value pairs (stripping out snmpTrapCommunity)
For the example trap above, if you ran:
$trap->expand('Port ${IF-MIB::ifIndex} (${P7}=${V7}) is Up with message ${V8}');
this would return:
Port 2 (ifType=ppp) is Up with message "PPP LCP Open"
SEE ALSO
Net-SNMP - http://www.net-snmp.org
AUTHOR
Ton Voon, <ton.voon@altinity.com>
COPYRIGHT AND LICENSE
Copyright (C) 2006 by Altinity Limited
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.4 or, at your option, any later version of Perl 5 you may have available.