1 | <?php |
||
2 | |||
3 | namespace App\Http\Controllers; |
||
4 | |||
5 | use App\Models\BgpPeer; |
||
6 | use App\Models\Dashboard; |
||
7 | use App\Models\Device; |
||
8 | use App\Models\Port; |
||
9 | use App\Models\Service; |
||
10 | use App\Models\Syslog; |
||
11 | use App\Models\User; |
||
12 | use App\Models\UserPref; |
||
13 | use App\Models\Widget; |
||
14 | use Illuminate\Http\Request; |
||
15 | use Illuminate\Support\Facades\Auth; |
||
16 | use LibreNMS\Config; |
||
17 | use Toastr; |
||
18 | |||
19 | class OverviewController extends Controller |
||
20 | { |
||
21 | public function index(Request $request) |
||
22 | { |
||
23 | $request->validate([ |
||
24 | 'dashboard' => 'integer', |
||
25 | 'bare' => 'nullable|in:yes', |
||
26 | ]); |
||
27 | |||
28 | $view = Config::get('front_page'); |
||
29 | |||
30 | if (view()->exists("overview.custom.$view")) { |
||
31 | return view("overview.custom.$view"); |
||
32 | } elseif (method_exists($this, $view)) { |
||
33 | return $this->{$view}($request); |
||
34 | } |
||
35 | |||
36 | return $this->default($request); |
||
37 | } |
||
38 | |||
39 | public function default(Request $request) |
||
40 | { |
||
41 | $user = Auth::user(); |
||
42 | $dashboards = Dashboard::allAvailable($user)->with('user:user_id,username')->get()->keyBy('dashboard_id'); |
||
43 | |||
44 | // Split dashboards into user owned or shared |
||
45 | [$user_dashboards, $shared_dashboards] = $dashboards->partition(function ($dashboard) use ($user) { |
||
46 | return $dashboard->user_id == $user->user_id; |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
47 | }); |
||
48 | |||
49 | if (! empty($request->dashboard) && isset($dashboards[$request->dashboard])) { |
||
50 | // specific dashboard |
||
51 | $dashboard = $dashboards[$request->dashboard]; |
||
52 | } else { |
||
53 | $user_default_dash = (int) UserPref::getPref($user, 'dashboard'); |
||
54 | $global_default = (int) Config::get('webui.default_dashboard_id'); |
||
55 | |||
56 | // load user default |
||
57 | if (isset($dashboards[$user_default_dash])) { |
||
58 | $dashboard = $dashboards[$user_default_dash]; |
||
59 | // load global default |
||
60 | } elseif (isset($dashboards[$global_default])) { |
||
61 | $dashboard = $dashboards[$global_default]; |
||
62 | // load users first dashboard |
||
63 | } elseif (! empty($user_dashboards)) { |
||
64 | $dashboard = $user_dashboards->first(); |
||
65 | } |
||
66 | |||
67 | // specific dashboard was requested, but doesn't exist |
||
68 | if (isset($dashboard) && ! empty($request->dashboard)) { |
||
69 | Toastr::error( |
||
70 | "Dashboard <code>#$request->dashboard</code> does not exist! Loaded <code> |
||
71 | " . htmlentities($dashboard->dashboard_name) . '</code> instead.', |
||
72 | 'Requested Dashboard Not Found!' |
||
73 | ); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | if (! isset($dashboard)) { |
||
78 | $dashboard = Dashboard::create([ |
||
79 | 'dashboard_name' => 'Default', |
||
80 | 'user_id' => $user->user_id, |
||
0 ignored issues
–
show
|
|||
81 | ]); |
||
82 | } |
||
83 | |||
84 | $data = $dashboard |
||
85 | ->widgets() |
||
86 | ->select(['user_widget_id', 'users_widgets.widget_id', 'title', 'widget', 'col', 'row', 'size_x', 'size_y', 'refresh', 'settings']) |
||
87 | ->join('widgets', 'widgets.widget_id', '=', 'users_widgets.widget_id') |
||
88 | ->get(); |
||
89 | |||
90 | if ($data->isEmpty()) { |
||
91 | $data[] = ['user_widget_id'=>'0', |
||
92 | 'widget_id'=>1, |
||
93 | 'title'=>'Add a widget', |
||
94 | 'widget'=>'placeholder', |
||
95 | 'col'=>1, |
||
96 | 'row'=>1, |
||
97 | 'size_x'=>6, |
||
98 | 'size_y'=>2, |
||
99 | 'refresh'=>60, |
||
100 | ]; |
||
101 | } |
||
102 | |||
103 | $bare = $request->bare; |
||
104 | $data = serialize(json_encode($data)); |
||
105 | $dash_config = unserialize($data); |
||
106 | $hide_dashboard_editor = UserPref::getPref($user, 'hide_dashboard_editor'); |
||
107 | $widgets = Widget::select('widget_id', 'widget_title')->orderBy('widget_title')->get(); |
||
108 | |||
109 | $user_list = []; |
||
110 | if ($user->can('manage', User::class)) { |
||
111 | $user_list = User::select(['username', 'user_id']) |
||
112 | ->where('user_id', '!=', $user->user_id) |
||
113 | ->orderBy('username') |
||
114 | ->get(); |
||
115 | } |
||
116 | |||
117 | return view('overview.default', compact('bare', 'dash_config', 'dashboard', 'hide_dashboard_editor', 'user_dashboards', 'shared_dashboards', 'widgets', 'user_list')); |
||
118 | } |
||
119 | |||
120 | public function simple(Request $request) |
||
121 | { |
||
122 | //TODO: All below missing D.ignore = '0' check |
||
123 | $ports_down = []; |
||
124 | $bgp_down = []; |
||
125 | $devices_uptime = []; |
||
126 | $syslog = []; |
||
127 | |||
128 | $devices_down = Device::hasAccess(Auth::user()) |
||
129 | ->isDown() |
||
130 | ->limit(Config::get('front_page_down_box_limit')) |
||
131 | ->get(); |
||
132 | |||
133 | if (Config::get('warn.ifdown')) { |
||
134 | $ports_down = Port::hasAccess(Auth::user()) |
||
135 | ->isDown() |
||
136 | ->limit(Config::get('front_page_down_box_limit')) |
||
137 | ->with('device') |
||
138 | ->get(); |
||
139 | } |
||
140 | |||
141 | $services_down = Service::hasAccess(Auth::user()) |
||
142 | ->isCritical() |
||
143 | ->limit(Config::get('front_page_down_box_limit')) |
||
144 | ->with('device') |
||
145 | ->get(); |
||
146 | |||
147 | // TODO: is inAlarm() equal to: bgpPeerAdminStatus != 'start' AND bgpPeerState != 'established' AND bgpPeerState != '' ? |
||
148 | if (Config::get('enable_bgp')) { |
||
149 | $bgp_down = BgpPeer::hasAccess(Auth::user()) |
||
150 | ->inAlarm() |
||
151 | ->limit(Config::get('front_page_down_box_limit')) |
||
152 | ->with('device') |
||
153 | ->get(); |
||
154 | } |
||
155 | |||
156 | if (filter_var(Config::get('uptime_warning'), FILTER_VALIDATE_FLOAT) !== false |
||
157 | && Config::get('uptime_warning') > 0 |
||
158 | ) { |
||
159 | $devices_uptime = Device::hasAccess(Auth::user()) |
||
160 | ->isUp() |
||
161 | ->whereUptime(Config::get('uptime_warning')) |
||
162 | ->limit(Config::get('front_page_down_box_limit')) |
||
163 | ->get(); |
||
164 | |||
165 | $devices_uptime = $devices_uptime->reject(function ($device) { |
||
166 | return Config::getOsSetting($device->os, 'bad_uptime') == true; |
||
167 | }); |
||
168 | } |
||
169 | |||
170 | if (Config::get('enable_syslog')) { |
||
171 | $syslog = Syslog::hasAccess(Auth::user()) |
||
172 | ->orderBy('timestamp', 'desc') |
||
173 | ->limit(20) |
||
174 | ->with('device') |
||
175 | ->get(); |
||
176 | } |
||
177 | |||
178 | return view('overview.simple', compact('devices_down', 'ports_down', 'services_down', 'bgp_down', 'devices_uptime', 'syslog')); |
||
179 | } |
||
180 | } |
||
181 |