Skip to content

Commit c937866

Browse files
committed
Revert changes to Rrd datastore and use local code.
getRrdFiles is just too much of a mess to use. If someone wants to fix it, they can do so in a separate PR.
1 parent dd45ed8 commit c937866

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

LibreNMS/Data/Store/Rrd.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -485,29 +485,26 @@ public function buildCommand(string $command, string $filename, array $options =
485485
}
486486

487487
/**
488-
* Get array of all rrd files for a device via rrdached or localdisk.
489-
* If $hostname is empty, it will list all rrd files
488+
* Get array of all rrd files for a device,
489+
* via rrdached or localdisk.
490490
*
491491
* @param string $hostname hostname of the device
492-
* @return string[] array of rrd files for this host relative to rrd_dir
492+
* @return string[] array of rrd files for this host
493493
*/
494494
public function getRrdFiles(string $hostname): array
495495
{
496-
$dir = $this->dirFromHost($hostname);
497-
498496
if ($this->rrdcached) {
499-
$output = $this->command('list', $dir . '/', ['--recursive']);
497+
$output = $this->command('list', '/' . self::safeName($hostname));
498+
500499
$files = explode("\n", trim($output[0] ?? ''));
501500
array_pop($files); // remove rrdcached status line
502-
$prepend = str_replace($this->rrd_dir . '/', '', $dir) . '/';
503-
504-
return array_map(fn ($file) => $prepend . $file, $files);
501+
} else {
502+
$files = glob($this->dirFromHost($hostname) . '/*.rrd') ?: [];
505503
}
506504

507-
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir));
508-
$rrdFiles = new \RegexIterator($iterator, '/\.rrd$/');
505+
sort($files);
509506

510-
return array_map(fn (\SplFileInfo $file) => str_replace($this->rrd_dir . '/', '', $file), iterator_to_array($rrdFiles, false));
507+
return $files;
511508
}
512509

513510
/**
@@ -525,8 +522,6 @@ public function getRrdApplicationArrays($device, $app_id, $app_name, $category =
525522
$separator = '-';
526523

527524
$rrdfile_array = $this->getRrdFiles($device['hostname']);
528-
sort($rrdfile_array);
529-
530525
if ($category) {
531526
$pattern = sprintf('%s-%s-%s-%s', 'app', $app_name, $app_id, $category);
532527
} else {

app/Console/Commands/MaintenanceRrdStep.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use App\Console\LnmsCommand;
66
use App\Facades\DeviceCache;
77
use App\Facades\LibrenmsConfig;
8-
use App\Facades\Rrd;
98
use Exception;
109
use LibreNMS\Exceptions\RrdException;
1110
use LibreNMS\RRD\RrdProcess;
@@ -50,11 +49,9 @@ public function handle(RrdProcess $rrdProcess): int
5049
return 0;
5150
}
5251

53-
$files = Rrd::getRrdFiles($hostname === 'all' ? '' : $hostname);
54-
5552
[$converted, $skipped, $failed] = [0, 0, 0];
5653

57-
foreach ($files as $file) {
54+
foreach ($this->listFiles($hostname, $rrdProcess) as $file) {
5855
$rrdFile = basename($file, '.rrd');
5956

6057
[$step, $heartbeat] = $rrdFile === 'icmp-perf'
@@ -128,4 +125,28 @@ private function modifyXml(string $xmlContent, int $step, int $heartbeat): strin
128125

129126
return $xmlContent;
130127
}
128+
129+
/**
130+
* @param string $hostname
131+
* @param RrdProcess $rrdProcess
132+
* @return string[]
133+
* @throws RrdException
134+
*/
135+
private function listFiles(string $hostname, RrdProcess $rrdProcess): array
136+
{
137+
$command= $hostname === 'all' ? ['list -r .', ''] : "list ./$hostname";
138+
$output = rtrim($rrdProcess->run($command));
139+
140+
if (empty($output)) {
141+
return [];
142+
}
143+
144+
$files = explode("\n", $output);
145+
146+
if ($hostname === 'all') {
147+
return $files;
148+
}
149+
150+
return array_map(fn ($file) => "$hostname/$file", $files);
151+
}
131152
}

0 commit comments

Comments
 (0)