Skip to content

Commit 8cf3709

Browse files
authored
Add customoid type to graph widget (librenms#18452)
* Update Graph.php, CustomoidController.php, GraphController.php, and 3 more files * Update Graph.php and GraphController.php * lint * lint * Update CustomoidController.php * Update GraphController.php
1 parent 8bf72bc commit 8cf3709

File tree

6 files changed

+80
-0
lines changed

6 files changed

+80
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Select;
4+
5+
use App\Models\Customoid;
6+
7+
class CustomoidController extends SelectController
8+
{
9+
/**
10+
* Defines the base query for this resource
11+
*
12+
* @param \Illuminate\Http\Request $request
13+
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder
14+
*/
15+
protected function baseQuery($request)
16+
{
17+
return Customoid::hasAccess($request->user())
18+
->with(['device' => function ($query): void {
19+
$query->select('device_id', 'hostname', 'sysName', 'display');
20+
}])
21+
->select('customoid_id', 'customoid_descr', 'device_id');
22+
}
23+
24+
/**
25+
* @param Customoid $customoid
26+
*/
27+
public function formatItem($customoid)
28+
{
29+
return [
30+
'id' => $customoid->customoid_id,
31+
'text' => $customoid->device->shortDisplayName() . ' (' . $customoid->customoid_descr . ')',
32+
];
33+
}
34+
}

app/Http/Controllers/Select/GraphController.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ public function __invoke(Request $request)
8787
];
8888
}
8989

90+
$customoid = $this->filterTypeGraphs(collect([
91+
'customoid_customoid' => 'CustomOID Graph',
92+
]), 'customoid', $search);
93+
if ($customoid->isNotEmpty()) {
94+
$data[] = [
95+
'text' => 'CustomOID',
96+
'children' => $customoid->map(fn ($text, $id) => ['id' => $id, 'text' => $text])->values(),
97+
];
98+
}
99+
90100
return response()->json([
91101
'results' => $data,
92102
'pagination' => ['more' => false],

app/Http/Controllers/Widgets/GraphController.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use App\Facades\LibrenmsConfig;
3030
use App\Models\Application;
3131
use App\Models\Bill;
32+
use App\Models\Customoid;
3233
use App\Models\Device;
3334
use App\Models\MuninPlugin;
3435
use App\Models\Port;
@@ -53,6 +54,7 @@ class GraphController extends WidgetController
5354
'graph_application' => null,
5455
'graph_munin' => null,
5556
'graph_service' => null,
57+
'graph_customoid' => null,
5658
'graph_ports' => [],
5759
'graph_custom' => [],
5860
'graph_manual' => null,
@@ -95,6 +97,10 @@ public function getTitle(): string
9597
if ($service = Service::find($settings['graph_service'])) {
9698
return $service->device->displayName() . ' / ' . $service->service_type . ' (' . $service->service_desc . ')' . ' / ' . $settings['graph_type'];
9799
}
100+
} elseif ($type == 'customoid') {
101+
if ($customoid = Customoid::find($settings['graph_customoid'])) {
102+
return $customoid->device?->displayName() . ' / ' . $type . ' / ' . $customoid->customoid_descr;
103+
}
98104
}
99105

100106
// fall back for types where we couldn't find the item
@@ -146,6 +152,11 @@ public function getSettingsView(Request $request): View
146152
}
147153
$data['service_text'] = isset($service) ? $service->device->displayName() . ' - ' . $service->service_type . ' (' . $service->service_desc . ')' : __('Service does not exist');
148154

155+
if ($primary == 'customoid' && $data['graph_customoid']) {
156+
$customoid = Customoid::with('device')->find($data['graph_customoid']);
157+
}
158+
$data['customoid_text'] = isset($customoid) ? $customoid->device?->displayName() . ' - ' . $customoid->customoid_descr : __('Custom OID does not exist');
159+
149160
$data['graph_ports'] = Port::whereIntegerInRaw('port_id', $data['graph_ports'])
150161
->select('ports.device_id', 'port_id', 'ifAlias', 'ifName', 'ifDescr')
151162
->with(['device' => function ($query): void {
@@ -189,6 +200,12 @@ public function getView(Request $request): string|View
189200
$params[] = 'device=' . $service->device_id;
190201
$params[] = 'id=' . $service->service_id;
191202
}
203+
} elseif ($type == 'customoid') {
204+
if ($customoid = Customoid::find($settings['graph_customoid'])) {
205+
$params[] = 'device=' . $customoid->device_id;
206+
$params[] = 'id=' . $customoid->customoid_id;
207+
$settings['graph_type'] = 'customoid_' . $customoid->customoid_descr;
208+
}
192209
} elseif ($type == 'aggregate') {
193210
$aggregate_type = $this->getGraphType(false);
194211
if ($aggregate_type == 'custom') {

app/Models/Customoid.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
class Customoid extends DeviceRelatedModel
6+
{
7+
public $timestamps = false;
8+
protected $primaryKey = 'customoid_id';
9+
}

resources/views/widgets/settings/graph.blade.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@
7878
@endif
7979
</select>
8080
</div>
81+
<div class="form-group graph_select_extra-{{ $id }}" id="graph_select_customoid-{{ $id }}" style="display: none;">
82+
<label for="graph_customoid-{{ $id }}" class="control-label">{{ __('Customoid') }}</label>
83+
<select class="form-control" id="graph_customoid-{{ $id }}" name="graph_customoid" data-placeholder="{{ __('Select a custom OID') }}">
84+
@if($graph_customoid)
85+
<option value="{{ $graph_customoid }}">{{ $customoid_text }}</option>
86+
@endif
87+
</select>
88+
</div>
8189
<div class="form-group graph_select_extra-{{ $id }}" id="graph_select_bill-{{ $id }}" style="display: none;">
8290
<label for="graph_bill-{{ $id }}" class="control-label">{{ __('Bill') }}</label>
8391
<select class="form-control" id="graph_bill-{{ $id }}" name="graph_bill" data-placeholder="{{ __('Select a bill') }}">
@@ -121,6 +129,7 @@
121129
}, '{{ $graph_application ?: '' }}');
122130
init_select2('#graph_munin-{{ $id }}', 'munin', {limit: 100}, '{{ $graph_munin ?: '' }}');
123131
init_select2('#graph_service-{{ $id }}', 'service', {limit: 100}, '{{ $graph_service ?: '' }}');
132+
init_select2('#graph_customoid-{{ $id }}', 'customoid', {limit: 100}, '{{ $graph_customoid ?: '' }}');
124133
init_select2('#graph_bill-{{ $id }}', 'bill', {limit: 100}, '{{ $graph_bill ?: '' }}');
125134
init_select2('#graph_custom-{{ $id }}', 'graph-aggregate', {}, false);
126135
init_select2('#graph_ports-{{ $id }}', 'port', {limit: 100}, {{ $graph_port_ids }});

routes/web.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@
275275
Route::get('munin', Select\MuninPluginController::class)->name('ajax.select.munin');
276276
Route::get('role', Select\RoleController::class)->name('ajax.select.role');
277277
Route::get('service', Select\ServiceController::class)->name('ajax.select.service');
278+
Route::get('customoid', Select\CustomoidController::class)->name('ajax.select.customoid');
278279
Route::get('template', Select\ServiceTemplateController::class)->name('ajax.select.template');
279280
Route::get('poller-group', Select\PollerGroupController::class)->name('ajax.select.poller-group');
280281
Route::get('port', Select\PortController::class)->name('ajax.select.port');

0 commit comments

Comments
 (0)