/usr/lib/x86_64-linux-gnu/perl5/5.20/NetSNMP/TrapReceiver.pm is in libsnmp-perl 5.7.2.1+dfsg-1+deb8u1+b1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | package NetSNMP::TrapReceiver;
use 5.00006;
use strict;
use Carp;
require Exporter;
require DynaLoader;
use AutoLoader;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $AUTOLOAD);
@ISA = qw(Exporter
DynaLoader);
require NetSNMP::OID;
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
# This allows declaration use NetSNMP::TrapReceiver ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
%EXPORT_TAGS = ( 'all' => [ qw(
NETSNMPTRAPD_AUTH_HANDLER
NETSNMPTRAPD_HANDLER_BREAK
NETSNMPTRAPD_HANDLER_FAIL
NETSNMPTRAPD_HANDLER_FINISH
NETSNMPTRAPD_HANDLER_OK
NETSNMPTRAPD_POST_HANDLER
NETSNMPTRAPD_PRE_HANDLER
netsnmp_add_default_traphandler
netsnmp_add_global_traphandler
netsnmp_add_traphandler
) ] );
@EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
@EXPORT = qw(
NETSNMPTRAPD_AUTH_HANDLER
NETSNMPTRAPD_HANDLER_BREAK
NETSNMPTRAPD_HANDLER_FAIL
NETSNMPTRAPD_HANDLER_FINISH
NETSNMPTRAPD_HANDLER_OK
NETSNMPTRAPD_POST_HANDLER
NETSNMPTRAPD_PRE_HANDLER
);
$VERSION = '5.07021';
# sub new {
# my $type = shift;
# my ($self);
# %$self = @_;
# bless($self, $type);
# return $self;
# }
# sub register($$$$) {
# my ($self, $oid, $sub) = @_;
# my $reg = NetSNMP::TrapReceiver::registration::new($oid, $sub);
# if ($reg) {
# $reg->register();
# $self->{'regobjs'}{$name} = $reg;
# }
# return $reg;
# }
sub AUTOLOAD {
# This AUTOLOAD is used to 'autoload' constants from the constant()
# XS function.
my $constname;
($constname = $AUTOLOAD) =~ s/.*:://;
croak "&NetSNMP::TrapReceiver::constant not defined" if $constname eq 'constant';
my ($error, $val) = constant($constname);
if ($error) { croak $error; }
{
no strict 'refs';
# Fixed between 5.005_53 and 5.005_61
#XXX if ($] >= 5.00561) {
#XXX *$AUTOLOAD = sub () { $val };
#XXX }
#XXX else {
*$AUTOLOAD = sub { $val };
#XXX }
}
goto &$AUTOLOAD;
}
bootstrap NetSNMP::TrapReceiver $VERSION;
# Preloaded methods go here.
# Autoload methods go after =cut, and are processed by the autosplit program.
1;
__END__
=head1 NAME
NetSNMP::TrapReceiver - Embedded perl trap handling for Net-SNMP's snmptrapd
=head1 SYNOPSIS
Put the following lines in your snmptrapd.conf file:
perl NetSNMP::TrapReceiver::register("trapOID", \&myfunc);
=head1 ABSTRACT
The NetSNMP::TrapReceiver module is used to register perl
subroutines into the Net-SNMP snmptrapd process. Net-SNMP MUST have
been configured using --enable-embedded-perl. Registration of
functions is then done through the snmptrapd.conf configuration
file. This module can NOT be used in a normal perl script to
receive traps. It is intended solely for embedded use within the
snmptrapd demon.
=head1 DESCRIPTION
Within the snmptrapd.conf file, the keyword "perl" may be used to call
any perl expression and using this ability, you can use the
NetSNMP::TrapReceiver module to register functions which will be
called every time a given notification (a trap or an inform) is
received. Registered functions are called with 2 arguments. The
first is a reference to a hash containing information about how the
trap was received (what version of the SNMP protocol was used, where
it came from, what SNMP user name or community name it was sent under,
etc). The second argument is a reference to an array containing the
variable bindings (OID and value information) that define the
noification itself. Each variable is itself a reference to an array
containing three values: a NetSNMP::OID object, the value that came
associated with it, and the value's numeric type (see NetSNMP::ASN for
further details on SNMP typing information).
Registered functions should return one of the following values:
=over 2
=item NETSNMPTRAPD_HANDLER_OK
Handling the trap succeeded, but lets the snmptrapd demon check for
further appropriate handlers.
=item NETSNMPTRAPD_HANDLER_FAIL
Handling the trap failed, but lets the snmptrapd demon check for
further appropriate handlers.
=item NETSNMPTRAPD_HANDLER_BREAK
Stops evaluating the list of handlers for this specific trap, but lets
the snmptrapd demon apply global handlers.
=item NETSNMPTRAPD_HANDLER_FINISH
Stops searching for further appropriate handlers.
=back
If a handler function does not return anything appropriate or even
nothing at all, a return value of NETSNMPTRAPD_HANDLER_OK is assumed.
Subroutines are registered using the NetSNMP::TrapReceiver::register
function, which takes two arguments. The first is a string describing
the notification you want to register for (such as "linkUp" or
"MyMIB::MyTrap" or ".1.3.6.1.4.1.2021...."). Two special keywords can
be used in place of an OID: "default" and "all". The "default"
keyword indicates you want your handler to be called in the case where
no other handlers are called. The "all" keyword indicates that the
handler should ALWAYS be called for every notification.
=head1 EXAMPLE
As an example, put the following code into a file (say
"/usr/local/share/snmp/mytrapd.pl"):
#!/usr/bin/perl
sub my_receiver {
print "********** PERL RECEIVED A NOTIFICATION:\n";
# print the PDU info (a hash reference)
print "PDU INFO:\n";
foreach my $k(keys(%{$_[0]})) {
if ($k eq "securityEngineID" || $k eq "contextEngineID") {
printf " %-30s 0x%s\n", $k, unpack('h*', $_[0]{$k});
}
else {
printf " %-30s %s\n", $k, $_[0]{$k};
}
}
# print the variable bindings:
print "VARBINDS:\n";
foreach my $x (@{$_[1]}) {
printf " %-30s type=%-2d value=%s\n", $x->[0], $x->[2], $x->[1];
}
}
NetSNMP::TrapReceiver::register("all", \&my_receiver) ||
warn "failed to register our perl trap handler\n";
print STDERR "Loaded the example perl snmptrapd handler\n";
Then, put the following line in your snmprapd.conf file:
perl do "/usr/local/share/snmp/mytrapd.pl";
Start snmptrapd (as root, and the following other opions make it stay
in the foreground and log to stderr):
snmptrapd -f -Le
You should see it start up and display the final message from the end
of the above perl script:
Loaded the perl snmptrapd handler
2004-02-11 10:08:45 NET-SNMP version 5.2 Started.
Then, if you send yourself a fake trap using the following example command:
snmptrap -v 2c -c mycommunity localhost 0 linkUp ifIndex.1 i 1 \
ifAdminStatus.1 i up ifOperStatus.1 i up ifDescr s eth0
You should see the following output appear from snmptrapd as your perl
code gets executed:
********** PERL RECEIVED A NOTIFICATION:
PDU INFO:
notificationtype TRAP
receivedfrom 127.0.0.1
version 1
errorstatus 0
messageid 0
community mycommunity
transactionid 2
errorindex 0
requestid 765160220
VARBINDS:
sysUpTimeInstance type=67 value=0:0:00:00.00
snmpTrapOID.0 type=6 value=linkUp
ifIndex.1 type=2 value=1
ifAdminStatus.1 type=2 value=1
ifOperStatus.1 type=2 value=1
ifDescr type=4 value="eth0"
=head1 EXPORT
None by default.
# =head2 Exportable constants
# NETSNMPTRAPD_AUTH_HANDLER
# NETSNMPTRAPD_HANDLER_BREAK
# NETSNMPTRAPD_HANDLER_FAIL
# NETSNMPTRAPD_HANDLER_FINISH
# NETSNMPTRAPD_HANDLER_OK
# NETSNMPTRAPD_POST_HANDLER
# NETSNMPTRAPD_PRE_HANDLER
=head1 SEE ALSO
NetSNMP::OID, NetSNMP::ASN
snmptrapd.conf(5) for configuring the Net-SNMP trap receiver.
snmpd.conf(5) for configuring the Net-SNMP snmp agent for sending traps.
http://www.Net-SNMP.org/
=head1 AUTHOR
W. Hardaker, E<lt>hardaker@users.sourceforge.netE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright 2004 by W. Hardaker
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
|