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,46 +2,20 @@
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']);
$options = [];
$output = NULL;
if (isset($c['http']['proxy'])) {
$options['proxy'] = $c['http']['proxy'];
}
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(); $output = $res->getBody()->getContents();
} elseif (isset($c['http']['data']) && $c['http']['data'] == 'hasresponse') {
$output = (int)$hasresponse;
} }
}
if(isset($c['jsonelem'])) { if(isset($c['jsonelem'])) {
$c['value'] = getArrayValue($c['jsonelem'], json_decode($output, true)); $c['value'] = getArrayValue($c['jsonelem'], json_decode($output, true));
} else {
$c['value'] = $output;
} }
} }