2024-06-03 19:48:39 +02:00
|
|
|
<?php
|
|
|
|
require 'vendor/autoload.php';
|
|
|
|
require 'config.php';
|
|
|
|
|
2024-06-03 23:41:42 +02:00
|
|
|
if(php_sapi_name() == 'cli' || $_SERVER["REQUEST_URI"] == '/metrics') {
|
2024-06-03 19:48:39 +02:00
|
|
|
header('Content-Type: text/plain; version=0.0.4');
|
2024-06-03 23:47:40 +02:00
|
|
|
foreach ($config as $key => &$metric) {
|
|
|
|
if (!isset($metric['submetrics'])) {
|
|
|
|
$metric['submetrics'] = [$metric];
|
|
|
|
}
|
|
|
|
foreach ($metric['submetrics'] as $key => &$submetric) {
|
2024-06-03 19:48:39 +02:00
|
|
|
|
2024-06-03 23:47:40 +02:00
|
|
|
if (isset($submetric['command'])) {
|
|
|
|
$output = null;
|
|
|
|
$retval = null;
|
2024-06-04 13:20:43 +02:00
|
|
|
try {
|
|
|
|
exec($submetric['command'], $output, $retval);
|
|
|
|
$output = implode("\n", $output);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$output = '';
|
|
|
|
}
|
2024-06-03 23:47:40 +02:00
|
|
|
} elseif (isset($submetric['http'])) {
|
|
|
|
$client = new GuzzleHttp\Client();
|
|
|
|
|
|
|
|
$options = [];
|
|
|
|
$output = NULL;
|
|
|
|
if (isset($submetric['http']['proxy'])) {
|
|
|
|
$options['proxy'] = $submetric['http']['proxy'];
|
|
|
|
}
|
|
|
|
if (isset($submetric['http']['data']) && $submetric['http']['data'] == 'responsetime') {
|
|
|
|
$options['on_stats'] = function (GuzzleHttp\TransferStats $stats) use (&$output) {
|
|
|
|
$output = $stats->getTransferTime();
|
|
|
|
};
|
|
|
|
}
|
2024-06-03 19:48:39 +02:00
|
|
|
|
2024-06-03 23:47:40 +02:00
|
|
|
$hasresponse = NULL;
|
|
|
|
try {
|
|
|
|
$res = $client->request('GET', $submetric['http']['url'], $options);
|
2024-06-03 19:48:39 +02:00
|
|
|
$hasresponse = true;
|
2024-06-03 23:47:40 +02:00
|
|
|
} catch (GuzzleHttp\Exception\GuzzleException $e) {
|
|
|
|
if (method_exists($e, 'getResponse')) {
|
|
|
|
$res = $e->getResponse();
|
|
|
|
$hasresponse = true;
|
|
|
|
} else {
|
|
|
|
$hasresponse = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($submetric['http']['data']) && $submetric['http']['data'] == 'responsebody') {
|
2024-06-04 13:22:16 +02:00
|
|
|
if ($hasresponse) {
|
|
|
|
$output = $res->getBody()->getContents();
|
|
|
|
} else {
|
|
|
|
$output = '';
|
|
|
|
}
|
2024-06-04 07:06:40 +02:00
|
|
|
} elseif ((isset($submetric['http']['statuscode'])) && (isset($submetric['http']['data']) && $submetric['http']['data'] == 'hasresponse')) {
|
2024-06-04 13:22:16 +02:00
|
|
|
if ($hasresponse) {
|
|
|
|
$output = $output = (int)in_array($res->getStatusCode(), $submetric['http']['statuscode']);;
|
|
|
|
} else {
|
|
|
|
$output = 0;
|
|
|
|
}
|
2024-06-03 23:47:40 +02:00
|
|
|
} elseif (isset($submetric['http']['data']) && $submetric['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
|
|
|
|
2024-06-03 23:47:40 +02:00
|
|
|
if (isset($submetric['jsonelem'])) {
|
|
|
|
$submetric['value'] = getArrayValue($submetric['jsonelem'], json_decode($output, true));
|
|
|
|
} else {
|
|
|
|
$submetric['value'] = $output;
|
2024-06-03 19:48:39 +02:00
|
|
|
}
|
2024-06-03 19:48:39 +02:00
|
|
|
|
2024-06-03 23:47:40 +02:00
|
|
|
if (is_bool($submetric['value'])) $submetric['value'] = $submetric['value'] ? 1 : 0;
|
2024-06-03 19:48:39 +02:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|