Skip to content

Commit

Permalink
Better services graphing support (librenms#10185)
Browse files Browse the repository at this point in the history
* Move services graph code from device to service and remove unused code

* Add proper support for services to dashboard graph weidget

* Fix phpdoc in ServiceController still pointing to MuninPluginController
  • Loading branch information
CirnoT authored and Spencer Butler committed May 21, 2019
1 parent 577dac8 commit 317d497
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 27 deletions.
2 changes: 1 addition & 1 deletion LibreNMS/Util/Graph.php
Expand Up @@ -32,7 +32,7 @@ class Graph
{
public static function getTypes()
{
return ['device', 'port', 'application', 'munin'];
return ['device', 'port', 'application', 'munin', 'service'];
}

/**
Expand Down
55 changes: 55 additions & 0 deletions app/Http/Controllers/Select/ServiceController.php
@@ -0,0 +1,55 @@
<?php
/**
* ServiceController.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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2018 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/

namespace App\Http\Controllers\Select;

use App\Models\Service;

class ServiceController extends SelectController
{

/**
* Defines the base query for this resource
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder
*/
protected function baseQuery($request)
{
return Service::hasAccess($request->user())
->with(['device' => function ($query) {
$query->select('device_id', 'hostname', 'sysName');
}])
->select('service_id', 'service_type', 'service_desc', 'device_id');
}

public function formatItem($service)
{
return [
'id' => $service->service_id,
'text' => $service->device->shortDisplayName() . ' - ' . $service->service_type . ' (' . $service->service_desc . ')'
];
}
}
17 changes: 17 additions & 0 deletions app/Http/Controllers/Widgets/GraphController.php
Expand Up @@ -30,6 +30,7 @@
use App\Models\Device;
use App\Models\MuninPlugin;
use App\Models\Port;
use App\Models\Service;
use App\Models\UserWidget;
use Carbon\Carbon;
use Illuminate\Http\Request;
Expand All @@ -50,6 +51,7 @@ class GraphController extends WidgetController
'graph_port' => null,
'graph_application' => null,
'graph_munin' => null,
'graph_service' => null,
'graph_ports' => [],
'graph_custom' => [],
'graph_manual' => null,
Expand Down Expand Up @@ -87,6 +89,10 @@ public function title()
if ($munin = MuninPlugin::find($settings['graph_munin'])) {
return $munin->device->displayName() . ' / ' . $munin->mplug_type . ' / ' . $settings['graph_type'];
}
} elseif ($type == 'service') {
if ($service = Service::find($settings['graph_service'])) {
return $service->device->displayName() . ' / ' . $service->service_type . ' (' . $service->service_desc . ')' . ' / ' . $settings['graph_type'];
}
}

// fall back for types where we couldn't find the item
Expand Down Expand Up @@ -133,6 +139,11 @@ public function getSettingsView(Request $request)
}
$data['munin_text'] = isset($mplug) ? $mplug->device->displayName() . ' - ' . $mplug->mplug_type : __('Munin plugin does not exist');

if ($primary == 'service' && $data['graph_service']) {
$service = Service::with('device')->find($data['graph_service']);
}
$data['service_text'] = isset($service) ? $service->device->displayName() . ' - ' . $service->service_type . ' (' . $service->service_desc . ')' : __('Service does not exist');

$data['graph_ports'] = Port::whereIn('port_id', $data['graph_ports'])
->select('ports.device_id', 'port_id', 'ifAlias', 'ifName', 'ifDescr')
->with(['device' => function ($query) {
Expand Down Expand Up @@ -171,6 +182,11 @@ public function getView(Request $request)
$params[] = 'device='.$mplug->device_id;
$params[] = 'plugin='.$mplug->mplug_type;
}
} elseif ($type == 'service') {
if ($service = Service::find($settings['graph_service'])) {
$params[] = 'device='.$service->device_id;
$params[] = 'id='.$service->service_id;
}
} elseif ($type == 'aggregate') {
$aggregate_type = $this->getGraphType(false);
if ($aggregate_type == 'custom') {
Expand Down Expand Up @@ -255,6 +271,7 @@ public function getSettings()
$settings['graph_port'] = $this->convertLegacySettingId($settings['graph_port'], 'port_id');
$settings['graph_application'] = $this->convertLegacySettingId($settings['graph_application'], 'app_id');
$settings['graph_munin'] = $this->convertLegacySettingId($settings['graph_munin'], 'mplug_id');
$settings['graph_service'] = $this->convertLegacySettingId($settings['graph_service'], 'service_id');
$settings['graph_bill'] = $this->convertLegacySettingId($settings['graph_bill'], 'bill_id');

$settings['graph_custom'] = (array)$settings['graph_custom'];
Expand Down
2 changes: 1 addition & 1 deletion includes/html/graphs/service/auth.inc.php
Expand Up @@ -10,7 +10,7 @@
$rrd_filename = rrd_name($device['hostname'], array('service', $service['service_type'], $service['service_id']));

$title = generate_device_link($device);
$title .= ' :: Service :: '.htmlentities($service['service_type']);
$title .= ' :: Service :: '.htmlentities($service['service_type']).' - '.htmlentities($service['service_desc']);
$auth = true;
}
}
21 changes: 0 additions & 21 deletions includes/html/graphs/service/availability.inc.php

This file was deleted.

Expand Up @@ -16,10 +16,10 @@
$services = service_get($device['device_id']);

// Determine which key is the service we want to show.
if (isset($vars['service'])) {
if (isset($vars['id'])) {
// Service is set, find its key.
foreach ($services as $key => $service) {
if ($service['service_id'] == $vars['service']) {
if ($service['service_id'] == $vars['id']) {
// We have found the service we want.
$vars['service'] = $key;
}
Expand Down
4 changes: 2 additions & 2 deletions includes/html/pages/device/services.inc.php
Expand Up @@ -113,8 +113,8 @@
$graphs = json_decode($service['service_ds'], true);
foreach ($graphs as $k => $v) {
$graph_array['device'] = $device['device_id'];
$graph_array['type'] = 'device_service';
$graph_array['service'] = $service['service_id'];
$graph_array['type'] = 'service_graph';
$graph_array['id'] = $service['service_id'];
$graph_array['ds'] = $k;

echo '<tr>';
Expand Down
9 changes: 9 additions & 0 deletions resources/views/widgets/settings/graph.blade.php
Expand Up @@ -69,6 +69,14 @@
@endif
</select>
</div>
<div class="form-group graph_select_extra-{{ $id }}" id="graph_select_service-{{ $id }}" style="display: none;">
<label for="graph_service-{{ $id }}" class="control-label">@lang('Service')</label>
<select class="form-control" id="graph_service-{{ $id }}" name="graph_service" data-placeholder="@lang('Select a service')">
@if($graph_service)
<option value="{{ $graph_service }}">{{ $service_text }}</option>
@endif
</select>
</div>
<div class="form-group graph_select_extra-{{ $id }}" id="graph_select_bill-{{ $id }}" style="display: none;">
<label for="graph_bill-{{ $id }}" class="control-label">@lang('Bill')</label>
<select class="form-control" id="graph_bill-{{ $id }}" name="graph_bill" data-placeholder="@lang('Select a bill')">
Expand Down Expand Up @@ -111,6 +119,7 @@
};
}, '{{ $graph_application ?: '' }}');
init_select2('#graph_munin-{{ $id }}', 'munin', {limit: 100}, '{{ $graph_munin ?: '' }}');
init_select2('#graph_service-{{ $id }}', 'service', {limit: 100}, '{{ $graph_service ?: '' }}');
init_select2('#graph_bill-{{ $id }}', 'bill', {limit: 100}, '{{ $graph_bill ?: '' }}');
init_select2('#graph_custom-{{ $id }}', 'graph-aggregate', {}, false);
init_select2('#graph_ports-{{ $id }}', 'port', {limit: 100}, {{ $graph_port_ids }});
Expand Down
1 change: 1 addition & 0 deletions routes/web.php
Expand Up @@ -72,6 +72,7 @@
Route::get('syslog', 'SyslogController');
Route::get('location', 'LocationController');
Route::get('munin', 'MuninPluginController');
Route::get('service', 'ServiceController');
Route::get('port', 'PortController');
Route::get('port-field', 'PortFieldController');
});
Expand Down

0 comments on commit 317d497

Please sign in to comment.