Skip to content

Commit

Permalink
Example SNMP Trap handler class (#10311)
Browse files Browse the repository at this point in the history
* Update SNMP-Traps.md

* Update SNMP-Traps.md
  • Loading branch information
jozefrebjak authored and murrant committed Jun 12, 2019
1 parent fec165f commit fd7defd
Showing 1 changed file with 68 additions and 4 deletions.
72 changes: 68 additions & 4 deletions doc/Developing/SNMP-Traps.md
Expand Up @@ -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
<?php
/**
* ColdBoot.php
*
* Handles the SNMPv2-MIB::coldStart trap
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @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
Expand Down

0 comments on commit fd7defd

Please sign in to comment.