1TrapReceiver(3) User Contributed Perl Documentation TrapReceiver(3)
2
3
4
6 NetSNMP::TrapReceiver - Embedded perl trap handling for Net-SNMP's
7 snmptrapd
8
10 Put the following lines in your snmptrapd.conf file:
11
12 perl NetSNMP::TrapReceiver::register("trapOID", \&myfunc);
13
15 The NetSNMP::TrapReceiver module is used to register perl subroutines
16 into the Net-SNMP snmptrapd process. Net-SNMP MUST have been config‐
17 ured using --enable-embedded-perl. Registration of functions is then
18 done through the snmptrapd.conf configuration file. This module can
19 NOT be used in a normal perl script to receive traps. It is intended
20 solely for embedded use within the snmptrapd demon.
21
23 Within the snmptrapd.conf file, the keyword "perl" may be used to call
24 any perl expression and using this ability, you can use the Net‐
25 SNMP::TrapReceiver module to register functions which will be called
26 every time a given notification (a trap or an inform) is received.
27 Registered functions are called with 2 arguments. The first is a ref‐
28 erence to a hash containing information about how the trap was received
29 (what version of the SNMP protocol was used, where it came from, what
30 SNMP user name or community name it was sent under, etc). The second
31 argument is a reference to an array containing the variable bindings
32 (OID and value information) that define the noification itself. Each
33 variable is itself a reference to an array containing three values: a
34 NetSNMP::OID object, the value that came associated with it, and the
35 value's numeric type (see NetSNMP::ASN for further details on SNMP typ‐
36 ing information).
37
38 Subroutines are registered using the NetSNMP::TrapReceiver::register
39 function, which takes two arguments. The first is a string describing
40 the notification you want to register for (such as "linkUp" or
41 "MyMIB::MyTrap" or ".1.3.6.1.4.1.2021...."). Two special keywords can
42 be used in place of an OID: "default" and "all". The "default" keyword
43 indicates you want your handler to be called in the case where no other
44 handlers are called. The "all" keyword indicates that the handler
45 should ALWAYS be called for every notification.
46
48 As an example, put the following code into a file (say
49 "/usr/local/share/snmp/mytrapd.pl"):
50
51 #!/usr/bin/perl
52
53 sub my_receiver {
54 print "********** PERL RECEIVED A NOTIFICATION:\n";
55
56 # print the PDU info (a hash reference)
57 print "PDU INFO:\n";
58 foreach my $k(keys(%{$_[0]})) {
59 printf " %-30s %s\n", $k, $_[0]{$k};
60 }
61
62 # print the variable bindings:
63 print "VARBINDS:\n";
64 foreach my $x (@{$_[1]}) {
65 printf " %-30s type=%-2d value=%s\n", $x->[0], $x->[2], $x->[1];
66 }
67 }
68
69 NetSNMP::TrapReceiver::register("all", \&my_receiver) ⎪⎪
70 warn "failed to register our perl trap handler\n";
71
72 print STDERR "Loaded the example perl snmptrapd handler\n";
73
74 Then, put the following line in your snmprapd.conf file:
75
76 perl do "/usr/local/share/snmp/mytrapd.pl";
77
78 Start snmptrapd (as root, and the following other opions make it stay
79 in the foreground and log to stderr):
80
81 snmptrapd -f -Le
82
83 You should see it start up and display the final message from the end
84 of the above perl script:
85
86 Loaded the perl snmptrapd handler
87 2004-02-11 10:08:45 NET-SNMP version 5.2 Started.
88
89 Then, if you send yourself a fake trap using the following example com‐
90 mand:
91
92 snmptrap -v 2c -c mycommunity localhost 0 linkUp ifIndex.1 i 1 \
93 ifAdminStatus.1 i up ifOperStatus.1 i up ifDescr s eth0
94
95 You should see the following output appear from snmptrapd as your perl
96 code gets executed:
97
98 ********** PERL RECEIVED A NOTIFICATION:
99 PDU INFO:
100 notificationtype TRAP
101 receivedfrom 127.0.0.1
102 version 1
103 errorstatus 0
104 messageid 0
105 community mycommunity
106 transactionid 2
107 errorindex 0
108 requestid 765160220
109 VARBINDS:
110 sysUpTimeInstance type=67 value=0:0:00:00.00
111 snmpTrapOID.0 type=6 value=linkUp
112 ifIndex.1 type=2 value=1
113 ifAdminStatus.1 type=2 value=1
114 ifOperStatus.1 type=2 value=1
115 ifDescr type=4 value="eth0"
116
118 None by default.
119
120 # =head2 Exportable constants
121
122 # NETSNMPTRAPD_AUTH_HANDLER # NETSNMPTRAPD_HANDLER_BREAK # NETSN‐
123 MPTRAPD_HANDLER_FAIL # NETSNMPTRAPD_HANDLER_FINISH # NETSN‐
124 MPTRAPD_HANDLER_OK # NETSNMPTRAPD_POST_HANDLER # NETSN‐
125 MPTRAPD_PRE_HANDLER
126
128 NetSNMP::OID, NetSNMP::ASN
129
130 snmptrapd.conf(5) for configuring the Net-SNMP trap receiver.
131
132 snmpd.conf(5) for configuring the Net-SNMP snmp agent for sending
133 traps.
134
135 http://www.Net-SNMP.org/
136
138 W. Hardaker, <hardaker@users.sourceforge.net>
139
141 Copyright 2004 by W. Hardaker
142
143 This library is free software; you can redistribute it and/or modify it
144 under the same terms as Perl itself.
145
146
147
148perl v5.8.8 2006-06-30 TrapReceiver(3)