2024-06-03 19:48:39 +02:00
|
|
|
<?php
|
|
|
|
require 'vendor/autoload.php';
|
|
|
|
require 'config.php';
|
|
|
|
|
2024-06-03 19:48:39 +02:00
|
|
|
if($_SERVER["REQUEST_URI"] == '/metrics' || php_sapi_name() == 'cli') {
|
2024-06-03 19:48:39 +02:00
|
|
|
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['http'])) {
|
|
|
|
$client = new GuzzleHttp\Client();
|
2024-06-03 19:48:39 +02:00
|
|
|
|
2024-06-03 19:48:39 +02:00
|
|
|
$options = [];
|
|
|
|
$output = NULL;
|
|
|
|
if (isset($c['http']['proxy'])) {
|
|
|
|
$options['proxy'] = $c['http']['proxy'];
|
|
|
|
}
|
|
|
|
if (isset($c['http']['data']) && $c['http']['data'] == 'responsetime') {
|
|
|
|
$options['on_stats'] = function (GuzzleHttp\TransferStats $stats) use (&$output) {
|
|
|
|
$output = $stats->getTransferTime();
|
|
|
|
};
|
|
|
|
}
|
2024-06-03 19:48:39 +02:00
|
|
|
|
|
|
|
$hasresponse = NULL;
|
|
|
|
try{
|
|
|
|
$res = $client->request('GET', $c['http']['url'], $options);
|
|
|
|
$hasresponse = true;
|
|
|
|
} catch(GuzzleHttp\Exception\GuzzleException $e) {
|
|
|
|
$hasresponse = false;
|
|
|
|
}
|
2024-06-03 19:48:39 +02:00
|
|
|
|
2024-06-03 19:48:39 +02:00
|
|
|
if (isset($c['http']['data']) && $c['http']['data'] == 'responsebody') {
|
|
|
|
$output = $res->getBody()->getContents();
|
2024-06-03 19:48:39 +02:00
|
|
|
} elseif (isset($c['http']['data']) && $c['http']['data'] == 'hasresponse') {
|
|
|
|
$output = (int)$hasresponse;
|
2024-06-03 19:48:39 +02:00
|
|
|
}
|
|
|
|
}
|
2024-06-03 19:48:39 +02:00
|
|
|
|
2024-06-03 19:48:39 +02:00
|
|
|
if(isset($c['jsonelem'])) {
|
|
|
|
$c['value'] = getArrayValue($c['jsonelem'], json_decode($output, true));
|
|
|
|
} else {
|
|
|
|
$c['value'] = $output;
|
|
|
|
}
|
|
|
|
}
|
2024-06-03 19:48:39 +02:00
|
|
|
|
2024-06-03 19:48:39 +02:00
|
|
|
$loader = new \Twig\Loader\FilesystemLoader('templates');
|
|
|
|
$twig = new \Twig\Environment($loader, [
|
|
|
|
'cache' => 'twig_cache',
|
|
|
|
]);
|
|
|
|
echo $twig->render('metrics.twig', ['config' => $config]);
|
2024-06-03 19:48:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getArrayValue($elem, $array) {
|
2024-06-03 19:48:39 +02:00
|
|
|
$elem = explode('.', $elem);
|
|
|
|
$new_array = $array;
|
|
|
|
foreach ($elem as $i) {
|
|
|
|
$new_array = $new_array[$i];
|
|
|
|
}
|
|
|
|
return $new_array;
|
2024-06-03 19:48:39 +02:00
|
|
|
}
|