Issues (2963)

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

1
<?php
2
/**
3
 * SyslogController.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\Syslog;
29
30
class SyslogController extends TableController
31
{
32
    public function rules()
33
    {
34
        return [
35
            'device' => 'nullable|int',
36
            'device_group' => 'nullable|int',
37
            'program' => 'nullable|string',
38
            'priority' => 'nullable|string',
39
            'to' => 'nullable|date',
40
            'from' => 'nullable|date',
41
            'level' => 'nullable|string',
42
        ];
43
    }
44
45
    public function searchFields($request)
46
    {
47
        return ['msg'];
48
    }
49
50
    public function filterFields($request)
51
    {
52
        return [
53
            'device_id' => 'device',
54
            'program' => 'program',
55
            'priority' => 'priority',
56
        ];
57
    }
58
59
    public function sortFields($request)
60
    {
61
        return ['label', 'timestamp', 'level', 'device_id', 'program', 'msg', 'priority'];
62
    }
63
64
    /**
65
     * Defines the base query for this resource
66
     *
67
     * @param  \Illuminate\Http\Request  $request
68
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder
69
     */
70
    public function baseQuery($request)
71
    {
72
        return Syslog::hasAccess($request->user())
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\Syslog...ion(...) { /* ... */ }) also could return the type Illuminate\Database\Eloquent\Model which is incompatible with the documented return type Illuminate\Database\Eloq...\Database\Query\Builder.
Loading history...
73
            ->with('device')
74
            ->when($request->device_group, function ($query) use ($request) {
75
                $query->inDeviceGroup($request->device_group);
76
            })
77
            ->when($request->from, function ($query) use ($request) {
78
                $query->where('timestamp', '>=', $request->from);
79
            })
80
            ->when($request->to, function ($query) use ($request) {
81
                $query->where('timestamp', '<=', $request->to);
82
            })
83
            ->when($request->level, function ($query) use ($request) {
84
                $query->where('level', '<=', $request->level);
85
            });
86
    }
87
88
    /**
89
     * @param  Syslog  $syslog
90
     */
91
    public function formatItem($syslog)
92
    {
93
        $device = $syslog->device;
94
95
        return [
96
            'label' => $this->setLabel($syslog),
97
            'timestamp' => $syslog->timestamp,
98
            'level' => htmlentities($syslog->level),
99
            'device_id' => $device ? \LibreNMS\Util\Url::deviceLink($device, $device->shortDisplayName()) : '',
100
            'program' => htmlentities($syslog->program),
101
            'msg' => htmlentities($syslog->msg),
102
            'priority' => htmlentities($syslog->priority),
103
        ];
104
    }
105
106
    private function setLabel($syslog)
107
    {
108
        $output = "<span class='alert-status ";
109
        $output .= $this->priorityLabel($syslog->priority);
110
        $output .= "'>";
111
        $output .= '</span>';
112
113
        return $output;
114
    }
115
116
    /**
117
     * @param  int  $syslog_priority
118
     * @return string
119
     */
120
    private function priorityLabel($syslog_priority)
121
    {
122
        switch ($syslog_priority) {
123
            case 'debug':
124
                return 'label-default'; //Debug
125
            case 'info':
126
                return 'label-info'; //Informational
127
            case 'notice':
128
                return 'label-primary'; //Notice
129
            case 'warning':
130
                return 'label-warning'; //Warning
131
            case 'err':
132
                return 'label-danger'; //Error
133
            case 'crit':
134
                return 'label-danger'; //Critical
135
            case 'alert':
136
                return 'label-danger'; //Alert
137
            case 'emerg':
138
                return 'label-danger'; //Emergency
139
            default:
140
                return '';
141
        }
142
    }
143
144
    // end syslog_priority
145
}
146