custom-prometheus-exporter/index.php
Jeroen De Meerleer 226ec5c6a1
Update handling of value assignment when 'jsonelem' is not set.
- Adjusted code to assign $output directly to 'value' if 'jsonelem' is not set.
2024-06-03 13:03:14 +02:00

38 lines
965 B
PHP

<?php
require 'vendor/autoload.php';
require 'config.php';
if($_SERVER["REQUEST_URI"] == '/metrics') {
foreach ($config as $key => &$c) {
if(isset($c['command'])) {
$output=null;
$retval=null;
exec($c['command'], $output, $retval);
$output = implode("\n", $output);
} elseif(isset($c['httpurl'])) {
$client = new GuzzleHttp\Client();
$res = $client->get($c['httpurl']);
$output = $res->getBody()->getContents();
}
if(isset($c['jsonelem'])) {
$c['value'] = getArrayValue($c['jsonelem'], json_decode($output, true));
} else {
$c['value'] = $output;
}
}
$loader = new \Twig\Loader\FilesystemLoader('templates');
$twig = new \Twig\Environment($loader, [
'cache' => 'twig_cache',
]);
echo $twig->render('metrics.twig', ['config' => $config]);
}
function getArrayValue($elem, $array) {
$elem = explode('.', $elem);
$new_array = $array;
foreach ($elem as $i) {
$new_array = $new_array[$i];
}
return $new_array;
}