From f7ca687ceb8c7be4332e096f57a5337d2fc2d817 Mon Sep 17 00:00:00 2001 From: Andy Norwood <2754635+bonzo81@users.noreply.github.com> Date: Thu, 10 Mar 2022 20:29:23 +0000 Subject: [PATCH] Add OspfTxRetransmit Trap Handler (#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 --- .../Snmptrap/Handlers/OspfTxRetransmit.php | 60 +++++++++++++ config/snmptraps.php | 1 + .../SnmpTraps/OspfTxRetransmitTest.php | 87 +++++++++++++++++++ 3 files changed, 148 insertions(+) create mode 100755 LibreNMS/Snmptrap/Handlers/OspfTxRetransmit.php mode change 100644 => 100755 config/snmptraps.php create mode 100755 tests/Feature/SnmpTraps/OspfTxRetransmitTest.php diff --git a/LibreNMS/Snmptrap/Handlers/OspfTxRetransmit.php b/LibreNMS/Snmptrap/Handlers/OspfTxRetransmit.php new file mode 100755 index 0000000000..724258920d --- /dev/null +++ b/LibreNMS/Snmptrap/Handlers/OspfTxRetransmit.php @@ -0,0 +1,60 @@ +. + * + * @link https://www.librenms.org + * + * @copyright 2022 Andy Norwood + * @author Andy Norwood + */ + +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); + } +} diff --git a/config/snmptraps.php b/config/snmptraps.php old mode 100644 new mode 100755 index af0abf9138..4d43994c05 --- a/config/snmptraps.php +++ b/config/snmptraps.php @@ -123,6 +123,7 @@ return [ 'VMWARE-VMINFO-MIB::vmwVmSuspended' => \LibreNMS\Snmptrap\Handlers\VmwVmSuspended::class, 'OSPF-TRAP-MIB::ospfIfStateChange' => \LibreNMS\Snmptrap\Handlers\OspfIfStateChange::class, 'OSPF-TRAP-MIB::ospfNbrStateChange' => \LibreNMS\Snmptrap\Handlers\OspfNbrStateChange::class, + 'OSPF-TRAP-MIB::ospfTxRetransmit' => \LibreNMS\Snmptrap\Handlers\OspfTxRetransmit::class, 'UPS-MIB::upsTraps.0.1' => \LibreNMS\Snmptrap\Handlers\UpsTrapsOnBattery::class, 'VEEAM-MIB::onBackupJobCompleted' => \LibreNMS\Snmptrap\Handlers\VeeamBackupJobCompleted::class, 'VEEAM-MIB::onVmBackupCompleted' => \LibreNMS\Snmptrap\Handlers\VeeamVmBackupCompleted::class, diff --git a/tests/Feature/SnmpTraps/OspfTxRetransmitTest.php b/tests/Feature/SnmpTraps/OspfTxRetransmitTest.php new file mode 100755 index 0000000000..2957258ccb --- /dev/null +++ b/tests/Feature/SnmpTraps/OspfTxRetransmitTest.php @@ -0,0 +1,87 @@ +. + * + * @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'); + } +}