Compare commits
3 Commits
81255d0b27
...
09bba2a0c3
Author | SHA1 | Date | |
---|---|---|---|
09bba2a0c3 | |||
8ac31b0092 | |||
2ffac2254c |
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,4 +1,3 @@
|
||||
config.php
|
||||
config*.php*
|
||||
vendor/
|
||||
twig_cache/
|
||||
metrics_cache.json
|
||||
|
@ -1,31 +1,94 @@
|
||||
<?php
|
||||
|
||||
$config = [
|
||||
'app_demo_metric1' => [ // Key is being used a prometheus key
|
||||
'help' => 'Help text for metric',
|
||||
'command' => 'The command to be run',
|
||||
'jsonelem' => '0.metric', // The json element where the metric is found (Dot notated)
|
||||
],
|
||||
'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
|
||||
'cpu_usage' => [
|
||||
'help' => 'CPU usage percentage',
|
||||
'type' => 'gauge',
|
||||
'submetrics' => [
|
||||
[
|
||||
'command' => 'top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk \'{print 100 - $1}\'',
|
||||
],
|
||||
],
|
||||
],
|
||||
'app_demo_metric4' => [
|
||||
'help' => 'Help text for metric',
|
||||
'http' => [
|
||||
'url' => 'http://example.com', // The url to get collected for the metric
|
||||
'data' => 'responsetime', // This will return the transfer time
|
||||
'proxy' => 'http://192.168.1.252:8080', // The proxy server the request needs to use
|
||||
'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' => [
|
||||
'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
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
16
index.php
16
index.php
@ -1,8 +1,16 @@
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
require 'config.php';
|
||||
if(file_exists('metrics_cache.json')) {
|
||||
$oldconfig = json_decode(file_get_contents('metrics_cache.json'), true);
|
||||
$configFile = getenv('CONFIG_FILE') ?: 'config.php';
|
||||
$cacheFile = $configFile . '.cache.json';
|
||||
|
||||
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 {
|
||||
$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');
|
||||
$twig = new \Twig\Environment($loader, [
|
||||
|
@ -2,7 +2,7 @@
|
||||
{% if metric.help is defined and metric.help is not empty %}
|
||||
# HELP {{ key }} {{ metric.help }}
|
||||
{% 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 %}
|
||||
{{ 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 %}
|
||||
|
Loading…
Reference in New Issue
Block a user