Compare commits

...

2 Commits

Author SHA1 Message Date
069e550a3f
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.
2024-06-03 23:47:40 +02:00
41f2ddea37
Reorder conditional for CLI check, then URI comparison.
- Reordered the condition to first check for CLI mode before URI comparison.
2024-06-03 23:41:42 +02:00
2 changed files with 51 additions and 43 deletions

View File

@ -2,34 +2,39 @@
require 'vendor/autoload.php'; require 'vendor/autoload.php';
require 'config.php'; require 'config.php';
if($_SERVER["REQUEST_URI"] == '/metrics' || php_sapi_name() == 'cli') { if(php_sapi_name() == 'cli' || $_SERVER["REQUEST_URI"] == '/metrics') {
header('Content-Type: text/plain; version=0.0.4'); header('Content-Type: text/plain; version=0.0.4');
foreach ($config as $key => &$c) { foreach ($config as $key => &$metric) {
if(isset($c['command'])) { if (!isset($metric['submetrics'])) {
$output=null; $metric['submetrics'] = [$metric];
$retval=null; }
exec($c['command'], $output, $retval); foreach ($metric['submetrics'] as $key => &$submetric) {
if (isset($submetric['command'])) {
$output = null;
$retval = null;
exec($submetric['command'], $output, $retval);
$output = implode("\n", $output); $output = implode("\n", $output);
} elseif(isset($c['http'])) { } elseif (isset($submetric['http'])) {
$client = new GuzzleHttp\Client(); $client = new GuzzleHttp\Client();
$options = []; $options = [];
$output = NULL; $output = NULL;
if (isset($c['http']['proxy'])) { if (isset($submetric['http']['proxy'])) {
$options['proxy'] = $c['http']['proxy']; $options['proxy'] = $submetric['http']['proxy'];
} }
if (isset($c['http']['data']) && $c['http']['data'] == 'responsetime') { if (isset($submetric['http']['data']) && $submetric['http']['data'] == 'responsetime') {
$options['on_stats'] = function (GuzzleHttp\TransferStats $stats) use (&$output) { $options['on_stats'] = function (GuzzleHttp\TransferStats $stats) use (&$output) {
$output = $stats->getTransferTime(); $output = $stats->getTransferTime();
}; };
} }
$hasresponse = NULL; $hasresponse = NULL;
try{ try {
$res = $client->request('GET', $c['http']['url'], $options); $res = $client->request('GET', $submetric['http']['url'], $options);
$hasresponse = true; $hasresponse = true;
} catch(GuzzleHttp\Exception\GuzzleException $e) { } catch (GuzzleHttp\Exception\GuzzleException $e) {
if(method_exists($e, 'getResponse')) { if (method_exists($e, 'getResponse')) {
$res = $e->getResponse(); $res = $e->getResponse();
$hasresponse = true; $hasresponse = true;
} else { } else {
@ -37,20 +42,21 @@ if($_SERVER["REQUEST_URI"] == '/metrics' || php_sapi_name() == 'cli') {
} }
} }
if (isset($c['http']['data']) && $c['http']['data'] == 'responsebody') { if (isset($submetric['http']['data']) && $submetric['http']['data'] == 'responsebody') {
$output = $res->getBody()->getContents(); $output = $res->getBody()->getContents();
} elseif (isset($c['http']['data']) && $c['http']['data'] == 'hasresponse') { } elseif (isset($submetric['http']['data']) && $submetric['http']['data'] == 'hasresponse') {
$output = (int)$hasresponse; $output = (int)$hasresponse;
} }
} }
if(isset($c['jsonelem'])) { if (isset($submetric['jsonelem'])) {
$c['value'] = getArrayValue($c['jsonelem'], json_decode($output, true)); $submetric['value'] = getArrayValue($submetric['jsonelem'], json_decode($output, true));
} else { } else {
$c['value'] = $output; $submetric['value'] = $output;
} }
if(is_bool($c['value'])) $c['value'] = $c['value'] ? 1 : 0; if (is_bool($submetric['value'])) $submetric['value'] = $submetric['value'] ? 1 : 0;
}
} }
$loader = new \Twig\Loader\FilesystemLoader('templates'); $loader = new \Twig\Loader\FilesystemLoader('templates');

View File

@ -1,8 +1,10 @@
{% for key, elem in config %} {% for key, metric in config %}
{% if elem.help is defined and elem.help is not empty %} {% if metric.help is defined and metric.help is not empty %}
# HELP {{ key }} {{ elem.help }} # HELP {{ key }} {{ metric.help }}
{% endif %} {% endif %}
# TYPE {{ key }} gauge # 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 %} {% endfor %}