From 1534574e97b026e9a1d6afbc4072a007e64610d2 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Mon, 3 Jun 2024 13:50:56 +0200 Subject: [PATCH] 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 --- index.php | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/index.php b/index.php index 1316c67..bf4249e 100644 --- a/index.php +++ b/index.php @@ -9,31 +9,27 @@ if($_SERVER["REQUEST_URI"] == '/metrics') { $retval=null; exec($c['command'], $output, $retval); $output = implode("\n", $output); - } elseif(isset($c['httpurl'])) { + } elseif(isset($c['http'])) { $client = new GuzzleHttp\Client(); $options = []; - if (isset($c['proxy'])) { - $options['proxy'] = $c['proxy']; - } - - $res = $client->request('GET', $c['httpurl'], $options); - $output = $res->getBody()->getContents(); - } elseif(isset($c['httpresponsetime'])) { - $client = new GuzzleHttp\Client(); $output = NULL; - - $options = [ - 'on_stats' => function (GuzzleHttp\TransferStats $stats) use (&$output) { - $output = $stats->getTransferTime(); - } - ]; - if (isset($c['proxy'])) { - $options['proxy'] = $c['proxy']; + 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(); + }; } - $res = $client->request('GET', $c['httpresponsetime'], $options); + $res = $client->request('GET', $c['http']['url'], $options); + + if (isset($c['http']['data']) && $c['http']['data'] == 'responsebody') { + $output = $res->getBody()->getContents(); + } } + if(isset($c['jsonelem'])) { $c['value'] = getArrayValue($c['jsonelem'], json_decode($output, true)); } else { @@ -43,7 +39,7 @@ if($_SERVER["REQUEST_URI"] == '/metrics') { $loader = new \Twig\Loader\FilesystemLoader('templates'); $twig = new \Twig\Environment($loader, [ - 'cache' => 'twig_cache', + 'cache' => 'twig_cache', ]); echo $twig->render('metrics.twig', ['config' => $config]); }