Skip to content

Instantly share code, notes, and snippets.

@djamp42
Last active March 31, 2021 16:00
Show Gist options
  • Save djamp42/39e2e849094595b5741faf806b43ab35 to your computer and use it in GitHub Desktop.
Save djamp42/39e2e849094595b5741faf806b43ab35 to your computer and use it in GitHub Desktop.
Make some applications display graph over rrdcached
function get_arrays_with_application($device, $app_id, $app_name, $category = null)
{
$entries = [];
$apparray = [];
$separator = '-';
if ($category) {
$pattern = sprintf('%s/%s-%s-%s-%s-*.rrd', Rrd::dirFromHost($device['hostname']), 'app', $app_name, $app_id, $category);
} else {
$pattern = sprintf('%s/%s-%s-%s-*.rrd', Rrd::dirFromHost($device['hostname']), 'app', $app_name, $app_id);
}
// app_name contains a separator character? consider it
$offset = substr_count($app_name, $separator);
if (Config::get('rrdcached')) {
$pattern = sprintf('%s-%s-%s', 'app', $app_name, $app_id);
// Build the rrdtool cmd to list rrdfiles for this device
$rrdcmd = sprintf('%s list /%s --daemon %s', Config::get('rrdtool', 'rrdtool'), $device['hostname'], Config::get('rrdcached'));
$rrd_files = shell_exec($rrdcmd);
// Split returned files into array
$rrdarray = preg_split('/\s+/', trim($rrd_files));
// Find only the rrd files for the app we care about and add them to apparray.
foreach ($rrdarray as $rrd) {
if (str_contains($rrd, $pattern)) {
array_push($apparray, $rrd);
}
}
} else {
$apparray = glob($pattern);
}
foreach ($apparray as $rrd) {
$filename = basename($rrd, '.rrd');
$entry = explode($separator, $filename, 4 + $offset)[3 + $offset];
if ($entry) {
array_push($entries, $entry);
}
}
return $entries;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment