Compare commits

..

No commits in common. "069e550a3fda3fd3208b054f9d5eaef48411212f" and "24f1f6c974caa2d9571f74321d57b595e2de28bd" have entirely different histories.

2 changed files with 44 additions and 52 deletions

View File

@ -2,28 +2,23 @@
require 'vendor/autoload.php'; require 'vendor/autoload.php';
require 'config.php'; require 'config.php';
if(php_sapi_name() == 'cli' || $_SERVER["REQUEST_URI"] == '/metrics') { if($_SERVER["REQUEST_URI"] == '/metrics' || php_sapi_name() == 'cli') {
header('Content-Type: text/plain; version=0.0.4'); header('Content-Type: text/plain; version=0.0.4');
foreach ($config as $key => &$metric) { foreach ($config as $key => &$c) {
if (!isset($metric['submetrics'])) { if(isset($c['command'])) {
$metric['submetrics'] = [$metric];
}
foreach ($metric['submetrics'] as $key => &$submetric) {
if (isset($submetric['command'])) {
$output=null; $output=null;
$retval=null; $retval=null;
exec($submetric['command'], $output, $retval); exec($c['command'], $output, $retval);
$output = implode("\n", $output); $output = implode("\n", $output);
} elseif (isset($submetric['http'])) { } elseif(isset($c['http'])) {
$client = new GuzzleHttp\Client(); $client = new GuzzleHttp\Client();
$options = []; $options = [];
$output = NULL; $output = NULL;
if (isset($submetric['http']['proxy'])) { if (isset($c['http']['proxy'])) {
$options['proxy'] = $submetric['http']['proxy']; $options['proxy'] = $c['http']['proxy'];
} }
if (isset($submetric['http']['data']) && $submetric['http']['data'] == 'responsetime') { if (isset($c['http']['data']) && $c['http']['data'] == 'responsetime') {
$options['on_stats'] = function (GuzzleHttp\TransferStats $stats) use (&$output) { $options['on_stats'] = function (GuzzleHttp\TransferStats $stats) use (&$output) {
$output = $stats->getTransferTime(); $output = $stats->getTransferTime();
}; };
@ -31,7 +26,7 @@ if(php_sapi_name() == 'cli' || $_SERVER["REQUEST_URI"] == '/metrics') {
$hasresponse = NULL; $hasresponse = NULL;
try{ try{
$res = $client->request('GET', $submetric['http']['url'], $options); $res = $client->request('GET', $c['http']['url'], $options);
$hasresponse = true; $hasresponse = true;
} catch(GuzzleHttp\Exception\GuzzleException $e) { } catch(GuzzleHttp\Exception\GuzzleException $e) {
if(method_exists($e, 'getResponse')) { if(method_exists($e, 'getResponse')) {
@ -42,21 +37,20 @@ if(php_sapi_name() == 'cli' || $_SERVER["REQUEST_URI"] == '/metrics') {
} }
} }
if (isset($submetric['http']['data']) && $submetric['http']['data'] == 'responsebody') { if (isset($c['http']['data']) && $c['http']['data'] == 'responsebody') {
$output = $res->getBody()->getContents(); $output = $res->getBody()->getContents();
} elseif (isset($submetric['http']['data']) && $submetric['http']['data'] == 'hasresponse') { } elseif (isset($c['http']['data']) && $c['http']['data'] == 'hasresponse') {
$output = (int)$hasresponse; $output = (int)$hasresponse;
} }
} }
if (isset($submetric['jsonelem'])) { if(isset($c['jsonelem'])) {
$submetric['value'] = getArrayValue($submetric['jsonelem'], json_decode($output, true)); $c['value'] = getArrayValue($c['jsonelem'], json_decode($output, true));
} else { } else {
$submetric['value'] = $output; $c['value'] = $output;
} }
if (is_bool($submetric['value'])) $submetric['value'] = $submetric['value'] ? 1 : 0; if(is_bool($c['value'])) $c['value'] = $c['value'] ? 1 : 0;
}
} }
$loader = new \Twig\Loader\FilesystemLoader('templates'); $loader = new \Twig\Loader\FilesystemLoader('templates');

View File

@ -1,10 +1,8 @@
{% for key, metric in config %} {% for key, elem in config %}
{% if metric.help is defined and metric.help is not empty %} {% if elem.help is defined and elem.help is not empty %}
# HELP {{ key }} {{ metric.help }} # HELP {{ key }} {{ elem.help }}
{% endif %} {% endif %}
# TYPE {{ key }} gauge # TYPE {{ key }} gauge
{% for subkey, submetric in metric.submetrics %} {{ key }} {{ elem.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 %} {% endfor %}