Compare commits

...

9 Commits

Author SHA1 Message Date
Jeroen De Meerleer
ba8158f35e
Update metric data retrieval configurations with additional details and proxy server setting. 2024-06-03 14:01:26 +02:00
Jeroen De Meerleer
13cadf3acc
Refactor metric configurations for HTTP requests
- Restructured config to include 'http' with url and data fields for each metric. Added metrics 3 and 4 with specific data outputs.
2024-06-03 14:00:10 +02:00
Jeroen De Meerleer
d0be3daa89
Refactor HTTP request handling for response status check
- Added try-catch block to handle GuzzleException
- Updated logic to check for response status before processing
2024-06-03 13:55:46 +02:00
Jeroen De Meerleer
98fa29d900
Refactor HTTP request handling and data extraction
- Improved handling of HTTP requests and extraction of response data.
2024-06-03 13:52:02 +02:00
Jeroen De Meerleer
d829ac06b0
Update URI check to include CLI request detection.
The code now checks if the request URI is '/metrics' or if the PHP script is running from the command line interface (CLI). This change allows for proper handling of CLI requests in addition to web requests.
2024-06-03 13:51:05 +02:00
Jeroen De Meerleer
1534574e97
Refactor HTTP handling, update Guzzle requests and Twig config.
- Refactored HTTP handling to use nested arrays for options
- Updated Guzzle requests based on new array structure
- Adjusted Twig cache configuration in the code
2024-06-03 13:50:56 +02:00
Jeroen De Meerleer
1dfddd64ba
Refactor Guzzle HTTP requests for flexibility and proxy support
Restructured Guzzle HTTP requests to allow for additional options like proxy support, enhancing flexibility in handling different types of requests.
2024-06-03 13:33:43 +02:00
Jeroen De Meerleer
bcb0d0b23d
Add handling for HTTP response time metric using GuzzleHttp. 2024-06-03 13:03:31 +02:00
Jeroen De Meerleer
226ec5c6a1
Update handling of value assignment when 'jsonelem' is not set.
- Adjusted code to assign $output directly to 'value' if 'jsonelem' is not set.
2024-06-03 13:03:14 +02:00
2 changed files with 72 additions and 28 deletions

View File

@ -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
],
],
];

View File

@ -2,35 +2,61 @@
require 'vendor/autoload.php';
require 'config.php';
if($_SERVER["REQUEST_URI"] == '/metrics') {
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'])) {
$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));
}
}
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['http'])) {
$client = new GuzzleHttp\Client();
$loader = new \Twig\Loader\FilesystemLoader('templates');
$twig = new \Twig\Environment($loader, [
'cache' => 'twig_cache',
]);
echo $twig->render('metrics.twig', ['config' => $config]);
$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;
}
}
$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) {
$elem = explode('.', $elem);
$new_array = $array;
foreach ($elem as $i) {
$new_array = $new_array[$i];
}
return $new_array;
$elem = explode('.', $elem);
$new_array = $array;
foreach ($elem as $i) {
$new_array = $new_array[$i];
}
return $new_array;
}