2024-06-03 19:48:39 +02:00
|
|
|
<?php
|
|
|
|
require 'vendor/autoload.php';
|
2024-06-05 08:28:57 +02:00
|
|
|
$configFile = getenv('CONFIG_FILE') ?: 'config.php';
|
|
|
|
$cacheFile = $configFile . '.cache.json';
|
|
|
|
|
|
|
|
if (file_exists($configFile)) {
|
|
|
|
require ($configFile);
|
|
|
|
} else {
|
|
|
|
echo ("Configuration file not found: " . $configFile);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if(file_exists($cacheFile)) {
|
|
|
|
$oldconfig = json_decode(file_get_contents($cacheFile), true);
|
2024-06-04 16:50:53 +02:00
|
|
|
} else {
|
|
|
|
$oldconfig = [];
|
|
|
|
}
|
2024-06-03 19:48:39 +02:00
|
|
|
|
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];
|
|
|
|
}
|
2024-06-04 16:50:53 +02:00
|
|
|
foreach ($metric['submetrics'] as $subkey => &$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-04 16:50:53 +02:00
|
|
|
|
|
|
|
|
2024-06-04 19:39:08 +02:00
|
|
|
if(!empty($oldconfig) && !isset($submetric['value']) && (isset($submetric['fallback']) && !empty($submetric['fallback']))) {
|
2024-06-04 16:59:23 +02:00
|
|
|
if(!isset($submetric['fallback']['maxage']) || time() < ($oldconfig[$key]['submetrics'][$subkey]['time'] + $submetric['fallback']['maxage'])) {
|
2024-06-04 16:50:53 +02:00
|
|
|
if($submetric['fallback']['type'] == 'previous') {
|
|
|
|
$submetric['value'] = $oldconfig[$key]['submetrics'][$subkey]['value'];
|
|
|
|
} elseif ($submetric['fallback']['type'] == 'static') {
|
|
|
|
$submetric['value'] = $submetric['fallback']['value'];
|
|
|
|
}
|
|
|
|
$submetric['time'] = $oldconfig[$key]['submetrics'][$subkey]['time'];
|
|
|
|
} else {
|
|
|
|
$submetric['value'] = '';
|
|
|
|
$submetric['time'] = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$submetric['time'] = time();
|
|
|
|
}
|
2024-06-03 19:48:39 +02:00
|
|
|
}
|
|
|
|
}
|
2024-06-03 19:48:39 +02:00
|
|
|
|
2024-06-05 08:28:57 +02:00
|
|
|
file_put_contents($cacheFile, json_encode($config, JSON_PRETTY_PRINT));
|
2024-06-04 16:50:53 +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) {
|
2024-06-04 16:51:05 +02:00
|
|
|
if(empty($new_array)) break;
|
2024-06-03 19:48:39 +02:00
|
|
|
$new_array = $new_array[$i];
|
|
|
|
}
|
|
|
|
return $new_array;
|
2024-06-03 19:48:39 +02:00
|
|
|
}
|