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' => [
|
||||
'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',
|
||||
],
|
||||
'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
|
||||
],
|
||||
],
|
||||
];
|
34
index.php
34
index.php
@ -2,20 +2,46 @@
|
||||
require 'vendor/autoload.php';
|
||||
require 'config.php';
|
||||
|
||||
if($_SERVER["REQUEST_URI"] == '/metrics') {
|
||||
if($_SERVER["REQUEST_URI"] == '/metrics' || php_sapi_name() == 'cli') {
|
||||
foreach ($config as $key => &$c) {
|
||||
if(isset($c['command'])) {
|
||||
$output=null;
|
||||
$retval=null;
|
||||
exec($c['command'], $output, $retval);
|
||||
$output = implode("\n", $output);
|
||||
} elseif(isset($c['httpurl'])) {
|
||||
} elseif(isset($c['http'])) {
|
||||
$client = new GuzzleHttp\Client();
|
||||
$res = $client->get($c['httpurl']);
|
||||
$output = $res->getBody()->getContents();
|
||||
|
||||
$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();
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user