diff --git a/index.php b/index.php index d127095..1310042 100644 --- a/index.php +++ b/index.php @@ -3,52 +3,52 @@ require 'vendor/autoload.php'; require 'config.php'; 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(); + 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(); - $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(); - }; - } + $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(); + }; + } + + $res = $client->request('GET', $c['http']['url'], $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['http']['data']) && $c['http']['data'] == 'responsebody') { - $output = $res->getBody()->getContents(); - } - } + if(isset($c['jsonelem'])) { + $c['value'] = getArrayValue($c['jsonelem'], json_decode($output, true)); + } else { + $c['value'] = $output; + } + } - 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]); + $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; } \ No newline at end of file