Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed device missing from traps/new Log::event() #9963

Merged
merged 3 commits into from Mar 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion LibreNMS/Snmptrap/Dispatcher.php
Expand Up @@ -51,7 +51,7 @@ public static function handle(Trap $trap)
$fallback = $handler instanceof Fallback;
$logging = Config::get('snmptraps.eventlog', 'unhandled');
if ($logging == 'all' || ($fallback && $logging == 'unhandled')) {
log_event("SNMP trap received: " . $trap->getTrapOid(), $trap->getDevice()->toArray(), 'trap');
Log::event("SNMP trap received: " . $trap->getTrapOid(), $trap->getDevice(), 'trap');
}

return !$fallback;
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Eventlog.php
Expand Up @@ -32,7 +32,7 @@ class Eventlog extends DeviceRelatedModel
protected $table = 'eventlog';
protected $primaryKey = 'event_id';
public $timestamps = false;
protected $fillable = ['datetime', 'message', 'type', 'reference', 'username', 'severity'];
protected $fillable = ['datetime', 'device_id', 'message', 'type', 'reference', 'username', 'severity'];

// ---- Helper Functions ----

Expand Down
31 changes: 30 additions & 1 deletion tests/Feature/SnmpTraps/CommonTrapTest.php
Expand Up @@ -26,12 +26,12 @@
namespace LibreNMS\Tests;

use App\Models\Device;
use App\Models\Eventlog;
use App\Models\Ipv4Address;
use App\Models\Port;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use LibreNMS\Snmptrap\Dispatcher;
use LibreNMS\Snmptrap\Trap;
use LibreNMS\Tests\Feature\SnmpTraps\TrapTestCase;
use Log;

class CommonTrapTest extends LaravelTestCase
Expand All @@ -58,13 +58,42 @@ public function testFindByIp()
UDP: [$ipv4->ipv4_address]:64610->[192.168.5.5]:162
DISMAN-EVENT-MIB::sysUpTimeInstance 198:2:10:48.91\n";

Log::shouldReceive('info')->once()->with('Unhandled trap snmptrap', ['device' => $device->hostname, 'oid' => null]);
Log::shouldReceive('event')->once()->withArgs(function ($e_message, $e_device, $e_type) use ($device) {
return $e_message == 'SNMP trap received: ' &&
$device->is($e_device) &&
$e_type == 'trap';
});

$trap = new Trap($trapText);
$this->assertFalse(Dispatcher::handle($trap), 'Found handler for trap with no snmpTrapOID');

// check that the device was found
$this->assertEquals($device->hostname, $trap->getDevice()->hostname);
}

public function testGenericTrap()
{
$device = factory(Device::class)->create();

$trapText = "$device->hostname
UDP: [$device->ip]:64610->[192.168.5.5]:162
DISMAN-EVENT-MIB::sysUpTimeInstance 198:2:10:48.91
SNMPv2-MIB::snmpTrapOID.0 SNMPv2-MIB::someOid\n";

$trap = new Trap($trapText);
$this->assertFalse(Dispatcher::handle($trap));

$this->assertEquals([
'device_id' => $device->device_id,
'message' => 'SNMP trap received: SNMPv2-MIB::someOid',
'type' => 'trap',
'reference' => null,
'username' => '',
'severity' => 2,
], Eventlog::orderBy('event_id', 'asc')->select(['device_id', 'message', 'type', 'reference', 'username', 'severity'])->first()->toArray());
}

public function testAuthorization()
{
$device = factory(Device::class)->create();
Expand Down