mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Add APC upsOnBattery & powerRestored trap handler (#13836)
* Add new apc classes to snmptraps config * Added new classsed * Added onbattery handler * Added Power Restored handler * Add onbattery test * Added powerRestored test * style fixes Co-authored-by: Jellyfrog <Jellyfrog@users.noreply.github.com>
This commit is contained in:
49
LibreNMS/Snmptrap/Handlers/ApcOnBattery.php
Normal file
49
LibreNMS/Snmptrap/Handlers/ApcOnBattery.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
/**
|
||||
* ApcOnBattery.php
|
||||
*
|
||||
* APC now using battery power.
|
||||
*
|
||||
* -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
|
||||
*
|
||||
* @author Andy Norwood(bonzo81)
|
||||
*/
|
||||
|
||||
namespace LibreNMS\Snmptrap\Handlers;
|
||||
|
||||
use App\Models\Device;
|
||||
use LibreNMS\Interfaces\SnmptrapHandler;
|
||||
use LibreNMS\Snmptrap\Trap;
|
||||
use Log;
|
||||
|
||||
class ApcOnBattery 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)
|
||||
{
|
||||
$message = $trap->getOidData($trap->findOid('PowerNet-MIB::mtrapargsString'));
|
||||
Log::event($message, $device->device_id, 'trap', 4);
|
||||
}
|
||||
}
|
49
LibreNMS/Snmptrap/Handlers/ApcPowerRestored.php
Normal file
49
LibreNMS/Snmptrap/Handlers/ApcPowerRestored.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
/**
|
||||
* ApcPowerRestoredTrap.php
|
||||
*
|
||||
* APC No longer on battery power.
|
||||
*
|
||||
* -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
|
||||
*
|
||||
* @author Andy Norwood(bonzo81)
|
||||
*/
|
||||
|
||||
namespace LibreNMS\Snmptrap\Handlers;
|
||||
|
||||
use App\Models\Device;
|
||||
use LibreNMS\Interfaces\SnmptrapHandler;
|
||||
use LibreNMS\Snmptrap\Trap;
|
||||
use Log;
|
||||
|
||||
class ApcPowerRestored 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)
|
||||
{
|
||||
$message = $trap->getOidData($trap->findOid('PowerNet-MIB::mtrapargsString'));
|
||||
Log::event($message, $device->device_id, 'trap', 1);
|
||||
}
|
||||
}
|
@@ -100,6 +100,8 @@ return [
|
||||
'PowerNet-MIB::rPDUNearOverloadCleared' => \LibreNMS\Snmptrap\Handlers\ApcPduNearOverloadCleared::class,
|
||||
'PowerNet-MIB::rPDUOverload' => \LibreNMS\Snmptrap\Handlers\ApcPduOverload::class,
|
||||
'PowerNet-MIB::rPDUOverloadCleared' => \LibreNMS\Snmptrap\Handlers\ApcPduOverloadCleared::class,
|
||||
'PowerNet-MIB::upsOnBattery' => \LibreNMS\Snmptrap\Handlers\ApcOnBattery::class,
|
||||
'PowerNet-MIB::powerRestored' => \LibreNMS\Snmptrap\Handlers\ApcPowerRestored::class,
|
||||
'PowerNet-MIB::smartAvrReducing' => \LibreNMS\Snmptrap\Handlers\ApcSmartAvrReducing::class,
|
||||
'PowerNet-MIB::smartAvrReducingOff' => \LibreNMS\Snmptrap\Handlers\ApcSmartAvrReducingOff::class,
|
||||
'RUCKUS-EVENT-MIB::ruckusEventAssocTrap' => \LibreNMS\Snmptrap\Handlers\RuckusAssocTrap::class,
|
||||
|
53
tests/Feature/SnmpTraps/ApcOnBatteryTest.php
Executable file
53
tests/Feature/SnmpTraps/ApcOnBatteryTest.php
Executable file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/**
|
||||
* ApcOnBatteryTest.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
|
||||
*
|
||||
* @author Andy Norwood(bonzo81)
|
||||
*/
|
||||
|
||||
namespace LibreNMS\Tests\Feature\SnmpTraps;
|
||||
|
||||
use App\Models\Device;
|
||||
use LibreNMS\Snmptrap\Dispatcher;
|
||||
use LibreNMS\Snmptrap\Trap;
|
||||
|
||||
class ApcOnBatteryTest extends SnmpTrapTestCase
|
||||
{
|
||||
/**
|
||||
* Test ApcOnBattery handle
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testApcOnBattery()
|
||||
{
|
||||
$device = Device::factory()->create(); /** @var Device $device */
|
||||
$trapText = "$device->hostname
|
||||
UDP: [$device->ip]:57602->[10.0.0.1]:162
|
||||
SNMPv2-MIB::sysUpTime.0 18:30:30.32
|
||||
SNMPv2-MIB::snmpTrapOID.0 PowerNet-MIB::upsOnBattery
|
||||
PowerNet-MIB::mtrapargsString \"The UPS has switched to battery backup power.\"
|
||||
SNMPv2-MIB::snmpTrapEnterprise.0 PowerNet-MIB::apc";
|
||||
|
||||
$trap = new Trap($trapText);
|
||||
$message = 'The UPS has switched to battery backup power.';
|
||||
\Log::shouldReceive('event')->once()->with($message, $device->device_id, 'trap', 4);
|
||||
$this->assertTrue(Dispatcher::handle($trap), 'Could not handle testApcOnBattery trap');
|
||||
}
|
||||
}
|
53
tests/Feature/SnmpTraps/ApcPowerRestoredTest.php
Executable file
53
tests/Feature/SnmpTraps/ApcPowerRestoredTest.php
Executable file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/**
|
||||
* ApcPowerRestoredTest.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
|
||||
*
|
||||
* @author Andy Norwood(bonzo81)
|
||||
*/
|
||||
|
||||
namespace LibreNMS\Tests\Feature\SnmpTraps;
|
||||
|
||||
use App\Models\Device;
|
||||
use LibreNMS\Snmptrap\Dispatcher;
|
||||
use LibreNMS\Snmptrap\Trap;
|
||||
|
||||
class ApcPowerRestoredTest extends SnmpTrapTestCase
|
||||
{
|
||||
/**
|
||||
* Test ApcPowerRestored handle
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testApcPowerRestored()
|
||||
{
|
||||
$device = Device::factory()->create(); /** @var Device $device */
|
||||
$trapText = "$device->hostname
|
||||
UDP: [$device->ip]:57602->[10.0.0.1]:162
|
||||
SNMPv2-MIB::sysUpTime.0 18:30:30.32
|
||||
SNMPv2-MIB::snmpTrapOID.0 PowerNet-MIB::powerRestored
|
||||
PowerNet-MIB::mtrapargsString.0 \"INFORMATIONAL: Utility power has been restored.\"
|
||||
SNMPv2-MIB::snmpTrapEnterprise.0 PowerNet-MIB::apc";
|
||||
|
||||
$trap = new Trap($trapText);
|
||||
$message = 'INFORMATIONAL: Utility power has been restored.';
|
||||
\Log::shouldReceive('event')->once()->with($message, $device->device_id, 'trap', 1);
|
||||
$this->assertTrue(Dispatcher::handle($trap), 'Could not handle testApcPowerRestored trap');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user