2018-09-20 15:33:03 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* SyslogController.php
|
|
|
|
*
|
|
|
|
* -Description-
|
|
|
|
*
|
|
|
|
* 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
|
2021-02-09 00:29:04 +01:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-09-20 15:33:03 -05:00
|
|
|
*
|
2021-02-09 00:29:04 +01:00
|
|
|
* @link https://www.librenms.org
|
2021-09-10 20:09:53 +02:00
|
|
|
*
|
2018-09-20 15:33:03 -05:00
|
|
|
* @copyright 2018 Tony Murray
|
|
|
|
* @author Tony Murray <murraytony@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Table;
|
|
|
|
|
|
|
|
use App\Models\Syslog;
|
2021-10-29 03:32:36 -05:00
|
|
|
use LibreNMS\Enum\SyslogSeverity;
|
2018-09-20 15:33:03 -05:00
|
|
|
|
|
|
|
class SyslogController extends TableController
|
|
|
|
{
|
|
|
|
public function rules()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'device' => 'nullable|int',
|
2019-08-08 02:59:14 +02:00
|
|
|
'device_group' => 'nullable|int',
|
2018-09-20 15:33:03 -05:00
|
|
|
'program' => 'nullable|string',
|
|
|
|
'priority' => 'nullable|string',
|
|
|
|
'to' => 'nullable|date',
|
|
|
|
'from' => 'nullable|date',
|
2021-08-21 05:52:15 +02:00
|
|
|
'level' => 'nullable|string',
|
2018-09-20 15:33:03 -05:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-01-14 14:00:05 -06:00
|
|
|
public function searchFields($request)
|
|
|
|
{
|
|
|
|
return ['msg'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function filterFields($request)
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'device_id' => 'device',
|
|
|
|
'program' => 'program',
|
|
|
|
'priority' => 'priority',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-06-15 09:50:21 -05:00
|
|
|
public function sortFields($request)
|
|
|
|
{
|
|
|
|
return ['label', 'timestamp', 'level', 'device_id', 'program', 'msg', 'priority'];
|
|
|
|
}
|
|
|
|
|
2018-09-20 15:33:03 -05:00
|
|
|
/**
|
|
|
|
* Defines the base query for this resource
|
|
|
|
*
|
2021-09-08 23:35:56 +02:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2018-09-20 15:33:03 -05:00
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder
|
|
|
|
*/
|
|
|
|
public function baseQuery($request)
|
|
|
|
{
|
2019-08-08 02:59:14 +02:00
|
|
|
return Syslog::hasAccess($request->user())
|
|
|
|
->with('device')
|
2021-10-29 03:32:36 -05:00
|
|
|
->when($request->device_group, function ($query, $group) {
|
|
|
|
$query->inDeviceGroup($group);
|
2019-08-08 02:59:14 +02:00
|
|
|
})
|
2021-10-29 03:32:36 -05:00
|
|
|
->when($request->from, function ($query, $from) {
|
|
|
|
$query->where('timestamp', '>=', $from);
|
2019-08-08 02:59:14 +02:00
|
|
|
})
|
2021-10-29 03:32:36 -05:00
|
|
|
->when($request->to, function ($query, $to) {
|
|
|
|
$query->where('timestamp', '<=', $to);
|
2021-08-21 05:52:15 +02:00
|
|
|
})
|
2021-10-29 03:32:36 -05:00
|
|
|
->when($request->level, function ($query, $level) {
|
|
|
|
if ($level >= 7) {
|
|
|
|
return; // include everything
|
|
|
|
}
|
|
|
|
|
|
|
|
$levels = array_slice(SyslogSeverity::LEVELS, 0, $level + 1);
|
|
|
|
$query->whereIn('level', $levels);
|
2019-08-08 02:59:14 +02:00
|
|
|
});
|
2018-09-20 15:33:03 -05:00
|
|
|
}
|
|
|
|
|
2021-03-30 11:16:44 +02:00
|
|
|
/**
|
2021-09-08 23:35:56 +02:00
|
|
|
* @param Syslog $syslog
|
2021-03-30 11:16:44 +02:00
|
|
|
*/
|
2018-09-20 15:33:03 -05:00
|
|
|
public function formatItem($syslog)
|
|
|
|
{
|
|
|
|
$device = $syslog->device;
|
|
|
|
|
|
|
|
return [
|
2020-06-05 05:25:34 +02:00
|
|
|
'label' => $this->setLabel($syslog),
|
2018-09-20 15:33:03 -05:00
|
|
|
'timestamp' => $syslog->timestamp,
|
2018-09-21 08:22:15 -05:00
|
|
|
'level' => htmlentities($syslog->level),
|
2018-09-20 15:33:03 -05:00
|
|
|
'device_id' => $device ? \LibreNMS\Util\Url::deviceLink($device, $device->shortDisplayName()) : '',
|
2018-09-21 07:33:34 -05:00
|
|
|
'program' => htmlentities($syslog->program),
|
|
|
|
'msg' => htmlentities($syslog->msg),
|
2018-09-21 08:22:15 -05:00
|
|
|
'priority' => htmlentities($syslog->priority),
|
2018-09-20 15:33:03 -05:00
|
|
|
];
|
|
|
|
}
|
2020-06-05 05:25:34 +02:00
|
|
|
|
|
|
|
private function setLabel($syslog)
|
|
|
|
{
|
|
|
|
$output = "<span class='alert-status ";
|
|
|
|
$output .= $this->priorityLabel($syslog->priority);
|
|
|
|
$output .= "'>";
|
|
|
|
$output .= '</span>';
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-09-08 23:35:56 +02:00
|
|
|
* @param int $syslog_priority
|
2021-04-20 12:47:22 +02:00
|
|
|
* @return string
|
2020-06-05 05:25:34 +02:00
|
|
|
*/
|
|
|
|
private function priorityLabel($syslog_priority)
|
|
|
|
{
|
|
|
|
switch ($syslog_priority) {
|
|
|
|
case 'debug':
|
|
|
|
return 'label-default'; //Debug
|
|
|
|
case 'info':
|
|
|
|
return 'label-info'; //Informational
|
|
|
|
case 'notice':
|
|
|
|
return 'label-primary'; //Notice
|
|
|
|
case 'warning':
|
|
|
|
return 'label-warning'; //Warning
|
|
|
|
case 'err':
|
|
|
|
return 'label-danger'; //Error
|
|
|
|
case 'crit':
|
|
|
|
return 'label-danger'; //Critical
|
|
|
|
case 'alert':
|
|
|
|
return 'label-danger'; //Alert
|
|
|
|
case 'emerg':
|
|
|
|
return 'label-danger'; //Emergency
|
2021-04-20 12:47:22 +02:00
|
|
|
default:
|
|
|
|
return '';
|
2020-06-05 05:25:34 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-21 14:54:51 +02:00
|
|
|
|
2020-06-05 05:25:34 +02:00
|
|
|
// end syslog_priority
|
2018-09-20 15:33:03 -05:00
|
|
|
}
|