NAME
Net::SNMPTrapd - Perl implementation of SNMP Trap Listener
SYNOPSIS
use Net::SNMPTrapd;
my $snmptrapd = Net::SNMPTrapd->new()
or die "Error creating SNMPTrapd listener: %s", Net::SNMPTrapd->error;
while (1) {
my $trap;
if (!($trap = $snmptrapd->get_trap())) { next }
if (!(defined($trap->process_trap()))) {
printf "$0: %s\n", Net::SNMPTrapd->error
} else {
printf "%s\t%i\t%i\t%s\n",
$message->peeraddr,
$message->peerport,
$message->version,
$message->community
}
}
DESCRIPTION
Net::SNMPTrapd is a class implementing a simple SNMP Trap listener in Perl. Net::SNMPTrapd will accept traps on the default SNMP Trap port (UDP 162) and attempts to decode them. Net::SNMPTrapd supports SNMP v1 and v2c traps.
Net::SNMPTrapd uses Convert::ASN1 by Graham Barr to do the decoding.
METHODS
new() - create a new Net::SNMPTrapd object
my $snmptrapd = new Net::SNMPTrapd([OPTIONS]);
or
my $snmptrapd = Net::SNMPTrapd->new([OPTIONS]);
Create a new Net::SNMPTrapd object with OPTIONS as optional parameters. Valid options are:
Option Description Default
------ ----------- -------
-LocalAddr Interface to bind to any
-LocalPort Port to bind server to 162
-Timeout Timeout in seconds to wait for request 10
get_trap() - listen for SNMP traps
my $trap = $snmptrapd->get_trap();
Listen for a SNMP trap. Timeout after default or user specified timeout set in new
method and return '0'; else, return is defined.
process_trap() - process received SNMP trap
$trap->process_trap();
Process a received SNMP trap. Varbinds are extracted and processed as SNMP ASN.1 types.
This can also be called as a procedure if one is inclined to write their own UDP listener instead of using get_trap()
. For example:
$sock = IO::Socket::INET->new( blah blah blah );
$sock->recv($buffer, 1500);
$trap = Net::SNMPTrapd->process_trap($buffer);
In either instantiation, allows the following methods to be called.
datagram() - return datagram from SNMP trap
$trap->datagram();
Return the raw datagram received from a processed (process_trap()
) SNMP trap.
peeraddr() - return remote address from SNMP trap
$trap->peeraddr();
Return peer address value from a received and processed (process_trap()
) SNMP trap. This is the address from the IP header on the UDP datagram.
peerport() - return remote port from SNMP trap
$trap->peerport();
Return peer port value from a received and processed (process_trap()
) SNMP trap. This is the port from the IP header on the UDP datagram.
version() - return version from SNMP trap
$trap->version();
Return SNMP Trap version from a received and processed (process_trap()
) SNMP trap.
NOTE: This module only decodes SNMP v1 and v2c traps.
community() - return community from SNMP trap
$trap->community();
Return community string from a received and processed (process_trap()
) SNMP trap.
varbinds() - return varbinds from SNMP trap
$trap->varbinds();
Return varbinds from a received and processed (process_trap()
) SNMP trap. This returns a pointer to an array containing a hash as each array element. The key/value pairs of each hash are the OID/value pairs for each varbind in the received trap.
An example extraction of the varbind data is provided:
for my $vals (@{$trap->varbinds}) {
for (keys(%{$vals})) {
$p .= sprintf "%s: %s; ", $_, $vals->{$_}
}
}
print "$p\n";
The above code will print the varbinds as:
OID: val; OID: val; OID: val; [...]
SNMP v1 SPECIFIC
The following methods are SNMP v1 trap specific.
ent_OID() - return enterprise OID from SNMP v1 trap
$trap->ent_OID();
Return enterprise OID from a received and processed (process_trap()
) SNMP v1 trap.
agentaddr() - return agent address from SNMP v1 trap
$trap->agentaddr();
Return agent address from a received and processed (process_trap()
) SNMP v1 trap.
generic_trap() - return generic trap from SNMP v1 trap
$trap->generic_trap([1]);
Return generic trap type from a received and processed (process_trap()
) SNMP v1 trap. This is the text representation of the generic trap type. For the raw number, use the optional boolean argument.
specific_trap() - return specific trap from SNMP v1 trap
$trap->specific_trap();
Return specific trap type from a received and processed (process_trap()
) SNMP v1 trap.
timeticks() - return timeticks from SNMP v1 trap
$trap->timeticks();
Return timeticks from a received and processed (process_trap()
) SNMP v1 trap.
SNMP v2c SPECIFIC
The following methods are SNMP v2c trap specific.
request_ID() - return request ID from SNMP v2c trap
$trap->request_ID();
Return request ID from a received and processed (process_trap()
) SNMP v2c trap.
error_status() - return error status from SNMP v2c trap
$trap->error_status();
Return error_status from a received and processed (process_trap()
) SNMP v2c trap.
error_index() - return error index from SNMP v2c trap
$trap->error_index();
Return error index from a received and processed (process_trap()
) SNMP v2c trap.
error() - return last error
printf "Error: %s\n", Net::SNMPTrapd->error;
Return last error.
EXPORT
None by default.
EXAMPLES
Simple SNMP Trap Server
This example implements a simple SNMP Trap server that listens on the default port and prints received messages to the console.
use Net::SNMPTrapd;
my $snmptrapd = Net::SNMPTrapd->new()
or die "Error creating SNMPTrapd listener: %s", Net::SNMPTrapd->error;
while (1) {
my $trap;
if (!($trap = $snmptrapd->get_trap())) { next }
if (!(defined($trap->process_trap()))) {
printf "$0: %s\n", Net::SNMPTrapd->error
} else {
my $p = sprintf "%s\t%i\t%i\t%s\t",
$trap->peeraddr,
$trap->peerport,
$trap->version,
$trap->community;
if ($trap->version == 1) {
$p .= sprintf "%s\t%s\t%s\t%s\t%s\t",
$trap->ent_OID,
$trap->agentaddr,
$trap->generic_trap,
$trap->specific_trap,
$trap->timeticks
} else {
$p .= sprintf "%s\t%s\t%s\t",
$trap->request_ID,
$trap->error_status,
$trap->error_index
}
for my $varbind (@{$trap->varbinds}) {
for (keys(%{$varbind})) {
# Here, one could use a MIB translation table or
# Perl module to map OID's ($_) to text and values
# ($varbind->{$_}) to applicable meanings or metrics.
# This example just prints -> OID: val; OID: val; ...
$p .= sprintf "%s: %s; ", $_, $varbind->{$_}
}
}
print "$p\n"
}
}
SEE ALSO
Convert::ASN1
LICENSE
This software is released under the same terms as Perl itself. If you don't know what that means visit http://perl.com/.
AUTHOR
Copyright (C) Michael Vincent 2010
All rights reserved