From 069e550a3fda3fd3208b054f9d5eaef48411212f Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Mon, 3 Jun 2024 23:47:40 +0200 Subject: [PATCH] Refactor metric retrieval and display Improved handling of submetrics for better data organization and presentation. Updated metric values based on retrieved data, ensuring accurate representation in the metrics template file. --- index.php | 82 ++++++++++++++++++++++-------------------- templates/metrics.twig | 10 +++--- 2 files changed, 50 insertions(+), 42 deletions(-) diff --git a/index.php b/index.php index 0cc6d81..f8d8a04 100644 --- a/index.php +++ b/index.php @@ -4,53 +4,59 @@ require 'config.php'; if(php_sapi_name() == 'cli' || $_SERVER["REQUEST_URI"] == '/metrics') { header('Content-Type: text/plain; version=0.0.4'); - 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 => &$metric) { + if (!isset($metric['submetrics'])) { + $metric['submetrics'] = [$metric]; + } + foreach ($metric['submetrics'] as $key => &$submetric) { - $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(); - }; - } + if (isset($submetric['command'])) { + $output = null; + $retval = null; + exec($submetric['command'], $output, $retval); + $output = implode("\n", $output); + } elseif (isset($submetric['http'])) { + $client = new GuzzleHttp\Client(); - $hasresponse = NULL; - try{ - $res = $client->request('GET', $c['http']['url'], $options); - $hasresponse = true; - } catch(GuzzleHttp\Exception\GuzzleException $e) { - if(method_exists($e, 'getResponse')) { - $res = $e->getResponse(); + $options = []; + $output = NULL; + if (isset($submetric['http']['proxy'])) { + $options['proxy'] = $submetric['http']['proxy']; + } + if (isset($submetric['http']['data']) && $submetric['http']['data'] == 'responsetime') { + $options['on_stats'] = function (GuzzleHttp\TransferStats $stats) use (&$output) { + $output = $stats->getTransferTime(); + }; + } + + $hasresponse = NULL; + try { + $res = $client->request('GET', $submetric['http']['url'], $options); $hasresponse = true; - } else { - $hasresponse = false; + } catch (GuzzleHttp\Exception\GuzzleException $e) { + if (method_exists($e, 'getResponse')) { + $res = $e->getResponse(); + $hasresponse = true; + } else { + $hasresponse = false; + } + } + + if (isset($submetric['http']['data']) && $submetric['http']['data'] == 'responsebody') { + $output = $res->getBody()->getContents(); + } elseif (isset($submetric['http']['data']) && $submetric['http']['data'] == 'hasresponse') { + $output = (int)$hasresponse; } } - 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($submetric['jsonelem'])) { + $submetric['value'] = getArrayValue($submetric['jsonelem'], json_decode($output, true)); + } else { + $submetric['value'] = $output; } - } - if(isset($c['jsonelem'])) { - $c['value'] = getArrayValue($c['jsonelem'], json_decode($output, true)); - } else { - $c['value'] = $output; + if (is_bool($submetric['value'])) $submetric['value'] = $submetric['value'] ? 1 : 0; } - - if(is_bool($c['value'])) $c['value'] = $c['value'] ? 1 : 0; } $loader = new \Twig\Loader\FilesystemLoader('templates'); diff --git a/templates/metrics.twig b/templates/metrics.twig index b60617c..3d3be84 100644 --- a/templates/metrics.twig +++ b/templates/metrics.twig @@ -1,8 +1,10 @@ -{% for key, elem in config %} -{% if elem.help is defined and elem.help is not empty %} -# HELP {{ key }} {{ elem.help }} +{% for key, metric in config %} +{% if metric.help is defined and metric.help is not empty %} +# HELP {{ key }} {{ metric.help }} {% endif %} # TYPE {{ key }} gauge -{{ key }} {{ elem.value }} +{% for subkey, submetric in metric.submetrics %} +{{ key }}{% if submetric.labels is defined and submetric.labels is not empty %}{{ '{' }}{% for label, value in submetric.labels %}{{ label }}="{{ value }}"{% if not loop.last %},{% endif %}{% endfor %}{{ '}' }}{% endif %} {{ submetric.value }} +{% endfor %} {% endfor %} \ No newline at end of file