Compare commits
2 Commits
b52abb69c7
...
f06c804c24
Author | SHA1 | Date | |
---|---|---|---|
f06c804c24 | |||
f7f3cb9704 |
21
index.php
21
index.php
@ -1,6 +1,17 @@
|
|||||||
<?php
|
<?php
|
||||||
header('Content-Type: text/plain; version=0.0.4');
|
|
||||||
$config = getenv('CONFIG_FILE');
|
if($_SERVER["REQUEST_URI"] == '/metrics') {
|
||||||
$phpbin = getenv('PHP_BINARY');
|
header('Content-Type: text/plain; version=0.0.4');
|
||||||
$output = shell_exec('CONFIG_FILE="' . $config . '" ' . $phpbin . ' ' . $_SERVER['DOCUMENT_ROOT'] . 'script.php');
|
$config = getenv('CONFIG_FILE');
|
||||||
echo $output;
|
$phpbin = getenv('PHP_BINARY');
|
||||||
|
$output = shell_exec('CONFIG_FILE="' . $config . '" ' . $phpbin . ' ' . $_SERVER['DOCUMENT_ROOT'] . 'script.php');
|
||||||
|
echo $output;
|
||||||
|
} else {
|
||||||
|
require_once('vendor/autoload.php');
|
||||||
|
http_response_code(404);
|
||||||
|
$loader = new \Twig\Loader\FilesystemLoader('templates');
|
||||||
|
$twig = new \Twig\Environment($loader, [
|
||||||
|
'cache' => 'twig_cache',
|
||||||
|
]);
|
||||||
|
echo $twig->render('404.twig', ['path' => $_SERVER["REQUEST_URI"]]);
|
||||||
|
}
|
197
script.php
197
script.php
@ -14,118 +14,115 @@ if(file_exists($cacheFile)) {
|
|||||||
} else {
|
} else {
|
||||||
$oldconfig = [];
|
$oldconfig = [];
|
||||||
}
|
}
|
||||||
|
foreach ($config as $key => &$metric) {
|
||||||
|
if (!isset($metric['submetrics'])) {
|
||||||
|
$metric['submetrics'] = [$metric];
|
||||||
|
}
|
||||||
|
foreach ($metric['submetrics'] as $subkey => &$submetric) {
|
||||||
|
$pid = pcntl_fork();
|
||||||
|
if ($pid > 0) {
|
||||||
|
$pids[] = $pid;
|
||||||
|
} else {
|
||||||
|
if (isset($submetric['command'])) {
|
||||||
|
$output = null;
|
||||||
|
$retval = null;
|
||||||
|
try {
|
||||||
|
exec($submetric['command'], $output, $retval);
|
||||||
|
$output = implode("\n", $output);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$output = '';
|
||||||
|
}
|
||||||
|
} elseif (isset($submetric['http'])) {
|
||||||
|
$client = new GuzzleHttp\Client();
|
||||||
|
|
||||||
if(php_sapi_name() == 'cli' || $_SERVER["REQUEST_URI"] == '/metrics') {
|
$options = [];
|
||||||
foreach ($config as $key => &$metric) {
|
$output = NULL;
|
||||||
if (!isset($metric['submetrics'])) {
|
if (isset($submetric['http']['proxy'])) {
|
||||||
$metric['submetrics'] = [$metric];
|
$options['proxy'] = $submetric['http']['proxy'];
|
||||||
}
|
}
|
||||||
foreach ($metric['submetrics'] as $subkey => &$submetric) {
|
if (isset($submetric['http']['data']) && $submetric['http']['data'] == 'responsetime') {
|
||||||
$pid = pcntl_fork();
|
$options['on_stats'] = function (GuzzleHttp\TransferStats $stats) use (&$output) {
|
||||||
if ($pid > 0) {
|
$output = $stats->getTransferTime();
|
||||||
$pids[] = $pid;
|
};
|
||||||
} else {
|
}
|
||||||
if (isset($submetric['command'])) {
|
|
||||||
$output = null;
|
$hasresponse = NULL;
|
||||||
$retval = null;
|
try {
|
||||||
try {
|
$res = $client->request('GET', $submetric['http']['url'], $options);
|
||||||
exec($submetric['command'], $output, $retval);
|
$hasresponse = true;
|
||||||
$output = implode("\n", $output);
|
} catch (GuzzleHttp\Exception\GuzzleException $e) {
|
||||||
} catch (Exception $e) {
|
if (method_exists($e, 'getResponse')) {
|
||||||
|
$res = $e->getResponse();
|
||||||
|
$hasresponse = true;
|
||||||
|
} else {
|
||||||
|
$hasresponse = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($submetric['http']['data']) && $submetric['http']['data'] == 'responsebody') {
|
||||||
|
if ($hasresponse) {
|
||||||
|
$output = $res->getBody()->getContents();
|
||||||
|
} else {
|
||||||
$output = '';
|
$output = '';
|
||||||
}
|
}
|
||||||
} elseif (isset($submetric['http'])) {
|
} elseif ((isset($submetric['http']['statuscode'])) && (isset($submetric['http']['data']) && $submetric['http']['data'] == 'hasresponse')) {
|
||||||
$client = new GuzzleHttp\Client();
|
if ($hasresponse) {
|
||||||
|
$output = $output = (int)in_array($res->getStatusCode(), $submetric['http']['statuscode']);;
|
||||||
$options = [];
|
} else {
|
||||||
$output = NULL;
|
$output = 0;
|
||||||
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();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
$hasresponse = NULL;
|
|
||||||
try {
|
|
||||||
$res = $client->request('GET', $submetric['http']['url'], $options);
|
|
||||||
$hasresponse = true;
|
|
||||||
} 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') {
|
|
||||||
if ($hasresponse) {
|
|
||||||
$output = $res->getBody()->getContents();
|
|
||||||
} else {
|
|
||||||
$output = '';
|
|
||||||
}
|
|
||||||
} elseif ((isset($submetric['http']['statuscode'])) && (isset($submetric['http']['data']) && $submetric['http']['data'] == 'hasresponse')) {
|
|
||||||
if ($hasresponse) {
|
|
||||||
$output = $output = (int)in_array($res->getStatusCode(), $submetric['http']['statuscode']);;
|
|
||||||
} else {
|
|
||||||
$output = 0;
|
|
||||||
}
|
|
||||||
} elseif (isset($submetric['http']['data']) && $submetric['http']['data'] == 'hasresponse') {
|
|
||||||
$output = (int)$hasresponse;
|
|
||||||
}
|
}
|
||||||
|
} elseif (isset($submetric['http']['data']) && $submetric['http']['data'] == 'hasresponse') {
|
||||||
|
$output = (int)$hasresponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($submetric['jsonelem'])) {
|
|
||||||
$submetric['value'] = getArrayValue($submetric['jsonelem'], json_decode($output, true));
|
|
||||||
} else {
|
|
||||||
$submetric['value'] = $output;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (is_bool($submetric['value'])) $submetric['value'] = $submetric['value'] ? 1 : 0;
|
|
||||||
|
|
||||||
file_put_contents('/tmp/.metrics.' . $key . '.' . $subkey, $submetric['value']);
|
|
||||||
exit();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
foreach ($pids as $pid) {
|
|
||||||
pcntl_waitpid($pid, $none);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (isset($submetric['jsonelem'])) {
|
||||||
foreach ($config as $key => &$metric) {
|
$submetric['value'] = getArrayValue($submetric['jsonelem'], json_decode($output, true));
|
||||||
foreach ($metric['submetrics'] as $subkey => &$submetric) {
|
|
||||||
$submetric['value'] = file_get_contents('/tmp/.metrics.' . $key . '.' . $subkey);
|
|
||||||
if (!empty($oldconfig) && !isset($submetric['value']) && (isset($submetric['fallback']) && !empty($submetric['fallback']))) {
|
|
||||||
if (!isset($submetric['fallback']['maxage']) || time() < ($oldconfig[$key]['submetrics'][$subkey]['time'] + $submetric['fallback']['maxage'])) {
|
|
||||||
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 {
|
} else {
|
||||||
$submetric['time'] = time();
|
$submetric['value'] = $output;
|
||||||
}
|
}
|
||||||
unlink('/tmp/.metrics.' . $key . '.' . $subkey);
|
|
||||||
|
if (is_bool($submetric['value'])) $submetric['value'] = $submetric['value'] ? 1 : 0;
|
||||||
|
|
||||||
|
file_put_contents('/tmp/.metrics.' . $key . '.' . $subkey, $submetric['value']);
|
||||||
|
exit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
file_put_contents($cacheFile, json_encode($config, JSON_PRETTY_PRINT));
|
|
||||||
|
|
||||||
$loader = new \Twig\Loader\FilesystemLoader('templates');
|
|
||||||
$twig = new \Twig\Environment($loader, [
|
|
||||||
'cache' => 'twig_cache',
|
|
||||||
]);
|
|
||||||
echo $twig->render('metrics.twig', ['config' => $config]);
|
|
||||||
}
|
}
|
||||||
|
foreach ($pids as $pid) {
|
||||||
|
pcntl_waitpid($pid, $none);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
foreach ($config as $key => &$metric) {
|
||||||
|
foreach ($metric['submetrics'] as $subkey => &$submetric) {
|
||||||
|
$submetric['value'] = file_get_contents('/tmp/.metrics.' . $key . '.' . $subkey);
|
||||||
|
if (!empty($oldconfig) && !isset($submetric['value']) && (isset($submetric['fallback']) && !empty($submetric['fallback']))) {
|
||||||
|
if (!isset($submetric['fallback']['maxage']) || time() < ($oldconfig[$key]['submetrics'][$subkey]['time'] + $submetric['fallback']['maxage'])) {
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
unlink('/tmp/.metrics.' . $key . '.' . $subkey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
file_put_contents($cacheFile, json_encode($config, JSON_PRETTY_PRINT));
|
||||||
|
|
||||||
|
$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) {
|
function getArrayValue($elem, $array) {
|
||||||
$elem = explode('.', $elem);
|
$elem = explode('.', $elem);
|
||||||
|
12
templates/404.twig
Normal file
12
templates/404.twig
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Page Title</title>
|
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||||
|
<style>
|
||||||
|
</style>
|
||||||
|
<body>
|
||||||
|
<h1>404 Uri {{ path }} not found</h1>
|
||||||
|
<p><a href="/metrics">/metrics</a></p>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in New Issue
Block a user