diff --git a/LibreNMS/IRCBot.php b/LibreNMS/IRCBot.php index e864ac5286..5f9a102448 100644 --- a/LibreNMS/IRCBot.php +++ b/LibreNMS/IRCBot.php @@ -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']); } diff --git a/app/Models/Device.php b/app/Models/Device.php index 9ea78812eb..569871680d 100644 --- a/app/Models/Device.php +++ b/app/Models/Device.php @@ -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() diff --git a/app/Models/Eventlog.php b/app/Models/Eventlog.php index 2488ebf33a..c09eddefcb 100644 --- a/app/Models/Eventlog.php +++ b/app/Models/Eventlog.php @@ -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 ---- diff --git a/html/includes/api_functions.inc.php b/html/includes/api_functions.inc.php index 63be15a7a3..3afefacf5f 100644 --- a/html/includes/api_functions.inc.php +++ b/html/includes/api_functions.inc.php @@ -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"; diff --git a/html/includes/print-event.inc.php b/html/includes/print-event.inc.php index 2f639ec1eb..f97c690c3c 100644 --- a/html/includes/print-event.inc.php +++ b/html/includes/print-event.inc.php @@ -15,7 +15,7 @@ * @author LibreNMS Contributors */ -$hostname = gethostbyid($entry['host']); +$hostname = gethostbyid($entry['device_id']); unset($icon); @@ -27,7 +27,7 @@ echo '
Timestamp | Port | Event | '; diff --git a/includes/functions.php b/includes/functions.php index 9f30932c2b..ed070a9208 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -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) diff --git a/misc/db_schema.yaml b/misc/db_schema.yaml index 80ee637fff..f1ea1d29d1 100644 --- a/misc/db_schema.yaml +++ b/misc/db_schema.yaml @@ -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: diff --git a/scripts/console-ui.php b/scripts/console-ui.php index a083504f3c..3b8c5d3ae8 100755 --- a/scripts/console-ui.php +++ b/scripts/console-ui.php @@ -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(); diff --git a/sql-schema/280.sql b/sql-schema/280.sql new file mode 100644 index 0000000000..1fe1f263e1 --- /dev/null +++ b/sql-schema/280.sql @@ -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;
---|