2018-08-14 01:56:16 -05:00
|
|
|
# Creating snmp trap handlers
|
|
|
|
|
|
2019-06-20 13:53:45 -05:00
|
|
|
You must have a working snmptrapd. See
|
|
|
|
|
[SNMP TRAP HANDLER](../Extensions/SNMP-Trap-Handler.md)
|
2018-08-14 01:56:16 -05:00
|
|
|
|
2019-06-20 13:53:45 -05:00
|
|
|
Make sure the MIB is loaded from the trap you are adding. Edit
|
|
|
|
|
`/etc/systemd/system/snmptrapd.service.d/mibs.conf` to add it then
|
|
|
|
|
restart snmptrapd.
|
2019-06-12 07:19:55 +02:00
|
|
|
|
|
|
|
|
`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
|
2021-02-09 00:29:04 +01:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-06-12 07:19:55 +02:00
|
|
|
*
|
|
|
|
|
* @package LibreNMS
|
2021-02-09 00:29:04 +01:00
|
|
|
* @link https://www.librenms.org
|
2019-06-12 07:19:55 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace LibreNMS\Snmptrap\Handlers;
|
|
|
|
|
|
|
|
|
|
use App\Models\Device;
|
2023-08-05 12:12:36 -05:00
|
|
|
use LibreNMS\Enum\Severity;
|
2019-06-12 07:19:55 +02:00
|
|
|
use LibreNMS\Interfaces\SnmptrapHandler;
|
|
|
|
|
use LibreNMS\Snmptrap\Trap;
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2023-08-05 12:12:36 -05:00
|
|
|
$trap->log('SNMP Trap: Device ' . $device->displayName() . ' cold booted', $device->device_id, 'reboot', Severity::Warning);
|
2019-06-12 07:19:55 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2022-11-05 14:43:54 -05:00
|
|
|
where number on the end means color of the eventlog:
|
2019-06-20 13:53:45 -05:00
|
|
|
|
2019-06-12 07:19:55 +02:00
|
|
|
```
|
2023-08-25 00:49:22 +01:00
|
|
|
Severity::Ok = green
|
|
|
|
|
Severity::Info = cyan
|
|
|
|
|
Severity::Notice = blue
|
|
|
|
|
Severity::Warning = yellow
|
|
|
|
|
Severity::Error = red
|
2019-06-12 07:19:55 +02:00
|
|
|
```
|
|
|
|
|
|
2019-06-20 13:53:45 -05:00
|
|
|
Register the mapping in the `config/snmptraps.php` file. Make sure to
|
|
|
|
|
use the full trap OID and correct class.
|
2018-08-14 01:56:16 -05:00
|
|
|
|
|
|
|
|
```php
|
2019-06-12 07:19:55 +02:00
|
|
|
'SNMPv2-MIB::coldStart' => \LibreNMS\Snmptrap\Handlers\ColdBoot::class,
|
2018-08-14 01:56:16 -05:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The handle function inside your new class will receive a LibreNMS/Snmptrap/Trap
|
|
|
|
|
object containing the parsed trap. It is common to update the database and create
|
|
|
|
|
event log entries within the handle function.
|
|
|
|
|
|
|
|
|
|
### Getting information from the Trap
|
|
|
|
|
|
|
|
|
|
#### Source information
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
$trap->getDevice(); // gets Device model for the device associated with this trap
|
2022-11-05 14:43:54 -05:00
|
|
|
$trap->ip; // gets source IP of this trap
|
2018-08-14 01:56:16 -05:00
|
|
|
$trap->getTrapOid(); // returns the string you registered your class with
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Retrieving data from the Trap
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
$trap->getOidData('IF-MIB::ifDescr.114');
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
getOidData() requires the full name including any additional index.
|
2019-06-17 16:23:44 -05:00
|
|
|
You can use these functions to search the OID keys.
|
2018-08-14 01:56:16 -05:00
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
$trap->findOid('ifDescr'); // returns the first oid key that contains the string
|
|
|
|
|
$trap->findOids('ifDescr'); // returns all oid keys containing the string
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Advanced
|
|
|
|
|
|
2019-06-17 16:23:44 -05:00
|
|
|
If the above isn't adequate, you can get the entire trap text:
|
2018-08-14 01:56:16 -05:00
|
|
|
|
|
|
|
|
```php
|
2022-11-05 14:43:54 -05:00
|
|
|
$trap->raw;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Tests
|
|
|
|
|
|
|
|
|
|
Submitting new traps requires them to be fully tested. You can find many examples in the
|
|
|
|
|
`tests/Feature/SnmpTraps/` directory.
|
|
|
|
|
|
|
|
|
|
Here is a basic example of a test that trap handler only creates a log message.
|
|
|
|
|
If your trap modifies the database, you should also test that it does so.
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace LibreNMS\Tests\Feature\SnmpTraps;
|
|
|
|
|
|
|
|
|
|
class ColdStratTest extends SnmpTrapTestCase
|
|
|
|
|
{
|
|
|
|
|
public function testColdStart(): void
|
|
|
|
|
{
|
|
|
|
|
$this->assertTrapLogsMessage(rawTrap: <<<'TRAP'
|
|
|
|
|
{{ hostname }}
|
|
|
|
|
UDP: [{{ ip }}]:44298->[192.168.5.5]:162
|
|
|
|
|
DISMAN-EVENT-MIB::sysUpTimeInstance 0:0:1:12.7
|
|
|
|
|
SNMPv2-MIB::snmpTrapOID.0 SNMPv2-MIB::coldStart
|
|
|
|
|
TRAP,
|
|
|
|
|
log: 'SNMP Trap: Device {{ hostname }} cold booted', // The log message sent
|
|
|
|
|
failureMessage: 'Failed to handle SNMPv2-MIB::coldStart', // an informative message to let user know what failed
|
|
|
|
|
args: [4, 'reboot'], // the additional arguments to the log method
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-14 01:56:16 -05:00
|
|
|
```
|