Make docker app work with rrdcached (#12746)

* docker app rrdcached

* offset fix and formatting fix

* more fixes

* Update Rrd.php

* Update Rrd.php

Co-authored-by: Jellyfrog <Jellyfrog@users.noreply.github.com>
This commit is contained in:
djamp42
2021-04-21 08:40:48 -04:00
committed by GitHub
parent 7c157c1577
commit 97d20dd09a
3 changed files with 67 additions and 3 deletions

View File

@@ -381,7 +381,7 @@ class Rrd extends BaseDatastore
}
// send the command!
if ($command == 'last' && $this->init(false)) {
if (in_array($command, ['last', 'list']) && $this->init(false)) {
// send this to our synchronous process so output is guaranteed
$output = $this->sync_process->sendCommand($cmd);
} elseif ($this->init()) {
@@ -442,6 +442,70 @@ class Rrd extends BaseDatastore
return "$command $filename $options";
}
/**
* Get array of all rrd files for a device,
* via rrdached or localdisk.
*
* @param array $device device for which we get the rrd's
* @return array array of rrd files for this host
*/
public function getRrdFiles($device)
{
if ($this->rrdcached) {
$filename = sprintf('/%s', $device['hostname']);
$rrd_files = $this->command('list', $filename, '');
// Command output is an array, create new array with each filename as a item in array.
$rrd_files_array = explode("\n", trim($rrd_files[0]));
// Remove status line from response
array_pop($rrd_files_array);
} else {
$rrddir = $this->dirFromHost($device['hostname']);
$pattern = sprintf('%s/*.rrd', $rrddir);
$rrd_files_array = glob($pattern);
}
sort($rrd_files_array);
return $rrd_files_array;
}
/**
* Get array of rrd files for specific application.
*
* @param array $device device for which we get the rrd's
* @param int $app_id application id on the device
* @param string $app_name name of app to be searched
* @param string $category which category of graphs are searched
* @return array array of rrd files for this host
*/
public function getRrdApplicationArrays($device, $app_id, $app_name, $category = null)
{
$entries = [];
$separator = '-';
$rrdfile_array = $this->getRrdFiles($device);
if ($category) {
$pattern = sprintf('%s-%s-%s-%s', 'app', $app_name, $app_id, $category);
} else {
$pattern = sprintf('%s-%s-%s', 'app', $app_name, $app_id);
}
// app_name contains a separator character? consider it
$offset = substr_count($app_name, $separator);
foreach ($rrdfile_array as $rrd) {
if (str_contains($rrd, $pattern)) {
$filename = basename($rrd, '.rrd');
$entry = explode($separator, $filename, 4 + $offset)[3 + $offset];
if ($entry) {
array_push($entries, $entry);
}
}
}
return $entries;
}
/**
* Checks if the rrd file exists on the server
* This will perform a remote check if using rrdcached and rrdtool >= 1.5

View File

@@ -15,7 +15,7 @@ $smalldescrlen = 25;
if (isset($vars['container'])) {
$containers = [$vars['container']];
} else {
$containers = get_arrays_with_application($device, $app['app_id'], 'docker');
$containers = Rrd::getRrdApplicationArrays($device, $app['app_id'], 'docker');
}
$int = 0;

View File

@@ -1,6 +1,6 @@
<?php
$domain_list = get_arrays_with_application($device, $app['app_id'], 'docker');
$domain_list = Rrd::getRrdApplicationArrays($device, $app['app_id'], 'docker');
print_optionbar_start();