Compare commits

..

No commits in common. "ba8158f35e13670b5014ea315a86eb9d545d4bfe" and "7c79ea3b6bb2b5d4a6d0ed34f771e5f81f7477db" have entirely different histories.

2 changed files with 28 additions and 72 deletions

View File

@ -7,25 +7,7 @@ $config = [
], ],
'app_demo_metric2' => [ 'app_demo_metric2' => [
'help' => 'Help text for metric', 'help' => 'Help text for metric',
'http' => [ 'httpurl' => 'http://example.com', // The url to get collected for the metric
'url' => 'http://example.com', // The url to get collected for the metric
'data' => 'responsebody', // The body is used as output
],
'jsonelem' => '0.metric', 'jsonelem' => '0.metric',
], ],
'app_demo_metric3' => [
'help' => 'Help text for metric',
'http' => [
'url' => 'http://example.com', // The url to get collected for the metric
'data' => 'hasresponse', // If an error occured during request this will return 0, otherwise 1
],
],
'app_demo_metric4' => [
'help' => 'Help text for metric',
'http' => [
'url' => 'http://example.com', // The url to get collected for the metric
'data' => 'responsetime', // This will return the transfer time
'proxy' => 'http://192.168.1.252:8080', // The proxy server the request needs to use
],
],
]; ];

View File

@ -2,61 +2,35 @@
require 'vendor/autoload.php'; require 'vendor/autoload.php';
require 'config.php'; require 'config.php';
if($_SERVER["REQUEST_URI"] == '/metrics' || php_sapi_name() == 'cli') { if($_SERVER["REQUEST_URI"] == '/metrics') {
foreach ($config as $key => &$c) { foreach ($config as $key => &$c) {
if(isset($c['command'])) { if(isset($c['command'])) {
$output=null; $output=null;
$retval=null; $retval=null;
exec($c['command'], $output, $retval); exec($c['command'], $output, $retval);
$output = implode("\n", $output); $output = implode("\n", $output);
} elseif(isset($c['http'])) { } elseif(isset($c['httpurl'])) {
$client = new GuzzleHttp\Client(); $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));
}
}
$options = []; $loader = new \Twig\Loader\FilesystemLoader('templates');
$output = NULL; $twig = new \Twig\Environment($loader, [
if (isset($c['http']['proxy'])) { 'cache' => 'twig_cache',
$options['proxy'] = $c['http']['proxy']; ]);
} echo $twig->render('metrics.twig', ['config' => $config]);
if (isset($c['http']['data']) && $c['http']['data'] == 'responsetime') {
$options['on_stats'] = function (GuzzleHttp\TransferStats $stats) use (&$output) {
$output = $stats->getTransferTime();
};
}
$hasresponse = NULL;
try{
$res = $client->request('GET', $c['http']['url'], $options);
$hasresponse = true;
} catch(GuzzleHttp\Exception\GuzzleException $e) {
$hasresponse = false;
}
if (isset($c['http']['data']) && $c['http']['data'] == 'responsebody') {
$output = $res->getBody()->getContents();
} elseif (isset($c['http']['data']) && $c['http']['data'] == 'hasresponse') {
$output = (int)$hasresponse;
}
}
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) { function getArrayValue($elem, $array) {
$elem = explode('.', $elem); $elem = explode('.', $elem);
$new_array = $array; $new_array = $array;
foreach ($elem as $i) { foreach ($elem as $i) {
$new_array = $new_array[$i]; $new_array = $new_array[$i];
} }
return $new_array; return $new_array;
} }