Compare commits
9 Commits
7c79ea3b6b
...
ba8158f35e
Author | SHA1 | Date | |
---|---|---|---|
|
ba8158f35e | ||
|
13cadf3acc | ||
|
d0be3daa89 | ||
|
98fa29d900 | ||
|
d829ac06b0 | ||
|
1534574e97 | ||
|
1dfddd64ba | ||
|
bcb0d0b23d | ||
|
226ec5c6a1 |
@ -7,7 +7,25 @@ $config = [
|
|||||||
],
|
],
|
||||||
'app_demo_metric2' => [
|
'app_demo_metric2' => [
|
||||||
'help' => 'Help text for metric',
|
'help' => 'Help text for metric',
|
||||||
'httpurl' => 'http://example.com', // The url to get collected for the metric
|
'http' => [
|
||||||
|
'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
|
||||||
|
],
|
||||||
|
],
|
||||||
];
|
];
|
80
index.php
80
index.php
@ -2,35 +2,61 @@
|
|||||||
require 'vendor/autoload.php';
|
require 'vendor/autoload.php';
|
||||||
require 'config.php';
|
require 'config.php';
|
||||||
|
|
||||||
if($_SERVER["REQUEST_URI"] == '/metrics') {
|
if($_SERVER["REQUEST_URI"] == '/metrics' || php_sapi_name() == 'cli') {
|
||||||
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['httpurl'])) {
|
} elseif(isset($c['http'])) {
|
||||||
$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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$loader = new \Twig\Loader\FilesystemLoader('templates');
|
$options = [];
|
||||||
$twig = new \Twig\Environment($loader, [
|
$output = NULL;
|
||||||
'cache' => 'twig_cache',
|
if (isset($c['http']['proxy'])) {
|
||||||
]);
|
$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;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user