Issues (2963)

app/Http/Controllers/Table/EventlogController.php (1 issue)

1
<?php
2
/**
3
 * EventlogController.php
4
 *
5
 * -Description-
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 *
20
 * @link       https://www.librenms.org
21
 *
22
 * @copyright  2018 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace App\Http\Controllers\Table;
27
28
use App\Models\Eventlog;
29
use Carbon\Carbon;
30
use LibreNMS\Config;
31
use LibreNMS\Enum\Alert;
32
use LibreNMS\Util\Url;
33
34
class EventlogController extends TableController
35
{
36
    public function rules()
37
    {
38
        return [
39
            'device' => 'nullable|int',
40
            'device_group' => 'nullable|int',
41
            'eventtype' => 'nullable|string',
42
        ];
43
    }
44
45
    public function searchFields($request)
46
    {
47
        return ['message'];
48
    }
49
50
    protected function filterFields($request)
51
    {
52
        return [
53
            'device_id' => 'device',
54
            'type' => 'eventtype',
55
        ];
56
    }
57
58
    protected function sortFields($request)
59
    {
60
        return ['datetime', 'type', 'device_id', 'message', 'username'];
61
    }
62
63
    /**
64
     * Defines the base query for this resource
65
     *
66
     * @param  \Illuminate\Http\Request  $request
67
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder
68
     */
69
    public function baseQuery($request)
70
    {
71
        return Eventlog::hasAccess($request->user())
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\Eventl...ion(...) { /* ... */ }) returns the type App\Models\Eventlog which is incompatible with the documented return type Illuminate\Database\Eloq...\Database\Query\Builder.
Loading history...
72
            ->with('device')
73
            ->when($request->device_group, function ($query) use ($request) {
74
                $query->inDeviceGroup($request->device_group);
75
            });
76
    }
77
78
    /**
79
     * @param  Eventlog  $eventlog
80
     */
81
    public function formatItem($eventlog)
82
    {
83
        return [
84
            'datetime' => $this->formatDatetime($eventlog),
85
            'device_id' => $eventlog->device ? Url::deviceLink($eventlog->device, $eventlog->device->shortDisplayName()) : null,
86
            'type' => $this->formatType($eventlog),
87
            'message' => htmlspecialchars($eventlog->message),
88
            'username' => $eventlog->username ?: 'System',
89
        ];
90
    }
91
92
    private function formatType($eventlog)
93
    {
94
        if ($eventlog->type == 'interface') {
95
            if (is_numeric($eventlog->reference)) {
96
                $port = $eventlog->related;
97
                if (isset($port)) {
98
                    return '<b>' . Url::portLink($port, $port->getShortLabel()) . '</b>';
99
                }
100
            }
101
        } elseif ($eventlog->type == 'stp') {
102
            return Url::deviceLink($eventlog->device, $eventlog->type, ['tab' => 'stp']);
103
        } elseif (in_array($eventlog->type, \App\Models\Sensor::getTypes())) {
104
            if (is_numeric($eventlog->reference)) {
105
                $sensor = $eventlog->related;
106
                if (isset($sensor)) {
107
                    return '<b>' . Url::sensorLink($sensor, $sensor->sensor_descr) . '</b>';
108
                }
109
            }
110
        }
111
112
        return $eventlog->type;
113
    }
114
115
    private function formatDatetime($eventlog)
116
    {
117
        $output = "<span class='alert-status ";
118
        $output .= $this->severityLabel($eventlog->severity);
119
        $output .= " eventlog-status'></span><span style='display:inline;'>";
120
        $output .= (new Carbon($eventlog->datetime))->format(Config::get('dateformat.compact'));
121
        $output .= '</span>';
122
123
        return $output;
124
    }
125
126
    /**
127
     * @param  int  $eventlog_severity
128
     * @return string $eventlog_severity_icon
129
     */
130
    private function severityLabel($eventlog_severity)
131
    {
132
        switch ($eventlog_severity) {
133
            case Alert::OK:
134
                return 'label-success'; //OK
135
            case Alert::INFO:
136
                return 'label-info'; //Informational
137
            case Alert::NOTICE:
138
                return 'label-primary'; //Notice
139
            case Alert::WARNING:
140
                return 'label-warning'; //Warning
141
            case Alert::ERROR:
142
                return 'label-danger'; //Critical
143
            default:
144
                return 'label-default'; //Unknown
145
        }
146
    }
147
148
    // end eventlog_severity
149
}
150