Create factory->Sensor model. Fix some format issues in UpsTrapsOnBattery.php and more UpsTrapsOnBatteryTests

This commit is contained in:
TheGreatDoc
2020-06-07 11:21:14 +02:00
parent 53c5eed4f7
commit 6f91e9e287
3 changed files with 32 additions and 6 deletions

View File

@@ -43,27 +43,30 @@ class UpsTrapsOnBattery implements SnmptrapHandler
{
$min_remaining = $trap->getOidData($trap->findOid('UPS-MIB::upsEstimatedMinutesRemaining.0'));
$sec_time = $trap->getOidData($trap->findOid('UPS-MIB::upsSecondsOnBattery.0'));
Log::event("UPS running on battery for $sec_time. Estimated $min_remaining remaining", $trap->getDevice(), 'trap', 5);
Log::event("UPS running on battery for $sec_time. Estimated $min_remaining remaining", $device->device_id, 'trap', 5);
$sensor_remaining = $device->sensors()->where('sensor_index', '200')->where('sensor_type', 'rfc1628')->first();
if(!$sensor_remaining){
if (!$sensor_remaining) {
Log::warning("Snmptrap UpsTraps: 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){
if (!$sensor_time) {
Log::warning("Snmptrap UpsTraps: 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){
if (!$sensor_output) {
Log::warning("Snmptrap UpsTraps: Could not find matching sensor \'upsOutputSourceState\' for device: " . $device->hostname);
return;
}
$sensor_output->sensor_current = 5;
$sensor_output->save();
}
}

View File

@@ -164,3 +164,13 @@ $factory->define(\App\Models\Component::class, function (Faker\Generator $faker)
'type' => $faker->regexify('[A-Za-z0-9]{4,20}'),
];
});
$factory->define(\App\Models\Sensor::class, function (Faker\Generator $faker) {
$sensor_class = ['airflow','ber','charge','chromatic_dispersion','cooling','count','current','dbm','delay','eer','fanspeed','frequency','humidity','load','loss','power','power_consumed','power_factor','pressure','quality_factor','runtime','signal','snr','state','temperature','voltage','waterflow'];
return [
'sensor_id' => $faker->randomDigit,
'sensor_index' => $faker->randomDigit,
'sensor_class' => $faker->randomElement($sensor_class),
'sensor_value' => $faker->randomDigit,
];
});

View File

@@ -25,17 +25,19 @@
namespace LibreNMS\Tests\Feature\SnmpTraps;
use App\Models\Device;
use App\Models\Ipv4Address;
use App\Models\Sensor;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
use LibreNMS\Tests\DBTestCase;
class UpsTrapsOnBatteryTest extends SnmpTrapTestCase
{
public function testOnBattery()
{
$device = factory(Device::class)->create();
$state = factory(Sensor::class)->make(['sensor_class' => 'state', 'sensor_type' => 'upsOutputSourceState', 'sensor_current' => '2']);
$time = factory(Sensor::class)->make(['sensor_class' => 'runtime', 'sensor_index' => '100', 'sensor_current' => '0']);
$remaining = factory(Sensor::class)->make(['sensor_class' => 'runtime', 'sensor_index' => '200', 'sensor_current' => '371']);
$trapText = "$device->hostname
UDP: [$device->ip]:161->[192.168.5.5]:162
@@ -45,10 +47,21 @@ UPS-MIB::upsEstimatedMinutesRemaining.0 100 minutes
UPS-MIB::upsSecondsOnBattery.0 120 seconds
UPS-MIB::upsConfigLowBattTime.0 1 minutes";
\Log::shouldReceive('warning')->never()->with("Snmptrap UpsTraps: Could not find matching sensor \'Estimated battery time remaining\' for device: " . $device->hostname);
\Log::shouldReceive('warning')->never()->with("Snmptrap UpsTraps: Could not find matching sensor \'Time on battery\' for device: " . $device->hostname);
\Log::shouldReceive('warning')->never()->with("Snmptrap UpsTraps: 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::upsTraps.0.1 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');
}
}