From fd7defd5eceb19498c8e6c6ab19fabb0bb9a09ad Mon Sep 17 00:00:00 2001 From: jozefrebjak <36922215+jozefrebjak@users.noreply.github.com> Date: Wed, 12 Jun 2019 07:19:55 +0200 Subject: [PATCH] Example SNMP Trap handler class (#10311) * Update SNMP-Traps.md * Update SNMP-Traps.md --- doc/Developing/SNMP-Traps.md | 72 ++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 4 deletions(-) diff --git a/doc/Developing/SNMP-Traps.md b/doc/Developing/SNMP-Traps.md index f09a48c67e5b..1d27ab8b0bd5 100644 --- a/doc/Developing/SNMP-Traps.md +++ b/doc/Developing/SNMP-Traps.md @@ -3,13 +3,77 @@ path: blob/master/doc/ # Creating snmp trap handlers -Create a new class in LibreNMS\Snmptrap\Handlers that implements the -LibreNMS\Interfaces\SnmptrapHandler interface. +You must to have working snmptrapd. See [SNMP TRAP HANDLER](../Extensions/SNMP-Trap-Handler.md) -Register the mapping in the config/snmptraps.php file. Make sure to use the full trap oid. +Make sure the MIB is loaded fro the trap you are adding. Edit `/etc/systemd/system/snmptrapd.service.d/mibs.conf` to add it then restart snmprtrapd. + +`MIBDIRS` option is not recursive, so you need to specify each directory individually. + +Create a new class in `LibreNMS\Snmptrap\Handlers` that implements the +`LibreNMS\Interfaces\SnmptrapHandler` interface. For example: + +```php +. + * + * @package LibreNMS + * @link http://librenms.org + */ + +namespace LibreNMS\Snmptrap\Handlers; + +use App\Models\Device; +use LibreNMS\Interfaces\SnmptrapHandler; +use LibreNMS\Snmptrap\Trap; +use Log; + +class ColdBoot implements SnmptrapHandler +{ + /** + * Handle snmptrap. + * Data is pre-parsed and delivered as a Trap. + * + * @param Device $device + * @param Trap $trap + * @return void + */ + public function handle(Device $device, Trap $trap) + { + Log::event('SNMP Trap: Device ' . $device->displayName() . ' cold booted', $device->device_id, 'reboot', 4); + } +} + +``` + +where number on the end of the row `Log::event` means color of the eventlog: +``` +1 green +2 cyan +3 blue +4 yellow +5 red +``` + +Register the mapping in the `config/snmptraps.php` file. Make sure to use the full trap oid and correct class. ```php -'IF-MIB::linkUp' => \LibreNMS\Snmptrap\Handlers\LinkUp::class +'SNMPv2-MIB::coldStart' => \LibreNMS\Snmptrap\Handlers\ColdBoot::class, ``` The handle function inside your new class will receive a LibreNMS/Snmptrap/Trap