SNMP Trap handler: UpsTrapOnBattery (#13482)

* SNMP Trap handler: upsTrapOnBattery

* Forgot to add the config

* Fixed handler name

* Fix test
This commit is contained in:
TheGreatDoc 2021-11-11 17:08:36 +01:00 committed by GitHub
parent 06701870cd
commit 63b174595f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 145 additions and 0 deletions

View File

@ -0,0 +1,75 @@
<?php
/**
* UpsTrapOnBattery.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 TheGreatDoc
*/
namespace LibreNMS\Snmptrap\Handlers;
use App\Models\Device;
use Illuminate\Support\Str;
use LibreNMS\Interfaces\SnmptrapHandler;
use LibreNMS\Snmptrap\Trap;
use Log;
class UpsTrapOnBattery 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)
{
$min_remaining = Str::before($trap->getOidData($trap->findOid('UPS-MIB::upsEstimatedMinutesRemaining')), ' ');
$sec_time = Str::before($trap->getOidData($trap->findOid('UPS-MIB::upsSecondsOnBattery')), ' ');
Log::event("UPS running on battery for $sec_time seconds. Estimated $min_remaining minutes remaining", $device->device_id, 'trap', 5);
$sensor_remaining = $device->sensors()->where('sensor_index', '200')->where('sensor_type', 'rfc1628')->first();
if (! $sensor_remaining) {
Log::warning("Snmptrap upsTrapOnBattery: Could not find matching sensor \'Estimated battery time remaining\' for device: " . $device->hostname);
return;
}
$sensor_remaining->sensor_current = $min_remaining / $sensor_remaining->sensor_divisor;
$sensor_remaining->save();
$sensor_time = $device->sensors()->where('sensor_index', '100')->where('sensor_type', 'rfc1628')->first();
if (! $sensor_time) {
Log::warning("Snmptrap upsTrapOnBattery: Could not find matching sensor \'Time on battery\' for device: " . $device->hostname);
return;
}
$sensor_time->sensor_current = $sec_time / $sensor_time->sensor_divisor;
$sensor_time->save();
$sensor_output = $device->sensors()->where('sensor_type', 'upsOutputSourceState')->first();
if (! $sensor_output) {
Log::warning("Snmptrap upsTrapOnBattery: Could not find matching sensor \'upsOutputSourceState\' for device: " . $device->hostname);
return;
}
$sensor_output->sensor_current = 5;
$sensor_output->save();
}
}

View File

@ -106,5 +106,6 @@ return [
'VEEAM-MIB::onBackupJobCompleted' => \LibreNMS\Snmptrap\Handlers\VeeamBackupJobCompleted::class,
'VEEAM-MIB::onVmBackupJobCompleted' => \LibreNMS\Snmptrap\Handlers\VeeamVmBackupJobCompleted::class,
'HP-ICF-FAULT-FINDER-MIB::hpicfFaultFinderTrap' => \LibreNMS\Snmptrap\Handlers\HpFault::class,
'UPS-MIB::upsTrapOnBattery' => \LibreNMS\Snmptrap\Handlers\UpsTrapOnBattery::class,
],
];

View File

@ -0,0 +1,69 @@
<?php
/**
* UpsTrapOnBatteryTest.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 TheGreatDoc
*/
namespace LibreNMS\Tests\Feature\SnmpTraps;
use App\Models\Device;
use App\Models\Sensor;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
class UpsTrapOnBatteryTest extends SnmpTrapTestCase
{
public function testOnBattery(): void
{
$device = Device::factory()->create(); /** @var Device $device */
$state = Sensor::factory()->make(['sensor_class' => 'state', 'sensor_type' => 'upsOutputSourceState', 'sensor_current' => '2']); /** @var Sensor $state */
$time = Sensor::factory()->make(['sensor_class' => 'runtime', 'sensor_index' => '100', 'sensor_type' => 'rfc1628', 'sensor_current' => '0']); /** @var Sensor $time */
$remaining = Sensor::factory()->make(['sensor_class' => 'runtime', 'sensor_index' => '200', 'sensor_type' => 'rfc1628', 'sensor_current' => '371']); /** @var Sensor $remaining */
$device->sensors()->save($state);
$device->sensors()->save($time);
$device->sensors()->save($remaining);
$trapText = "$device->hostname
UDP: [$device->ip]:161->[192.168.5.5]:162
DISMAN-EVENT-MIB::sysUpTimeInstance 9:22:15:00.01
SNMPv2-MIB::snmpTrapOID.0 UPS-MIB::upsTrapOnBattery
UPS-MIB::upsEstimatedMinutesRemaining 100 minutes
UPS-MIB::upsSecondsOnBattery 120 seconds
UPS-MIB::upsConfigLowBattTime 1 minutes";
\Log::shouldReceive('warning')->never()->with("Snmptrap upsTrapOnBattery: Could not find matching sensor \'Estimated battery time remaining\' for device: " . $device->hostname);
\Log::shouldReceive('warning')->never()->with("Snmptrap upsTrapOnBattery: Could not find matching sensor \'Time on battery\' for device: " . $device->hostname);
\Log::shouldReceive('warning')->never()->with("Snmptrap upsTrapOnBattery: Could not find matching sensor \'upsOutputSourceState\' for device: " . $device->hostname);
$message = 'UPS running on battery for 120 seconds. Estimated 100 minutes remaining';
\Log::shouldReceive('event')->once()->with($message, $device->device_id, 'trap', 5);
$trap = new Trap($trapText);
$this->assertTrue(Dispatcher::handle($trap), 'Could not handle UPS-MIB::upsTrapOnBattery trap');
$state = $state->fresh();
$time = $time->fresh();
$remaining = $remaining->fresh();
$this->assertEquals($state->sensor_current, '5');
$this->assertEquals($time->sensor_current, '120');
$this->assertEquals($remaining->sensor_current, '100');
}
}