forked from librenms/librenms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OspfTxRetransmit Trap Handler (librenms#13824)
* OspfTxRetransmit class * Message tidyup * Initial test class * add ospftxretransmit class * add empty line * chnage int to string in traptext * new block to catch non lsupdates trap * Add second test * lint fail changes * extra space removed
- Loading branch information
Showing
3 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
/** | ||
* OspfTxRetransmit.php | ||
* | ||
* -Description- | ||
* | ||
* 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 <https://www.gnu.org/licenses/>. | ||
* | ||
* @link https://www.librenms.org | ||
* | ||
* @copyright 2022 Andy Norwood | ||
* @author Andy Norwood <[email protected]> | ||
*/ | ||
|
||
namespace LibreNMS\Snmptrap\Handlers; | ||
|
||
use App\Models\Device; | ||
use LibreNMS\Interfaces\SnmptrapHandler; | ||
use LibreNMS\Snmptrap\Trap; | ||
use Log; | ||
|
||
class OspfTxRetransmit 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) | ||
{ | ||
$ospfRouterId = $trap->getOidData($trap->findOid('OSPF-MIB::ospfRouterId')); | ||
$ospfPacketType = $trap->getOidData($trap->findOid('OSPF-TRAP-MIB::ospfPacketType')); | ||
$ospfLsdbRouterId = $trap->getOidData($trap->findOid('OSPF-MIB::ospfLsdbRouterId')); | ||
$ospfLsdbType = $trap->getOidData($trap->findOid('OSPF-MIB::ospfLsdbType')); | ||
$ospfNbrRtrId = $trap->getOidData($trap->findOid('OSPF-MIB::ospfNbrRtrId')); | ||
$ospfLsdbLsid = $trap->getOidData($trap->findOid('OSPF-MIB::ospfLsdbLsid')); | ||
|
||
if ($ospfPacketType != 'lsUpdate') { | ||
Log::event('SNMP TRAP: ' . $device->displayName() . "(Router ID: $ospfRouterId) sent a $ospfPacketType packet to $ospfNbrRtrId.", $device->device_id, 'trap', 2); | ||
|
||
return; | ||
} | ||
|
||
Log::event('SNMP Trap: OSPFTxRetransmit trap recieved from ' . $device->displayName() . "(Router ID: $ospfRouterId). A $ospfPacketType packet was sent to $ospfNbrRtrId. LSType: $ospfLsdbType, route ID: $ospfLsdbLsid, originating from $ospfLsdbRouterId.", $device->device_id, 'trap', 2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
/** | ||
* OspfTxRetransmitTest.php | ||
* | ||
* -Description- | ||
* | ||
* Unit test for the OspfTxRetransmit SNMP trap handler. Will verify | ||
* that the trap is handled correctly logging the right event. | ||
* | ||
* 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 <https://www.gnu.org/licenses/>. | ||
* | ||
* @link https://www.librenms.org | ||
*/ | ||
|
||
namespace LibreNMS\Tests\Feature\SnmpTraps; | ||
|
||
use App\Models\Device; | ||
use LibreNMS\Snmptrap\Dispatcher; | ||
use LibreNMS\Snmptrap\Trap; | ||
|
||
class OspfTxRetransmitTest extends SnmpTrapTestCase | ||
{ | ||
/** | ||
* Test OSPF lsUpdate packet type trap | ||
* | ||
* @return void | ||
*/ | ||
public function testLsUpdatePacket() | ||
{ | ||
$device = Device::factory()->create(); /** @var Device $device */ | ||
$trapText = "$device->hostname | ||
UDP: [$device->ip]:57602->[10.0.0.1]:162 | ||
SNMPv2-MIB::sysUpTime.0 16:21:49.33 | ||
SNMPv2-MIB::snmpTrapOID.0 OSPF-TRAP-MIB::ospfTxRetransmit | ||
OSPF-MIB::ospfRouterId 10.1.2.3 | ||
OSPF-MIB::ospfIfIpAddress 10.8.9.10 | ||
OSPF-MIB::ospfAddressLessIf 0 | ||
OSPF-MIB::ospfNbrRtrId 10.3.4.5 | ||
OSPF-TRAP-MIB::ospfPacketType lsUpdate | ||
OSPF-MIB::ospfLsdbType routerLink | ||
OSPF-MIB::ospfLsdbLsid 10.1.1.0 | ||
OSPF-MIB::ospfLsdbRouterId 10.4.5.6"; | ||
|
||
$trap = new Trap($trapText); | ||
$message = 'SNMP Trap: OSPFTxRetransmit trap recieved from ' . $device->displayName() . '(Router ID: 10.1.2.3). A lsUpdate packet was sent to 10.3.4.5. LSType: routerLink, route ID: 10.1.1.0, originating from 10.4.5.6.'; | ||
\Log::shouldReceive('event')->once()->with($message, $device->device_id, 'trap', 2); | ||
$this->assertTrue(Dispatcher::handle($trap), 'Could not handle testlsUpdatePacket trap'); | ||
} | ||
|
||
/** | ||
* Test OSPF non lsUpdate packet type | ||
* | ||
* @return void | ||
*/ | ||
public function testNotLsUpdatePacket() | ||
{ | ||
$device = Device::factory()->create(); /** @var Device $device */ | ||
$trapText = "$device->hostname | ||
UDP: [$device->ip]:57602->[10.0.0.1]:162 | ||
SNMPv2-MIB::sysUpTime.0 16:21:49.33 | ||
SNMPv2-MIB::snmpTrapOID.0 OSPF-TRAP-MIB::ospfTxRetransmit | ||
OSPF-MIB::ospfRouterId 10.1.2.3 | ||
OSPF-MIB::ospfIfIpAddress 10.8.9.10 | ||
OSPF-MIB::ospfAddressLessIf 0 | ||
OSPF-MIB::ospfNbrRtrId 10.3.4.5 | ||
OSPF-TRAP-MIB::ospfPacketType hello | ||
OSPF-MIB::ospfLsdbType routerLink | ||
OSPF-MIB::ospfLsdbLsid 10.1.1.0 | ||
OSPF-MIB::ospfLsdbRouterId 10.4.5.6"; | ||
|
||
$trap = new Trap($trapText); | ||
$message = 'SNMP TRAP: ' . $device->displayName() . '(Router ID: 10.1.2.3) sent a hello packet to 10.3.4.5.'; | ||
\Log::shouldReceive('event')->once()->with($message, $device->device_id, 'trap', 2); | ||
$this->assertTrue(Dispatcher::handle($trap), 'Could not handle testNotLsUpdatePacket trap'); | ||
} | ||
} |