Eventlog updates (#9633)

* Eventlog updates
remove host from eventlog table, retain in api
allow most fields nullable

* updated schema

* revert auth for legacy log_event function
safer check in the model

* add function modifiers
This commit is contained in:
Tony Murray 2019-01-10 00:52:00 -06:00 committed by GitHub
parent 98f75bd9ce
commit daffa7e274
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 67 additions and 32 deletions

View File

@ -691,13 +691,13 @@ class IRCBot
}
if ($this->user['level'] < 5) {
$tmp = dbFetchRows('SELECT `event_id`,`host`,`datetime`,`message`,`type` FROM `eventlog` WHERE `host` IN ('.implode(',', $this->user['devices']).') ORDER BY `event_id` DESC LIMIT '.mres($num));
$tmp = dbFetchRows('SELECT `event_id`,`device_id`,`datetime`,`message`,`type` FROM `eventlog` WHERE `device_id` IN ('.implode(',', $this->user['devices']).') ORDER BY `event_id` DESC LIMIT '. (int)$num);
} else {
$tmp = dbFetchRows('SELECT `event_id`,`host`,`datetime`,`message`,`type` FROM `eventlog` ORDER BY `event_id` DESC LIMIT '.mres($num));
$tmp = dbFetchRows('SELECT `event_id`,`device_id`,`datetime`,`message`,`type` FROM `eventlog` ORDER BY `event_id` DESC LIMIT '.(int)$num);
}
foreach ($tmp as $device) {
$hostid = dbFetchRow('SELECT `hostname` FROM `devices` WHERE `device_id` = '.$device['host']);
$hostid = dbFetchRow('SELECT `hostname` FROM `devices` WHERE `device_id` = '.$device['device_id']);
$this->respond($device['event_id'].' '.$hostid['hostname'].' '.$device['datetime'].' '.$device['message'].' '.$device['type']);
}

View File

@ -487,7 +487,7 @@ class Device extends BaseModel
public function eventlogs()
{
return $this->hasMany('App\Models\General\Eventlog', 'host', 'device_id');
return $this->hasMany('App\Models\Eventlog', 'device_id', 'device_id');
}
public function groups()

View File

@ -25,11 +25,43 @@
namespace App\Models;
use Carbon\Carbon;
class Eventlog extends BaseModel
{
protected $table = 'eventlog';
protected $primaryKey = 'event_id';
public $timestamps = false;
protected $fillable = ['datetime', 'message', 'type', 'reference', 'username', 'severity'];
// ---- Helper Functions ----
/**
* Log events to the event table
*
* @param string $text message describing the event
* @param Device $device related device
* @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 static function log($text, $device = null, $type = null, $severity = 2, $reference = null)
{
$log = new static([
'reference' => $reference,
'type' => $type,
'datetime' => Carbon::now(),
'severity' => $severity,
'message' => $text,
'username' => (class_exists('\Auth') && \Auth::check()) ? \Auth::user()->username : '',
]);
if ($device instanceof Device) {
$device->eventlogs()->save($log);
} else {
$log->save();
}
}
// ---- Query scopes ----

View File

@ -2150,7 +2150,7 @@ function list_services()
function list_logs()
{
check_is_read();
global $config;
$app = \Slim\Slim::getInstance();
$router = $app->router()->getCurrentRoute()->getParams();
$type = $app->router()->getCurrentRoute()->getName();
@ -2158,6 +2158,7 @@ function list_logs()
$device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname);
if ($type === 'list_eventlog') {
$table = 'eventlog';
$select = '`eventlog`.`device_id` as `host`, `eventlog`.*'; // inject host for backward compat
$timestamp = 'datetime';
} elseif ($type === 'list_syslog') {
$table = 'syslog';
@ -2173,13 +2174,14 @@ function list_logs()
$timestamp = 'datetime';
}
$start = mres($_GET['start']) ?: 0;
$limit = mres($_GET['limit']) ?: 50;
$from = mres($_GET['from']);
$to = mres($_GET['to']);
$start = (int)$_GET['start'] ?: 0;
$limit = (int)$_GET['limit'] ?: 50;
$from = (int)$_GET['from'];
$to = (int)$_GET['to'];
$count_query = 'SELECT COUNT(*)';
$full_query = "SELECT `devices`.`hostname`, `devices`.`sysName`, `$table`.*";
$full_query = "SELECT `devices`.`hostname`, `devices`.`sysName`, ";
$full_query .= isset($select) ? $select : "`$table`.*";
$param = array();
$query = " FROM $table LEFT JOIN `devices` ON `$table`.`device_id`=`devices`.`device_id` WHERE 1";

View File

@ -15,7 +15,7 @@
* @author LibreNMS Contributors
*/
$hostname = gethostbyid($entry['host']);
$hostname = gethostbyid($entry['device_id']);
unset($icon);
@ -27,7 +27,7 @@ echo '<td>'.$icon .'</td>';
echo '<td style="vertical-align: middle;">'.$entry['datetime'].'</td>';
if (!isset($vars['device'])) {
$dev = device_by_id_cache($entry['host']);
$dev = device_by_id_cache($entry['device_id']);
echo '<td style="vertical-align: middle;">'.generate_device_link($dev, shorthost($dev['hostname'])).'</td>';
}

View File

@ -18,8 +18,8 @@ use LibreNMS\Authentication\LegacyAuth;
$where = '1';
if (is_numeric($vars['device'])) {
$where .= ' AND E.host = ?';
$param[] = $vars['device'];
$where .= ' AND E.device_id = ?';
$param[] = (int)$vars['device'];
}
if (!empty($vars['eventtype'])) {
@ -33,9 +33,9 @@ if ($vars['string']) {
}
if (LegacyAuth::user()->hasGlobalRead()) {
$sql = " FROM `eventlog` AS E LEFT JOIN `devices` AS `D` ON `E`.`host`=`D`.`device_id` WHERE $where";
$sql = " FROM `eventlog` AS E LEFT JOIN `devices` AS `D` ON `E`.`device_id`=`D`.`device_id` WHERE $where";
} else {
$sql = " FROM `eventlog` AS E, devices_perms AS P WHERE $where AND E.host = P.device_id AND P.user_id = ?";
$sql = " FROM `eventlog` AS E, devices_perms AS P WHERE $where AND E.device_id = P.device_id AND P.user_id = ?";
$param[] = LegacyAuth::id();
}
@ -67,7 +67,7 @@ if ($rowCount != -1) {
$sql = "SELECT `E`.*,DATE_FORMAT(datetime, '" . $config['dateformat']['mysql']['compact'] . "') as humandate,severity $sql";
foreach (dbFetchRows($sql, $param) as $eventlog) {
$dev = device_by_id_cache($eventlog['host']);
$dev = device_by_id_cache($eventlog['device_id']);
if ($eventlog['type'] == 'interface') {
$this_if = cleanPort(getifbyid($eventlog['reference']));
$type = '<b>' . generate_port_link($this_if, makeshortif(strtolower($this_if['label']))) . '</b>';

View File

@ -9,7 +9,7 @@ echo '<i class="fa fa-bookmark fa-lg icon-theme" aria-hidden="true"></i> <strong
echo ' </div>
<table class="table table-hover table-condensed table-striped">';
$eventlog = dbFetchRows("SELECT *,DATE_FORMAT(datetime, '".$config['dateformat']['mysql']['compact']."') as humandate FROM `eventlog` WHERE `host` = ? ORDER BY `datetime` DESC LIMIT 0,10", array($device['device_id']));
$eventlog = dbFetchRows("SELECT *,DATE_FORMAT(datetime, '".$config['dateformat']['mysql']['compact']."') as humandate FROM `eventlog` WHERE `device_id` = ? ORDER BY `datetime` DESC LIMIT 0,10", [$device['device_id']]);
foreach ($eventlog as $entry) {
include 'includes/print-event-short.inc.php';
}

View File

@ -1,6 +1,6 @@
<?php
$entries = dbFetchRows("SELECT *,DATE_FORMAT(datetime, '".$config['dateformat']['mysql']['compact']."') as humandate FROM `eventlog` WHERE `host` = ? AND `type` = 'interface' AND `reference` = '".$port['port_id']."' ORDER BY `datetime` DESC LIMIT 0,250", array($device['device_id']));
$entries = dbFetchRows("SELECT *,DATE_FORMAT(datetime, '".$config['dateformat']['mysql']['compact']."') as humandate FROM `eventlog` WHERE `device_id` = ? AND `type` = 'interface' AND `reference` = '".$port['port_id']."' ORDER BY `datetime` DESC LIMIT 0,250", [$device['device_id']]);
echo '<table class="table table-condensed">';
echo '<th>Timestamp</th><th>Port</th><th>Event</th>';

View File

@ -941,17 +941,15 @@ function log_event($text, $device = null, $type = null, $severity = 2, $referenc
$device = device_by_id_cache($device);
}
$insert = array('host' => ($device['device_id'] ?: 0),
dbInsert([
'device_id' => ($device['device_id'] ?: 0),
'reference' => ($reference ?: "NULL"),
'type' => ($type ?: "NULL"),
'datetime' => array("NOW()"),
'reference' => $reference,
'type' => $type,
'datetime' => \Carbon\Carbon::now(),
'severity' => $severity,
'message' => $text,
'username' => isset(LegacyAuth::user()->username) ? LegacyAuth::user()->username : '',
);
dbInsert($insert, 'eventlog');
], 'eventlog');
}
// Parse string with emails. Return array with email (as key) and name (as value)

View File

@ -619,17 +619,15 @@ entPhysical_state:
eventlog:
Columns:
- { Field: event_id, Type: 'int(10) unsigned', 'Null': false, Extra: auto_increment }
- { Field: host, Type: int(11), 'Null': false, Extra: '', Default: '0' }
- { Field: device_id, Type: 'int(10) unsigned', 'Null': false, Extra: '' }
- { Field: device_id, Type: 'int(10) unsigned', 'Null': true, Extra: '' }
- { Field: datetime, Type: datetime, 'Null': false, Extra: '', Default: '1970-01-02 00:00:01' }
- { Field: message, Type: text, 'Null': true, Extra: '' }
- { Field: type, Type: varchar(64), 'Null': true, Extra: '' }
- { Field: reference, Type: varchar(64), 'Null': false, Extra: '' }
- { Field: reference, Type: varchar(64), 'Null': true, Extra: '' }
- { Field: username, Type: varchar(128), 'Null': true, Extra: '' }
- { Field: severity, Type: tinyint(4), 'Null': true, Extra: '', Default: '2' }
- { Field: severity, Type: tinyint(4), 'Null': false, Extra: '', Default: '2' }
Indexes:
PRIMARY: { Name: PRIMARY, Columns: [event_id], Unique: true, Type: BTREE }
host: { Name: host, Columns: [host], Unique: false, Type: BTREE }
datetime: { Name: datetime, Columns: [datetime], Unique: false, Type: BTREE }
device_id: { Name: device_id, Columns: [device_id], Unique: false, Type: BTREE }
graph_types:

View File

@ -41,7 +41,7 @@ while ($end == 0) {
$query = "SELECT *,DATE_FORMAT(datetime, '".$config['dateformat']['mysql']['compact']."') as humandate FROM `eventlog` AS E $sql ORDER BY `datetime` DESC LIMIT 20";
foreach (dbFetchRows($query, $param) as $entry) {
$tbl->addRow(array($entry['datetime'], gethostbyid($entry['host']), $entry['message'], $entry['type'], $entry['reference']));
$tbl->addRow(array($entry['datetime'], gethostbyid($entry['device_id']), $entry['message'], $entry['type'], $entry['reference']));
}
echo $tbl->getTable();

5
sql-schema/280.sql Normal file
View File

@ -0,0 +1,5 @@
alter table eventlog modify device_id int unsigned null;
alter table eventlog modify reference varchar(64) null;
alter table eventlog modify severity tinyint default 2 not null;
drop index host on eventlog;
alter table eventlog drop column host;