Compare commits

..

No commits in common. "09bba2a0c307b03cfc3958759b38ef44daf08e04" and "81255d0b274490cc07fc8e5a44be944cb21970ae" have entirely different histories.

4 changed files with 32 additions and 102 deletions

3
.gitignore vendored
View File

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

View File

@ -1,94 +1,31 @@
<?php <?php
$config = [ $config = [
'cpu_usage' => [ 'app_demo_metric1' => [ // Key is being used a prometheus key
'help' => 'CPU usage percentage', 'help' => 'Help text for metric',
'type' => 'gauge', 'command' => 'The command to be run',
'submetrics' => [ 'jsonelem' => '0.metric', // The json element where the metric is found (Dot notated)
[ ],
'command' => 'top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk \'{print 100 - $1}\'', 'app_demo_metric2' => [
], 'help' => 'Help text for metric',
'http' => [
'url' => 'http://example.com', // The url to get collected for the metric
'data' => 'responsebody', // The body is used as output
],
'jsonelem' => '0.metric',
],
'app_demo_metric3' => [
'help' => 'Help text for metric',
'http' => [
'url' => 'http://example.com', // The url to get collected for the metric
'data' => 'hasresponse', // If an error occured during request this will return 0, otherwise 1
], ],
], ],
'memory_usage' => [ 'app_demo_metric4' => [
'help' => 'Memory usage percentage', 'help' => 'Help text for metric',
'type' => 'gauge', 'http' => [
'submetrics' => [ 'url' => 'http://example.com', // The url to get collected for the metric
[ 'data' => 'responsetime', // This will return the transfer time
'command' => 'free | grep Mem | awk \'{print $3/$2 * 100.0}\'', 'proxy' => 'http://192.168.1.252:8080', // The proxy server the request needs to use
],
], ],
], ],
'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' => [
'url' => 'http://example.com',
'data' => 'responsetime',
],
],
],
],
'website_status' => [
'help' => 'HTTP status code of example.com',
'type' => 'gauge',
'submetrics' => [
[
'http' => [
'url' => 'http://example.com',
'data' => 'hasresponse',
'statuscode' => [200],
],
],
],
],
'http_request_count' => [
'help' => 'Number of HTTP requests to example.com',
'type' => 'counter',
'submetrics' => [
[
'http' => [
'url' => 'http://example.com',
'data' => 'hasresponse',
],
],
],
],
'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,16 +1,8 @@
<?php <?php
require 'vendor/autoload.php'; require 'vendor/autoload.php';
$configFile = getenv('CONFIG_FILE') ?: 'config.php'; require 'config.php';
$cacheFile = $configFile . '.cache.json'; if(file_exists('metrics_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 = [];
} }
@ -103,7 +95,7 @@ if(php_sapi_name() == 'cli' || $_SERVER["REQUEST_URI"] == '/metrics') {
} }
} }
file_put_contents($cacheFile, json_encode($config, JSON_PRETTY_PRINT)); 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, [

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 }} {% if metric.type is defined and metric.type is not empty %}{{ metric.help }}{% else %}gauge{% endif %} # TYPE {{ key }} gauge
{% 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 %}