Compare commits

..

2 Commits

Author SHA1 Message Date
f06c804c24
Updated response handling and added 404 page
The code changes include an update to the response handling in index.php. Now, it checks if the request URI is '/metrics'. If so, it continues with the previous behavior of executing a script and outputting its result. If not, it loads a new 404 error page using Twig templating engine.

A new file '404.twig' has been added under templates directory which serves as a custom 404 error page. It displays the requested URI and provides a link back to '/metrics'.
2024-06-13 16:03:44 +02:00
f7f3cb9704
Refactored script for better readability
The code has been refactored to improve readability and maintainability. The main changes include removing unnecessary condition checks, simplifying the flow of control, and reorganizing the code blocks for better logical grouping. This should make it easier to understand and modify in the future.
2024-06-13 15:52:08 +02:00
3 changed files with 125 additions and 105 deletions

View File

@ -1,6 +1,17 @@
<?php <?php
header('Content-Type: text/plain; version=0.0.4');
$config = getenv('CONFIG_FILE'); if($_SERVER["REQUEST_URI"] == '/metrics') {
$phpbin = getenv('PHP_BINARY'); header('Content-Type: text/plain; version=0.0.4');
$output = shell_exec('CONFIG_FILE="' . $config . '" ' . $phpbin . ' ' . $_SERVER['DOCUMENT_ROOT'] . 'script.php'); $config = getenv('CONFIG_FILE');
echo $output; $phpbin = getenv('PHP_BINARY');
$output = shell_exec('CONFIG_FILE="' . $config . '" ' . $phpbin . ' ' . $_SERVER['DOCUMENT_ROOT'] . 'script.php');
echo $output;
} else {
require_once('vendor/autoload.php');
http_response_code(404);
$loader = new \Twig\Loader\FilesystemLoader('templates');
$twig = new \Twig\Environment($loader, [
'cache' => 'twig_cache',
]);
echo $twig->render('404.twig', ['path' => $_SERVER["REQUEST_URI"]]);
}

View File

@ -14,9 +14,7 @@ if(file_exists($cacheFile)) {
} else { } else {
$oldconfig = []; $oldconfig = [];
} }
foreach ($config as $key => &$metric) {
if(php_sapi_name() == 'cli' || $_SERVER["REQUEST_URI"] == '/metrics') {
foreach ($config as $key => &$metric) {
if (!isset($metric['submetrics'])) { if (!isset($metric['submetrics'])) {
$metric['submetrics'] = [$metric]; $metric['submetrics'] = [$metric];
} }
@ -90,13 +88,13 @@ if(php_sapi_name() == 'cli' || $_SERVER["REQUEST_URI"] == '/metrics') {
exit(); exit();
} }
} }
} }
foreach ($pids as $pid) { foreach ($pids as $pid) {
pcntl_waitpid($pid, $none); pcntl_waitpid($pid, $none);
} }
foreach ($config as $key => &$metric) { foreach ($config as $key => &$metric) {
foreach ($metric['submetrics'] as $subkey => &$submetric) { foreach ($metric['submetrics'] as $subkey => &$submetric) {
$submetric['value'] = file_get_contents('/tmp/.metrics.' . $key . '.' . $subkey); $submetric['value'] = file_get_contents('/tmp/.metrics.' . $key . '.' . $subkey);
if (!empty($oldconfig) && !isset($submetric['value']) && (isset($submetric['fallback']) && !empty($submetric['fallback']))) { if (!empty($oldconfig) && !isset($submetric['value']) && (isset($submetric['fallback']) && !empty($submetric['fallback']))) {
@ -116,17 +114,16 @@ if(php_sapi_name() == 'cli' || $_SERVER["REQUEST_URI"] == '/metrics') {
} }
unlink('/tmp/.metrics.' . $key . '.' . $subkey); unlink('/tmp/.metrics.' . $key . '.' . $subkey);
} }
}
file_put_contents($cacheFile, json_encode($config, JSON_PRETTY_PRINT));
$loader = new \Twig\Loader\FilesystemLoader('templates');
$twig = new \Twig\Environment($loader, [
'cache' => 'twig_cache',
]);
echo $twig->render('metrics.twig', ['config' => $config]);
} }
file_put_contents($cacheFile, json_encode($config, JSON_PRETTY_PRINT));
$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) { function getArrayValue($elem, $array) {
$elem = explode('.', $elem); $elem = explode('.', $elem);
$new_array = $array; $new_array = $array;

12
templates/404.twig Normal file
View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>Page Title</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<style>
</style>
<body>
<h1>404 Uri {{ path }} not found</h1>
<p><a href="/metrics">/metrics</a></p>
</body>
</html>