Compare commits

..

3 Commits

4 changed files with 102 additions and 32 deletions

3
.gitignore vendored
View File

@ -1,4 +1,3 @@
config.php config*.php*
vendor/ vendor/
twig_cache/ twig_cache/
metrics_cache.json

View File

@ -1,31 +1,94 @@
<?php <?php
$config = [ $config = [
'app_demo_metric1' => [ // Key is being used a prometheus key 'cpu_usage' => [
'help' => 'Help text for metric', 'help' => 'CPU usage percentage',
'command' => 'The command to be run', 'type' => 'gauge',
'jsonelem' => '0.metric', // The json element where the metric is found (Dot notated) 'submetrics' => [
[
'command' => 'top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk \'{print 100 - $1}\'',
], ],
'app_demo_metric2' => [ ],
'help' => 'Help text for metric', ],
'memory_usage' => [
'help' => 'Memory usage percentage',
'type' => 'gauge',
'submetrics' => [
[
'command' => 'free | grep Mem | awk \'{print $3/$2 * 100.0}\'',
],
],
],
'disk_usage' => [
'help' => 'Disk usage percentage',
'type' => 'gauge',
'submetrics' => [
[
'command' => 'df -h / | grep / | awk \'{print $5}\' | sed \'s/%//g\'',
],
],
],
'website_response_time' => [
'help' => 'Response time of example.com in seconds',
'type' => 'gauge',
'submetrics' => [
[
'http' => [ 'http' => [
'url' => 'http://example.com', // The url to get collected for the metric 'url' => 'http://example.com',
'data' => 'responsebody', // The body is used as output 'data' => 'responsetime',
], ],
'jsonelem' => '0.metric',
], ],
'app_demo_metric3' => [ ],
'help' => 'Help text for metric', ],
'website_status' => [
'help' => 'HTTP status code of example.com',
'type' => 'gauge',
'submetrics' => [
[
'http' => [ 'http' => [
'url' => 'http://example.com', // The url to get collected for the metric 'url' => 'http://example.com',
'data' => 'hasresponse', // If an error occured during request this will return 0, otherwise 1 'data' => 'hasresponse',
'statuscode' => [200],
], ],
], ],
'app_demo_metric4' => [ ],
'help' => 'Help text for metric', ],
'http_request_count' => [
'help' => 'Number of HTTP requests to example.com',
'type' => 'counter',
'submetrics' => [
[
'http' => [ 'http' => [
'url' => 'http://example.com', // The url to get collected for the metric 'url' => 'http://example.com',
'data' => 'responsetime', // This will return the transfer time 'data' => 'hasresponse',
'proxy' => 'http://192.168.1.252:8080', // The proxy server the request needs to use ],
],
],
],
'proxied_request' => [
'help' => 'Response time of example.com via proxy in seconds',
'type' => 'gauge',
'submetrics' => [
[
'http' => [
'url' => 'http://example.com',
'data' => 'responsetime',
'proxy' => 'http://your-proxy-server:8080',
],
],
],
],
'json_data_metric' => [
'help' => 'Value from JSON response of example.com',
'type' => 'gauge',
'submetrics' => [
[
'http' => [
'url' => 'http://example.com/api/data',
'data' => 'responsebody',
],
'jsonelem' => 'path.to.value', // Adjust the JSON path to the actual structure
],
], ],
], ],
]; ];

View File

@ -1,8 +1,16 @@
<?php <?php
require 'vendor/autoload.php'; require 'vendor/autoload.php';
require 'config.php'; $configFile = getenv('CONFIG_FILE') ?: 'config.php';
if(file_exists('metrics_cache.json')) { $cacheFile = $configFile . '.cache.json';
$oldconfig = json_decode(file_get_contents('metrics_cache.json'), true);
if (file_exists($configFile)) {
require ($configFile);
} else {
echo ("Configuration file not found: " . $configFile);
exit(1);
}
if(file_exists($cacheFile)) {
$oldconfig = json_decode(file_get_contents($cacheFile), true);
} else { } else {
$oldconfig = []; $oldconfig = [];
} }
@ -95,7 +103,7 @@ if(php_sapi_name() == 'cli' || $_SERVER["REQUEST_URI"] == '/metrics') {
} }
} }
file_put_contents('metrics_cache.json', json_encode($config, JSON_PRETTY_PRINT)); file_put_contents($cacheFile, 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, [

View File

@ -2,7 +2,7 @@
{% if metric.help is defined and metric.help is not empty %} {% if metric.help is defined and metric.help is not empty %}
# HELP {{ key }} {{ metric.help }} # HELP {{ key }} {{ metric.help }}
{% endif %} {% endif %}
# TYPE {{ key }} gauge # TYPE {{ key }} {% if metric.type is defined and metric.type is not empty %}{{ metric.help }}{% else %}gauge{% endif %}
{% for subkey, submetric in metric.submetrics %} {% 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 }} {{ 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 %}