Files
librenms-librenms/app/Http/Controllers/Widgets/AvailabilityMapController.php
T

189 lines
6.5 KiB
PHP
Raw Normal View History

2018-12-16 15:18:17 -06:00
<?php
/**
* AvailabilityMapController.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-12-16 15:18:17 -06:00
*
2021-02-09 00:29:04 +01:00
* @link https://www.librenms.org
2018-12-16 15:18:17 -06:00
* @copyright 2018 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace App\Http\Controllers\Widgets;
use App\Models\Device;
use App\Models\DeviceGroup;
use App\Models\Service;
use Illuminate\Http\Request;
use LibreNMS\Config;
class AvailabilityMapController extends WidgetController
{
protected $title = 'Availability Map';
public function __construct()
{
$this->defaults = [
'title' => null,
2020-09-21 14:54:51 +02:00
'type' => (int) Config::get('webui.availability_map_compact', 0),
2018-12-16 15:18:17 -06:00
'tile_size' => 12,
'color_only_select' => 0,
'show_disabled_and_ignored' => 0,
'mode_select' => 0,
'order_by' => Config::get('webui.availability_map_sort_status') ? 'status' : 'hostname',
2019-08-08 02:59:14 +02:00
'device_group' => null,
2018-12-16 15:18:17 -06:00
];
}
public function getView(Request $request)
{
$data = $this->getSettings();
$devices = [];
$device_totals = [];
$services = [];
$services_totals = [];
$mode = $data['mode_select'];
if ($mode == 0 || $mode == 2) {
2020-09-21 14:54:51 +02:00
[$devices, $device_totals] = $this->getDevices($request);
2018-12-16 15:18:17 -06:00
}
if ($mode > 0) {
2020-09-21 14:54:51 +02:00
[$services, $services_totals] = $this->getServices($request);
2018-12-16 15:18:17 -06:00
}
$data['device'] = Device::first();
$data['devices'] = $devices;
$data['device_totals'] = $device_totals;
$data['services'] = $services;
$data['services_totals'] = $services_totals;
return view('widgets.availability-map', $data);
}
public function getSettingsView(Request $request)
{
2019-08-08 02:59:14 +02:00
return view('widgets.settings.availability-map', $this->getSettings(true));
2018-12-16 15:18:17 -06:00
}
/**
* @param Request $request
* @return array
*/
private function getDevices(Request $request)
{
$settings = $this->getSettings();
// filter for by device group or show all
2019-08-08 02:59:14 +02:00
if ($settings['device_group']) {
$device_query = DeviceGroup::find($settings['device_group'])->devices()->hasAccess($request->user());
2018-12-16 15:18:17 -06:00
} else {
$device_query = Device::hasAccess($request->user());
}
2020-09-21 14:54:51 +02:00
if (! $settings['show_disabled_and_ignored']) {
$device_query->isNotDisabled();
2018-12-16 15:18:17 -06:00
}
$device_query->orderBy($settings['order_by']);
2021-07-13 16:35:43 -05:00
$devices = $device_query->select(['devices.device_id', 'hostname', 'sysName', 'status', 'uptime', 'last_polled', 'disabled', 'disable_notify', 'location_id'])->get();
2018-12-16 15:18:17 -06:00
// process status
$uptime_warn = Config::get('uptime_warning', 84600);
2020-02-02 15:33:01 +01:00
$totals = ['warn' => 0, 'up' => 0, 'down' => 0, 'maintenance' => 0, 'ignored' => 0, 'disabled' => 0];
2021-07-13 16:35:43 -05:00
$data = [];
2018-12-16 15:18:17 -06:00
foreach ($devices as $device) {
2021-07-13 16:35:43 -05:00
$row = ['device' => $device];
2018-12-16 15:18:17 -06:00
if ($device->disabled) {
$totals['disabled']++;
2021-07-13 16:35:43 -05:00
$row['stateName'] = 'disabled';
$row['labelClass'] = 'blackbg';
} elseif ($device->disable_notify) {
2018-12-16 15:18:17 -06:00
$totals['ignored']++;
2021-07-13 16:35:43 -05:00
$row['stateName'] = 'alert-dis';
$row['labelClass'] = 'label-default';
2018-12-16 15:18:17 -06:00
} elseif ($device->status == 1) {
if (($device->uptime < $uptime_warn) && ($device->uptime != 0)) {
$totals['warn']++;
2021-07-13 16:35:43 -05:00
$row['stateName'] = 'warn';
$row['labelClass'] = 'label-warning';
2018-12-16 15:18:17 -06:00
} else {
$totals['up']++;
2021-07-13 16:35:43 -05:00
$row['stateName'] = 'up';
$row['labelClass'] = 'label-success';
2018-12-16 15:18:17 -06:00
}
} else {
$totals['down']++;
2021-07-13 16:35:43 -05:00
$row['stateName'] = 'down';
$row['labelClass'] = 'label-danger';
2018-12-16 15:18:17 -06:00
}
2020-02-02 15:33:01 +01:00
if ($device->isUnderMaintenance()) {
2021-07-13 16:35:43 -05:00
$row['labelClass'] = 'label-default';
2020-02-02 15:33:01 +01:00
$totals['maintenance']++;
}
2021-07-13 16:35:43 -05:00
$data[] = $row;
2018-12-16 15:18:17 -06:00
}
2020-09-21 14:54:51 +02:00
return [$data, $totals];
2018-12-16 15:18:17 -06:00
}
private function getServices($request)
{
$settings = $this->getSettings();
// filter for by device group or show all
2019-08-08 02:59:14 +02:00
if ($settings['device_group']) {
$services_query = DeviceGroup::find($settings['device_group'])->services()->hasAccess($request->user());
2018-12-16 15:18:17 -06:00
} else {
$services_query = Service::hasAccess($request->user());
}
if ($settings['order_by'] == 'status') {
$services_query->orderBy('service_status', 'DESC')->orderBy('service_type');
} elseif ($settings['order_by'] == 'hostname') {
$services_query->leftJoin('devices', 'services.device_id', 'devices.device_id')->orderBy('hostname')->orderBy('service_type');
}
2018-12-16 15:18:17 -06:00
$services = $services_query->with(['device' => function ($query) {
2021-07-13 16:35:43 -05:00
$query->select(['devices.device_id', 'hostname', 'sysName']);
}])->select(['service_id', 'services.device_id', 'service_type', 'service_desc', 'service_status'])->get();
2018-12-16 15:18:17 -06:00
// process status
$totals = ['warn' => 0, 'up' => 0, 'down' => 0];
2021-07-13 16:35:43 -05:00
$data = [];
2018-12-16 15:18:17 -06:00
foreach ($services as $service) {
2021-07-13 16:35:43 -05:00
$row = ['service' => $service];
2018-12-16 15:18:17 -06:00
if ($service->service_status == 0) {
2021-07-13 16:35:43 -05:00
$row['labelClass'] = 'label-success';
$row['stateName'] = 'up';
2018-12-16 15:18:17 -06:00
$totals['up']++;
} elseif ($service->service_status == 1) {
2021-07-13 16:35:43 -05:00
$row['labelClass'] = 'label-warning';
$row['stateName'] = 'warn';
2018-12-16 15:18:17 -06:00
$totals['warn']++;
} else {
2021-07-13 16:35:43 -05:00
$row['labelClass'] = 'label-danger';
$row['stateName'] = 'down';
2018-12-16 15:18:17 -06:00
$totals['down']++;
}
2021-07-13 16:35:43 -05:00
$data[] = $row;
2018-12-16 15:18:17 -06:00
}
2020-09-21 14:54:51 +02:00
return [$data, $totals];
2018-12-16 15:18:17 -06:00
}
}