NetSNMP::TrapReceiver - Embedded perl trap handling for Net-SNMP's snmptrapd
perl NetSNMP::TrapReceiver::register("trapOID", \&myfunc);
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.
#!/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]})) { 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"
# =head2 Exportable constants
# NETSNMPTRAPD_AUTH_HANDLER # NETSNMPTRAPD_HANDLER_BREAK # NETSNMPTRAPD_HANDLER_FAIL # NETSNMPTRAPD_HANDLER_FINISH # NETSNMPTRAPD_HANDLER_OK # NETSNMPTRAPD_POST_HANDLER # NETSNMPTRAPD_PRE_HANDLER
snmptrapd.conf(5) for configuring the Net-SNMP trap receiver.
snmpd.conf(5) for configuring the Net-SNMP snmp agent for sending traps.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |