SNMP Traps: APC Overload Traps (#13726)

* push apc trap handlers

* added unit test

* removed CR and fixed test

* switch cr for lf

* fixing ci complaints

* fixing another ci error

* fixed unit test

* annotating test
This commit is contained in:
Heath Barnhart
2022-02-08 03:37:11 -06:00
committed by GitHub
parent f8fba91b83
commit a71feb1636
7 changed files with 421 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
<?php
/**
* ApcPduNearOverload.php
*
* -Description-
*
* APC PDU nearing over current level.
*
* 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/>.
*
* Traps when Adva objects are created. This includes Remote User Login object,
* Flow Creation object, and LAG Creation object.
*
* @link http://librenms.org
*
* @copyright 2022 KanREN, Inc
* @author Heath Barnhart <hbarnhart#kanren.net>
*/
namespace LibreNMS\Snmptrap\Handlers;
use App\Models\Device;
use LibreNMS\Interfaces\SnmptrapHandler;
use LibreNMS\Snmptrap\Trap;
use Log;
class ApcPduNearOverload 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)
{
//Get the PDU's name, affected phase, and the alarm string
$pdu_id = ApcTrapUtil::getPduIdentName($trap);
$phase_num = ApcTrapUtil::getPduPhaseNum($trap);
$alarm_str = ApcTrapUtil::getApcTrapString($trap);
Log::event("$pdu_id phase $phase_num $alarm_str", $device->device_id, 'trap', 4);
}
}

View File

@@ -0,0 +1,56 @@
<?php
/**
* ApcPduNearOverloadCleared.php
*
* -Description-
*
* APC PDU nearing over current level has cleared.
*
* 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/>.
*
* Traps when Adva objects are created. This includes Remote User Login object,
* Flow Creation object, and LAG Creation object.
*
* @link http://librenms.org
*
* @copyright 2022 KanREN, Inc
* @author Heath Barnhart <hbarnhart#kanren.net>
*/
namespace LibreNMS\Snmptrap\Handlers;
use App\Models\Device;
use LibreNMS\Interfaces\SnmptrapHandler;
use LibreNMS\Snmptrap\Trap;
use Log;
class ApcPduNearOverloadCleared 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)
{
//Get the PDU's name, affected phase, and the alarm string
$pdu_id = ApcTrapUtil::getPduIdentName($trap);
$phase_num = ApcTrapUtil::getPduPhaseNum($trap);
$alarm_str = ApcTrapUtil::getApcTrapString($trap);
Log::event("$pdu_id phase $phase_num $alarm_str", $device->device_id, 'trap', 1);
}
}

View File

@@ -0,0 +1,56 @@
<?php
/**
* ApcPduOverload.php
*
* -Description-
*
* APC PDU has reached or exceeded the over current load.
*
* 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/>.
*
* Traps when Adva objects are created. This includes Remote User Login object,
* Flow Creation object, and LAG Creation object.
*
* @link http://librenms.org
*
* @copyright 2022 KanREN, Inc
* @author Heath Barnhart <hbarnhart#kanren.net>
*/
namespace LibreNMS\Snmptrap\Handlers;
use App\Models\Device;
use LibreNMS\Interfaces\SnmptrapHandler;
use LibreNMS\Snmptrap\Trap;
use Log;
class ApcPduOverload 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)
{
//Get the PDU's name, affected phase, and the alarm string
$pdu_id = ApcTrapUtil::getPduIdentName($trap);
$phase_num = ApcTrapUtil::getPduPhaseNum($trap);
$alarm_str = ApcTrapUtil::getApcTrapString($trap);
Log::event("$pdu_id phase $phase_num $alarm_str", $device->device_id, 'trap', 5);
}
}

View File

@@ -0,0 +1,56 @@
<?php
/**
* ApcPduOverloadCleared.php
*
* -Description-
*
* APC PDU over current load has fallen below its overload rating.
*
* 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/>.
*
* Traps when Adva objects are created. This includes Remote User Login object,
* Flow Creation object, and LAG Creation object.
*
* @link http://librenms.org
*
* @copyright 2022 KanREN, Inc
* @author Heath Barnhart <hbarnhart#kanren.net>
*/
namespace LibreNMS\Snmptrap\Handlers;
use App\Models\Device;
use LibreNMS\Interfaces\SnmptrapHandler;
use LibreNMS\Snmptrap\Trap;
use Log;
class ApcPduOverloadCleared 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)
{
//Get the PDU's name, affected phase, and the alarm string
$pdu_id = ApcTrapUtil::getPduIdentName($trap);
$phase_num = ApcTrapUtil::getPduPhaseNum($trap);
$alarm_str = ApcTrapUtil::getApcTrapString($trap);
Log::event("$pdu_id phase $phase_num $alarm_str", $device->device_id, 'trap', 1);
}
}

View File

@@ -0,0 +1,67 @@
<?php
/**
* ApcTrapUtil.php
*
* -Description-
*
* Common utility class for handling APC Power traps.
*
*
* 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 KanREN, Inc.
* @author Heath Barnhart <hbarnhart@kanren.net>
*/
namespace LibreNMS\Snmptrap\Handlers;
use LibreNMS\Snmptrap\Trap;
class ApcTrapUtil
{
/**
* Get the APC PDU Name
*
* @param Trap $trap
* @return string
*/
public static function getPduIdentName($trap)
{
return $trap->getOidData($trap->findOid('PowerNet-MIB::rPDUIdentName'));
}
/**
* Get the APC PDU Phase Number
*
* @param Trap $trap
* @return string
*/
public static function getPduPhaseNum($trap)
{
return $trap->getOidData($trap->findOid('PowerNet-MIB::rPDULoadStatusPhaseNumber'));
}
/**
* Get the APC Trap String
*
* @param Trap $trap
* @return string
*/
public static function getApcTrapString($trap)
{
return $trap->getOidData($trap->findOid('PowerNet-MIB::mtrapargsString'));
}
}

View File

@@ -93,6 +93,10 @@ return [
'PowerNet-MIB::outletOff' => \LibreNMS\Snmptrap\Handlers\ApcPduOutletOff::class,
'PowerNet-MIB::outletOn' => \LibreNMS\Snmptrap\Handlers\ApcPduOutletOn::class,
'PowerNet-MIB::outletReboot' => \LibreNMS\Snmptrap\Handlers\ApcPduOutletReboot::class,
'PowerNet-MIB::rPDUNearOverload' => \LibreNMS\Snmptrap\Handlers\ApcPduNearOverload::class,
'PowerNet-MIB::rPDUNearOverloadCleared' => \LibreNMS\Snmptrap\Handlers\ApcPduNearOverloadCleared::class,
'PowerNet-MIB::rPDUOverload' => \LibreNMS\Snmptrap\Handlers\ApcPduOverload::class,
'PowerNet-MIB::rPDUOverloadCleared' => \LibreNMS\Snmptrap\Handlers\ApcPduOverloadCleared::class,
'RUCKUS-EVENT-MIB::ruckusEventAssocTrap' => \LibreNMS\Snmptrap\Handlers\RuckusAssocTrap::class,
'RUCKUS-EVENT-MIB::ruckusEventDiassocTrap' => \LibreNMS\Snmptrap\Handlers\RuckusDiassocTrap::class,
'RUCKUS-EVENT-MIB::ruckusEventSetErrorTrap' => \LibreNMS\Snmptrap\Handlers\RuckusSetError::class,

View File

@@ -0,0 +1,126 @@
<?php
/**
* ApcPduOverloadTest.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
*/
namespace LibreNMS\Tests\Feature\SnmpTraps;
use App\Models\Device;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
class ApcPduOverloadTest extends SnmpTrapTestCase
{
/**
* Test ApcPduNearOverload trap handle
*
* @return void
*/
public function testNearOverload()
{
$device = Device::factory()->create(); /** @var Device $device */
$trapText = "$device->hostname
UDP: [$device->ip]:57602->[192.168.5.5]:162
DISMAN-EVENT-MIB::sysUpTimeInstance 318:0:09:38.28
SNMPv2-MIB::snmpTrapOID.0 PowerNet-MIB::rPDUNearOverload
PowerNet-MIB::rPDUIdentSerialNumber.0 \"5A1036E02224\"
PowerNet-MIB::rPDUIdentName.0 \"Grand POP PDU R15 A1\"
PowerNet-MIB::rPDULoadStatusPhaseNumber.0 1
PowerNet-MIB::mtrapargsString.0 \"Metered Rack PDU: Near overload.\"
SNMPv2-MIB::snmpTrapEnterprise.0 PowerNet-MIB::apc";
$trap = new Trap($trapText);
$message = 'Grand POP PDU R15 A1 phase 1 Metered Rack PDU: Near overload.';
\Log::shouldReceive('event')->once()->with($message, $device->device_id, 'trap', 4);
$this->assertTrue(Dispatcher::handle($trap), 'Could not handle rPDUNearOverload trap');
}
/**
* Test ApcPduNearOverloadClear trap handle
*
* @return void
*/
public function testNearOverloadClear()
{
$device = Device::factory()->create(); /** @var Device $device */
$trapText = "$device->hostname
UDP: [$device->ip]:57602->[192.168.5.5]:162
DISMAN-EVENT-MIB::sysUpTimeInstance 318:0:09:38.28
SNMPv2-MIB::snmpTrapOID.0 PowerNet-MIB::rPDUNearOverloadCleared
PowerNet-MIB::rPDUIdentSerialNumber.0 \"5A1036E02224\"
PowerNet-MIB::rPDUIdentName.0 \"Grand POP PDU R15 A1\"
PowerNet-MIB::rPDULoadStatusPhaseNumber.0 1
PowerNet-MIB::mtrapargsString.0 \"Metered Rack PDU: Near overload cleared.\"
SNMPv2-MIB::snmpTrapEnterprise.0 PowerNet-MIB::apc";
$trap = new Trap($trapText);
$message = 'Grand POP PDU R15 A1 phase 1 Metered Rack PDU: Near overload cleared.';
\Log::shouldReceive('event')->once()->with($message, $device->device_id, 'trap', 1);
$this->assertTrue(Dispatcher::handle($trap), 'Could not handle rPDUNearOverloadClear trap');
}
/**
* Test ApcPduOverload trap handle
*
* @return void
*/
public function testOverload()
{
$device = Device::factory()->create(); /** @var Device $device */
$trapText = "$device->hostname
UDP: [$device->ip]:57602->[192.168.5.5]:162
DISMAN-EVENT-MIB::sysUpTimeInstance 318:0:09:38.28
SNMPv2-MIB::snmpTrapOID.0 PowerNet-MIB::rPDUOverload
PowerNet-MIB::rPDUIdentSerialNumber.0 \"5A1036E02224\"
PowerNet-MIB::rPDUIdentName.0 \"Grand POP PDU R15 A1\"
PowerNet-MIB::rPDULoadStatusPhaseNumber.0 1
PowerNet-MIB::mtrapargsString.0 \"APC Rack PDU: Overload condition.\"
SNMPv2-MIB::snmpTrapEnterprise.0 PowerNet-MIB::apc";
$trap = new Trap($trapText);
$message = 'Grand POP PDU R15 A1 phase 1 APC Rack PDU: Overload condition.';
\Log::shouldReceive('event')->once()->with($message, $device->device_id, 'trap', 5);
$this->assertTrue(Dispatcher::handle($trap), 'Could not handle rPDUOverload trap');
}
/**
* Test ApcPduOverloadCleared trap handle
*
* @return void
*/
public function testOverloadClear()
{
$device = Device::factory()->create(); /** @var Device $device */
$trapText = "$device->hostname
UDP: [$device->ip]:57602->[192.168.5.5]:162
DISMAN-EVENT-MIB::sysUpTimeInstance 318:0:09:38.28
SNMPv2-MIB::snmpTrapOID.0 PowerNet-MIB::rPDUOverloadCleared
PowerNet-MIB::rPDUIdentSerialNumber.0 \"5A1036E02224\"
PowerNet-MIB::rPDUIdentName.0 \"Grand POP PDU R15 A1\"
PowerNet-MIB::rPDULoadStatusPhaseNumber.0 1
PowerNet-MIB::mtrapargsString.0 \"APC Rack PDU: Overload condition has cleared.\"
SNMPv2-MIB::snmpTrapEnterprise.0 PowerNet-MIB::apc";
$trap = new Trap($trapText);
$message = 'Grand POP PDU R15 A1 phase 1 APC Rack PDU: Overload condition has cleared.';
\Log::shouldReceive('event')->once()->with($message, $device->device_id, 'trap', 1);
$this->assertTrue(Dispatcher::handle($trap), 'Could not handle rPDUOverloadClear trap');
}
}