Skip to content

Commit 85e1f75

Browse files
committed
Refactor and cleanup the processor graph
1 parent 5893404 commit 85e1f75

4 files changed

Lines changed: 43 additions & 78 deletions

File tree

LibreNMS/Data/Graphing/Device/ProcessorGraph.php

Lines changed: 40 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,34 @@
2929
use App\Facades\LibrenmsConfig;
3030
use App\Facades\Rrd;
3131
use App\Models\Device;
32-
use Illuminate\Support\Facades\DB;
32+
use Illuminate\Support\Collection;
3333
use Illuminate\Support\Facades\Gate;
3434
use LibreNMS\Data\Graphing\AbstractGraph;
3535
use LibreNMS\Data\Graphing\Builders\MultiLineGraphBuilder;
3636
use LibreNMS\Data\Graphing\Builders\MultiSimplexSeparatedGraphBuilder;
37-
use LibreNMS\Exceptions\RrdGraphException;
38-
use LibreNMS\Util\Rewrite;
3937

4038
class ProcessorGraph extends AbstractGraph
4139
{
4240
public string $type = 'device';
4341
public string $subtype = 'processor';
4442

4543
private Device $device;
44+
private Collection $processors;
4645

47-
48-
public function __construct(private readonly array $vars = [])
49-
{
46+
public function __construct(
47+
private readonly array $vars = [],
48+
) {
5049
$this->device = DeviceCache::get($this->vars['device'] ?? null);
50+
$this->processors = $this->device->exists ? $this->device->processors : new Collection;
5151
}
5252

5353
public function authorize(): bool
5454
{
55-
return Gate::allows('view', $this->device);
55+
if ($processor = $this->processors->first()) {
56+
return Gate::allows('view', $processor);
57+
}
58+
59+
return $this->device->exists && Gate::allows('view', $this->device);
5660
}
5761

5862
public function getGraphTitle(): string
@@ -62,42 +66,30 @@ public function getGraphTitle(): string
6266

6367
public function getRrdFiles(): array
6468
{
65-
$procs = DB::table('processors')->where('device_id', $this->device->device_id)->get();
6669
$files = [];
67-
foreach ($procs as $proc) {
68-
$rrd_filename = Rrd::name($this->device->hostname, ['processor', $proc->processor_type, $proc->processor_index]);
69-
if (Rrd::checkRrdExists($rrd_filename)) {
70-
$files[] = $rrd_filename;
71-
}
70+
foreach ($this->processors as $proc) {
71+
$files[] = Rrd::name($this->device->hostname, ['processor', $proc->processor_type, $proc->processor_index]);
7272
}
7373
return $files;
7474
}
7575

7676
public function definition(array $vars = []): array
7777
{
78-
$this->authorize();
79-
80-
$procs = DB::table('processors')->where('device_id', $this->device->device_id)->get();
81-
if ($procs->isEmpty()) {
82-
throw new RrdGraphException('No Processors');
78+
if ($this->processors->isEmpty()) {
79+
throw new \LibreNMS\Exceptions\RrdGraphException('No Processors');
8380
}
8481

85-
$rrd_list = [];
86-
foreach ($procs as $proc) {
82+
$vars = array_merge($this->vars, $vars);
83+
84+
// Count how many files actually exist on disk for the layout
85+
$rrd_count = 0;
86+
foreach ($this->processors as $proc) {
8787
$rrd_filename = Rrd::name($this->device->hostname, ['processor', $proc->processor_type, $proc->processor_index]);
8888
if (Rrd::checkRrdExists($rrd_filename)) {
89-
$descr = Rewrite::shortHrDevice($proc->processor_descr);
90-
$rrd_list[] = [
91-
'filename' => $rrd_filename,
92-
'descr' => $descr,
93-
'ds' => 'usage',
94-
];
89+
$rrd_count++;
9590
}
9691
}
9792

98-
$vars = array_merge($this->vars, $vars);
99-
100-
// Check if stacked processor graph is configured for this OS
10193
if (LibrenmsConfig::getOsSetting($this->device->os, 'processor_stacked')) {
10294
$builder = (new MultiSimplexSeparatedGraphBuilder())
10395
->unitText('Load %')
@@ -106,37 +98,29 @@ public function definition(array $vars = []): array
10698
->colours('oranges')
10799
->scaleMin(0)
108100
->scaleMax(100)
109-
->divider(count($rrd_list))
101+
->divider(max(1, $rrd_count))
110102
->textOrig()
111103
->noTotal();
112-
113-
foreach ($rrd_list as $rrd) {
114-
$builder->addDataset(
115-
filename: $rrd['filename'],
116-
ds: $rrd['ds'],
117-
description: $rrd['descr']
118-
);
119-
}
120-
121-
return $builder->build($vars);
104+
} else {
105+
$builder = (new MultiLineGraphBuilder())
106+
->unitText('Load %')
107+
->units('')
108+
->totalUnits('%')
109+
->colours('mixed')
110+
->scaleMin(0)
111+
->scaleMax(100)
112+
->noTotal();
122113
}
123114

124-
$builder = (new MultiLineGraphBuilder())
125-
->unitText('Load %')
126-
->units('')
127-
->totalUnits('%')
128-
->colours('mixed')
129-
->scaleMin(0)
130-
->scaleMax(100)
131-
->noTotal();
132-
133-
foreach ($rrd_list as $rrd) {
134-
$builder->addDataset(
135-
filename: $rrd['filename'],
136-
ds: $rrd['ds'],
137-
description: $rrd['descr'],
138-
area: true
139-
);
115+
foreach ($this->processors as $proc) {
116+
$rrd_filename = Rrd::name($this->device->hostname, ['processor', $proc->processor_type, $proc->processor_index]);
117+
if (Rrd::checkRrdExists($rrd_filename)) {
118+
if ($builder instanceof MultiLineGraphBuilder) {
119+
$builder->addDataset($rrd_filename, 'usage', $proc->getFormattedDescription(), area: true);
120+
} else {
121+
$builder->addDataset($rrd_filename, 'usage', $proc->getFormattedDescription());
122+
}
123+
}
140124
}
141125

142126
return $builder->build($vars);

LibreNMS/Data/Graphing/LegacyGraph.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private function load(array $vars = []): void
7979
include_once base_path('includes/rewrites.php');
8080

8181
if (! $this->device && isset($this->vars['device'])) {
82-
$this->device = DeviceCache::get(is_numeric($this->vars['device']) ? $this->vars['device'] : getidbyname($this->vars['device']));
82+
$this->device = DeviceCache::get($this->vars['device']);
8383
}
8484

8585
if ($this->device) {

LibreNMS/Util/Rewrite.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -405,21 +405,4 @@ public static function celsiusToFahrenheit(float $celsius): float
405405
{
406406
return round($celsius * 1.8 + 32, 2);
407407
}
408-
409-
public static function shortHrDevice(string $descr): string
410-
{
411-
$rewrite_hrDevice = [
412-
'GenuineIntel:' => '',
413-
'AuthenticAMD:' => '',
414-
'Intel(R)' => '',
415-
'CPU' => '',
416-
'(R)' => '',
417-
' ' => ' ',
418-
];
419-
420-
$descr = str_replace(array_keys($rewrite_hrDevice), array_values($rewrite_hrDevice), $descr);
421-
$descr = preg_replace('/\ +/', ' ', $descr);
422-
423-
return trim($descr);
424-
}
425408
}

app/Models/Processor.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Processor extends DeviceRelatedModel
1818
*
1919
* @return string
2020
*/
21-
public function getFormattedDescription()
21+
public function getFormattedDescription(): string
2222
{
2323
$bad_descr = [
2424
'GenuineIntel:',
@@ -32,8 +32,6 @@ public function getFormattedDescription()
3232
$descr = str_replace($bad_descr, '', $this->processor_descr);
3333

3434
// reduce extra spaces
35-
$descr = str_replace(' ', ' ', $descr);
36-
37-
return $descr;
35+
return str_replace(' ', ' ', $descr);
3836
}
3937
}

0 commit comments

Comments
 (0)