Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* Reorganize trap tests

* Testing db DRIVER to prevent .env from interfering

* New code to detect if Laravel is booted.  Hopefully more reliable.

* WIP external test process

* revert module test helper

* Use .env in Eloquent::boot()

* Fix test database settings loading

* fix undefined classes
(didn't find the one I needed)

* Fix incorrect Config usages
And RrdDefinition return type

* fix .env loading

* use the right DB

* slightly more accurate isConnected

* Move db_name to DBSetupTest specifically

* restore $_SERVER in AuthSSOTest

* missed item

* WIP

* tear down in the correct order.

* some testing cleanups

* remove check for duplicate event listener, it's not working right

* Don't need this change anymore

* Implement Log::event to replace legacy function log_event()

* fix port tests

* fix up tests

* remove pointless TrapTestCase class

* fix style

* Fix db config not being merged...

* skip env check for tests

* defer database operations until after Laravel is booted.

* don't include dbFaciale...

* redundant use
  • Loading branch information
murrant authored Mar 13, 2019
1 parent 17b5d7f commit cb00521
Show file tree
Hide file tree
Showing 43 changed files with 460 additions and 278 deletions.
2 changes: 1 addition & 1 deletion LibreNMS/Authentication/SSOAuthorizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
namespace LibreNMS\Authentication;

use LibreNMS\Config;
use LibreNMS\Util\IP;
use LibreNMS\Exceptions\AuthenticationException;
use LibreNMS\Exceptions\InvalidIpException;
use LibreNMS\Util\IP;

/**
* Some functionality in this mechanism is inspired by confluence_http_authenticator (@chauth) and graylog-plugin-auth-sso (@Graylog)
Expand Down
21 changes: 8 additions & 13 deletions LibreNMS/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,19 +533,14 @@ public static function getDatabaseSettings()
}

// Check for testing database
if (getenv('DBTEST')) {
if (isset($config['test_db_name'])) {
putenv('DB_DATABASE=' . $config['test_db_name']);
$config['db_name'] = $config['test_db_name'];
}
if (isset($config['test_db_user'])) {
putenv('DB_USERNAME=' . $config['test_db_user']);
$config['db_user'] = $config['test_db_user'];
}
if (isset($config['test_db_pass'])) {
putenv('DB_PASSWORD=' . $config['test_db_pass']);
$config['db_pass'] = $config['test_db_pass'];
}
if (isset($config['test_db_name'])) {
putenv('DB_TEST_DATABASE=' . $config['test_db_name']);
}
if (isset($config['test_db_user'])) {
putenv('DB_TEST_USERNAME=' . $config['test_db_user']);
}
if (isset($config['test_db_pass'])) {
putenv('DB_TEST_PASSWORD=' . $config['test_db_pass']);
}

return array_intersect_key($config, $keys); // return only the db settings
Expand Down
15 changes: 8 additions & 7 deletions LibreNMS/DB/Eloquent.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,25 @@

namespace LibreNMS\DB;

use Dotenv\Dotenv;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Events\StatementPrepared;
use Illuminate\Events\Dispatcher;
use LibreNMS\Util\Laravel;

class Eloquent
{
/** @var Capsule static reference to capsule */
private static $capsule;
private static $legacy_listener_installed = false;

public static function boot($options = [])
{
// boot Eloquent outside of Laravel
if (!defined('LARAVEL_START') && class_exists(Capsule::class) && is_null(self::$capsule)) {
if (!Laravel::isBooted() && is_null(self::$capsule)) {
$install_dir = realpath(__DIR__ . '/../../');

(new Dotenv($install_dir))->load();

$db_config = include($install_dir . '/config/database.php');
$settings = $db_config['connections'][$db_config['default']];

Expand Down Expand Up @@ -75,15 +78,14 @@ public static function boot($options = [])

public static function initLegacyListeners()
{
if (self::isConnected() && !self::$legacy_listener_installed) {
if (self::isConnected()) {
// set FETCH_ASSOC for queries that required by setting the global variable $PDO_FETCH_ASSOC (for dbFacile)
self::DB()->getEventDispatcher()->listen(StatementPrepared::class, function ($event) {
global $PDO_FETCH_ASSOC;
if ($PDO_FETCH_ASSOC) {
$event->statement->setFetchMode(\PDO::FETCH_ASSOC);
}
});
self::$legacy_listener_installed = true;
}
}

Expand All @@ -107,8 +109,7 @@ public static function isConnected()
try {
$conn = self::DB();
if ($conn) {
$conn->getPdo();
return true;
return !is_null($conn->getPdo());
}
} catch (\PDOException $e) {
return false;
Expand All @@ -125,7 +126,7 @@ public static function isConnected()
public static function DB()
{
// check if Laravel is booted
if (defined('LARAVEL_START') && class_exists('DB')) {
if (Laravel::isBooted()) {
return \DB::connection();
}

Expand Down
2 changes: 1 addition & 1 deletion LibreNMS/RRD/RrdDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static function make()
* @param int $min Minimum allowed value. null means undefined.
* @param int $max Maximum allowed value. null means undefined.
* @param int $heartbeat Heartbeat for this dataset. Uses the global setting if null.
* @return $this
* @return RrdDefinition
*/
public function addDataset($name, $type, $min = null, $max = null, $heartbeat = null)
{
Expand Down
6 changes: 2 additions & 4 deletions LibreNMS/Snmptrap/Handlers/AuthenticationFailure.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
use App\Models\Device;
use LibreNMS\Interfaces\SnmptrapHandler;
use LibreNMS\Snmptrap\Trap;
use Log;

class AuthenticationFailure implements SnmptrapHandler
{

/**
* Handle snmptrap.
* Data is pre-parsed and delivered as a Trap.
Expand All @@ -42,8 +42,6 @@ class AuthenticationFailure implements SnmptrapHandler
*/
public function handle(Device $device, Trap $trap)
{
//FIXME added device hostname format helper in some branch, use that when merged
$device_array = $device->toArray();
log_event('SNMP Trap: Authentication Failure: ' . format_hostname($device_array), $device_array, 'auth', 3, $device->hostname);
Log::event('SNMP Trap: Authentication Failure: ' . $device->displayName(), $device->device_id, 'auth', 3);
}
}
2 changes: 1 addition & 1 deletion LibreNMS/Snmptrap/Handlers/BgpBackwardTransition.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function handle(Device $device, Trap $trap)
$bgpPeer->bgpPeerState = $trap->getOidData($state_oid);

if ($bgpPeer->isDirty('bgpPeerState')) {
log_event('SNMP Trap: BGP Down ' . $bgpPeer->bgpPeerIdentifier . ' ' . get_astext($bgpPeer->bgpPeerRemoteAs) . ' is now ' . $bgpPeer->bgpPeerState, $device->toArray(), 'bgpPeer', 5, $bgpPeerIp);
\Log::event('SNMP Trap: BGP Down ' . $bgpPeer->bgpPeerIdentifier . ' ' . get_astext($bgpPeer->bgpPeerRemoteAs) . ' is now ' . $bgpPeer->bgpPeerState, $device->device_id, 'bgpPeer', 5, $bgpPeerIp);
}

$bgpPeer->save();
Expand Down
2 changes: 1 addition & 1 deletion LibreNMS/Snmptrap/Handlers/BgpEstablished.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function handle(Device $device, Trap $trap)
$bgpPeer->bgpPeerState = $trap->getOidData($state_oid);

if ($bgpPeer->isDirty('bgpPeerState')) {
log_event('SNMP Trap: BGP Up ' . $bgpPeer->bgpPeerIdentifier . ' ' . get_astext($bgpPeer->bgpPeerRemoteAs) . ' is now ' . $bgpPeer->bgpPeerState, $device->toArray(), 'bgpPeer', 1, $bgpPeerIp);
Log::event('SNMP Trap: BGP Up ' . $bgpPeer->bgpPeerIdentifier . ' ' . get_astext($bgpPeer->bgpPeerRemoteAs) . ' is now ' . $bgpPeer->bgpPeerState, $device->device_id, 'bgpPeer', 1, $bgpPeerIp);
}

$bgpPeer->save();
Expand Down
4 changes: 2 additions & 2 deletions LibreNMS/Snmptrap/Handlers/EquipStatusTrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
use App\Models\Device;
use LibreNMS\Interfaces\SnmptrapHandler;
use LibreNMS\Snmptrap\Trap;
use Log;

class EquipStatusTrap implements SnmptrapHandler
{

/**
* Handle snmptrap.
* Data is pre-parsed and delivered as a Trap.
Expand All @@ -45,7 +45,7 @@ public function handle(Device $device, Trap $trap)
$state = $trap->getOidData('EQUIPMENT-MIB::equipStatus.0');

$severity = $this->getSeverity($state);
log_event('SNMP Trap: Equipment Status ' . $state, $device->toArray(), 'state', $severity);
Log::event('SNMP Trap: Equipment Status ' . $state, $device->device_id, 'state', $severity);
}

private function getSeverity($state)
Expand Down
8 changes: 3 additions & 5 deletions LibreNMS/Snmptrap/Handlers/LinkDown.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,14 @@ public function handle(Device $device, Trap $trap)
$port->ifOperStatus = $trap->getOidData("IF-MIB::ifOperStatus.$ifIndex");
$port->ifAdminStatus = $trap->getOidData("IF-MIB::ifAdminStatus.$ifIndex");

$device_array = $device->toArray();

log_event("SNMP Trap: linkDown $port->ifAdminStatus/$port->ifOperStatus " . $port->ifDescr, $device_array, 'interface', 5, $port->port_id);
Log::event("SNMP Trap: linkDown $port->ifAdminStatus/$port->ifOperStatus " . $port->ifDescr, $device->device_id, 'interface', 5, $port->port_id);

if ($port->isDirty('ifAdminStatus')) {
log_event("Interface Disabled : " . $port['ifDescr'] . " (TRAP)", $device_array, "interface", 3, $port['port_id']);
Log::event("Interface Disabled : $port->ifDescr (TRAP)", $device->device_id, "interface", 3, $port->port_id);
}

if ($port->isDirty('ifOperStatus')) {
log_event("Interface went Down : " . $port['ifDescr'] . " (TRAP)", $device_array, "interface", 5, $port['port_id']);
Log::event("Interface went Down : $port->ifDescr (TRAP)", $device->device_id, "interface", 5, $port->port_id);
}

$port->save();
Expand Down
8 changes: 3 additions & 5 deletions LibreNMS/Snmptrap/Handlers/LinkUp.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,14 @@ public function handle(Device $device, Trap $trap)
$port->ifOperStatus = $trap->getOidData("IF-MIB::ifAdminStatus.$ifIndex");
$port->ifAdminStatus = $trap->getOidData("IF-MIB::ifOperStatus.$ifIndex");

$device_array = $device->toArray();

log_event("SNMP Trap: linkUp $port->ifAdminStatus/$port->ifOperStatus " . $port->ifDescr, $device_array, "interface", 1, $port->port_id);
Log::event("SNMP Trap: linkUp $port->ifAdminStatus/$port->ifOperStatus " . $port->ifDescr, $device->device_id, "interface", 1, $port->port_id);

if ($port->isDirty('ifAdminStatus')) {
log_event("Interface Enabled : $port->ifDescr (TRAP)", $device_array, "interface", 3, $port->port_id);
Log::event("Interface Enabled : $port->ifDescr (TRAP)", $device->device_id, "interface", 3, $port->port_id);
}

if ($port->isDirty('ifOperStatus')) {
log_event("Interface went Up : $port->ifDescr (TRAP)", $device_array, "interface", 1, $port->port_id);
Log::event("Interface went Up : $port->ifDescr (TRAP)", $device->device_id, "interface", 1, $port->port_id);
}

$port->save();
Expand Down
3 changes: 2 additions & 1 deletion LibreNMS/Snmptrap/Handlers/LogTrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use App\Models\Device;
use LibreNMS\Interfaces\SnmptrapHandler;
use LibreNMS\Snmptrap\Trap;
use Log;

class LogTrap implements SnmptrapHandler
{
Expand All @@ -52,7 +53,7 @@ public function handle(Device $device, Trap $trap)
$state = $trap->getOidData('LOG-MIB::logEquipStatusV2.'.$index);

$severity = $this->getSeverity($state);
log_event('SNMP Trap: Log '.$logName.' '.$logEvent.' '.$logPC.' '.$logAI.' '.$state, $device->toArray(), 'log', $severity);
Log::event('SNMP Trap: Log '.$logName.' '.$logEvent.' '.$logPC.' '.$logAI.' '.$state, $device->device_id, 'log', $severity);
}

private function getSeverity($state)
Expand Down
3 changes: 1 addition & 2 deletions LibreNMS/Snmptrap/Handlers/UpsmgUtilityFailure.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public function handle(Device $device, Trap $trap)
}
$sensor->sensor_current = 1;
$sensor->save();
$device_array = $device->toArray();
log_event("UPS power failed, state sensor " . $sensor->sensor_descr . " has changed to ".$sensor->sensor_current . ".", $device_array, "Power", 5);
Log::event("UPS power failed, state sensor " . $sensor->sensor_descr . " has changed to ".$sensor->sensor_current . ".", $device->device_id, "Power", 5);
}
}
3 changes: 1 addition & 2 deletions LibreNMS/Snmptrap/Handlers/UpsmgUtilityRestored.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public function handle(Device $device, Trap $trap)
}
$sensor->sensor_current = 2;
$sensor->save();
$device_array = $device->toArray();
log_event("UPS power restored, state sensor " . $sensor->sensor_descr . " has changed to ".$sensor->sensor_current . ".", $device_array, "Power", 1);
Log::event("UPS power restored, state sensor " . $sensor->sensor_descr . " has changed to ".$sensor->sensor_current . ".", $device->device_id, "Power", 1);
}
}
14 changes: 9 additions & 5 deletions LibreNMS/Util/Laravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

use App;
use Illuminate\Database\Events\QueryExecuted;
use LibreNMS\Config;
use LibreNMS\DB\Eloquent;
use Log;

Expand All @@ -36,7 +35,7 @@ class Laravel
public static function bootCli()
{
// make sure Laravel isn't already booted
if (class_exists('App') && App::isBooted()) {
if (self::isBooted()) {
return;
}

Expand All @@ -47,6 +46,11 @@ public static function bootCli()
$kernel->bootstrap();
}

public static function isBooted()
{
return !empty(app()->isAlias('Illuminate\Foundation\Application')) && app()->isBooted();
}

public static function enableQueryDebug()
{
$db = Eloquent::DB();
Expand All @@ -62,7 +66,7 @@ public static function enableQueryDebug()
return $item;
})->toJson();

if (class_exists('Log')) {
if (self::isBooted()) {
Log::debug("SQL[%Y{$query->sql} %y$bindings%n {$query->time}ms] \n", ['color' => true]);
} else {
c_echo("SQL[%Y{$query->sql} %y$bindings%n {$query->time}ms] \n");
Expand All @@ -83,14 +87,14 @@ public static function disableQueryDebug()

public static function enableCliDebugOutput()
{
if (class_exists('\Log') && App::runningInConsole()) {
if (self::isBooted() && App::runningInConsole()) {
Log::setDefaultDriver('console');
}
}

public static function disableCliDebugOutput()
{
if (class_exists('Log')) {
if (self::isBooted()) {
Log::setDefaultDriver('logfile');
}
}
Expand Down
53 changes: 53 additions & 0 deletions app/Facades/LogManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* Log.php
*
* Extending the built in logging to add an event logger function
*
* 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/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2019 Tony Murray
* @author Tony Murray <[email protected]>
*/

namespace App\Facades;

use Auth;

class LogManager extends \Illuminate\Log\LogManager
{
/**
* Log events to the event table
*
* @param string $text message describing the event
* @param \App\Models\Device|int $device device array or device_id
* @param string $type brief category for this event. Examples: sensor, state, stp, system, temperature, interface
* @param int $severity 1: ok, 2: info, 3: notice, 4: warning, 5: critical, 0: unknown
* @param int $reference the id of the referenced entity. Supported types: interface
*/
public function event($text, $device = null, $type = null, $severity = 2, $reference = null)
{
(new \App\Models\Eventlog([
'device_id' => $device instanceof \App\Models\Device ? $device->device_id : $device,
'reference' => $reference,
'type' => $type,
'datetime' => \Carbon\Carbon::now(),
'severity' => $severity,
'message' => $text,
'username' => Auth::user() ? Auth::user()->username : '',
]))->save();
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Ajax/NetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
namespace App\Http\Controllers\Ajax;

use App\Http\Controllers\Controller;
use Config;
use \LibreNMS\Config;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Process\Process;
Expand Down
3 changes: 0 additions & 3 deletions app/Listeners/AuthEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

namespace App\Listeners;

use App\Checks;
use App\Events\Event;
use App\Models\User;
use DB;
use Illuminate\Auth\Events\Login;
use Illuminate\Auth\Events\Logout;
use LibreNMS\Authentication\LegacyAuth;
use Request;
use Session;
use Toastr;

class AuthEventListener
Expand Down
Loading

0 comments on commit cb00521

Please sign in to comment.