Compare commits
2 Commits
fd8a67c23c
...
9707a3cfb9
Author | SHA1 | Date | |
---|---|---|---|
9707a3cfb9 | |||
dcf62d3865 |
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
config.php
|
config.php
|
||||||
vendor/
|
vendor/
|
||||||
twig_cache/
|
twig_cache/
|
||||||
|
metrics_cache.json
|
||||||
|
27
index.php
27
index.php
@ -1,6 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
require 'vendor/autoload.php';
|
require 'vendor/autoload.php';
|
||||||
require 'config.php';
|
require 'config.php';
|
||||||
|
if(file_exists('metrics_cache.json')) {
|
||||||
|
$oldconfig = json_decode(file_get_contents('metrics_cache.json'), true);
|
||||||
|
} else {
|
||||||
|
$oldconfig = [];
|
||||||
|
}
|
||||||
|
|
||||||
if(php_sapi_name() == 'cli' || $_SERVER["REQUEST_URI"] == '/metrics') {
|
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');
|
||||||
@ -8,7 +13,7 @@ if(php_sapi_name() == 'cli' || $_SERVER["REQUEST_URI"] == '/metrics') {
|
|||||||
if (!isset($metric['submetrics'])) {
|
if (!isset($metric['submetrics'])) {
|
||||||
$metric['submetrics'] = [$metric];
|
$metric['submetrics'] = [$metric];
|
||||||
}
|
}
|
||||||
foreach ($metric['submetrics'] as $key => &$submetric) {
|
foreach ($metric['submetrics'] as $subkey => &$submetric) {
|
||||||
|
|
||||||
if (isset($submetric['command'])) {
|
if (isset($submetric['command'])) {
|
||||||
$output = null;
|
$output = null;
|
||||||
@ -70,9 +75,28 @@ if(php_sapi_name() == 'cli' || $_SERVER["REQUEST_URI"] == '/metrics') {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (is_bool($submetric['value'])) $submetric['value'] = $submetric['value'] ? 1 : 0;
|
if (is_bool($submetric['value'])) $submetric['value'] = $submetric['value'] ? 1 : 0;
|
||||||
|
|
||||||
|
|
||||||
|
if(!empty($oldconfig) && empty($submetric['value']) && (isset($submetric['fallback']) && !empty($submetric['fallback']))) {
|
||||||
|
if(time() < ($oldconfig[$key]['submetrics'][$subkey]['time'] + $submetric['fallback']['maxage'])) {
|
||||||
|
if($submetric['fallback']['type'] == 'previous') {
|
||||||
|
$submetric['value'] = $oldconfig[$key]['submetrics'][$subkey]['value'];
|
||||||
|
} elseif ($submetric['fallback']['type'] == 'static') {
|
||||||
|
$submetric['value'] = $submetric['fallback']['value'];
|
||||||
|
}
|
||||||
|
$submetric['time'] = $oldconfig[$key]['submetrics'][$subkey]['time'];
|
||||||
|
} else {
|
||||||
|
$submetric['value'] = '';
|
||||||
|
$submetric['time'] = 0;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$submetric['time'] = time();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
file_put_contents('metrics_cache.json', json_encode($config, JSON_PRETTY_PRINT));
|
||||||
|
|
||||||
$loader = new \Twig\Loader\FilesystemLoader('templates');
|
$loader = new \Twig\Loader\FilesystemLoader('templates');
|
||||||
$twig = new \Twig\Environment($loader, [
|
$twig = new \Twig\Environment($loader, [
|
||||||
'cache' => 'twig_cache',
|
'cache' => 'twig_cache',
|
||||||
@ -84,6 +108,7 @@ function getArrayValue($elem, $array) {
|
|||||||
$elem = explode('.', $elem);
|
$elem = explode('.', $elem);
|
||||||
$new_array = $array;
|
$new_array = $array;
|
||||||
foreach ($elem as $i) {
|
foreach ($elem as $i) {
|
||||||
|
if(empty($new_array)) break;
|
||||||
$new_array = $new_array[$i];
|
$new_array = $new_array[$i];
|
||||||
}
|
}
|
||||||
return $new_array;
|
return $new_array;
|
||||||
|
Loading…
Reference in New Issue
Block a user