add ts3_up metric to signal failed query connection

master
lub 5 years ago
parent 6aa58afab0
commit b0e9ec65a0

@ -1,6 +1,7 @@
<?php <?php
$help_texts = [ $help_texts = [
'up' => 'Query connection; 1 = success, 0 = fail',
'virtualserver_clientsonline' => 'Clients online (e.g. Users)', 'virtualserver_clientsonline' => 'Clients online (e.g. Users)',
'virtualserver_queryclientsonline' => 'Query clients online (e.g. this exporter)', 'virtualserver_queryclientsonline' => 'Query clients online (e.g. this exporter)',
'virtualserver_total_packetloss_speech' => 'Average packet loss', 'virtualserver_total_packetloss_speech' => 'Average packet loss',

@ -3,15 +3,6 @@ require_once('../vendor/autoload.php');
require_once('help.php'); // provides $help_texts require_once('help.php'); // provides $help_texts
$host = 'localhost';
$port = 10011;
$user = rawurlencode('serveradmin');
$password = rawurlencode('4NReP1Wf');
$ssh = TRUE;
$uri = 'serverquery://'.$user.':'.$password.'@'.$host.':'.$port.'/';
// initialize the Prometheus registry // initialize the Prometheus registry
$registry = new \Prometheus\CollectorRegistry(new \Prometheus\Storage\InMemory()); $registry = new \Prometheus\CollectorRegistry(new \Prometheus\Storage\InMemory());
$namespace = 'ts3'; $namespace = 'ts3';
@ -20,47 +11,64 @@ $virtualserver_label_keys = [
'virtualserver_port', 'virtualserver_port',
'virtualserver_unique_identifier' 'virtualserver_unique_identifier'
]; ];
$totals = [
'virtualserver_uptime' // set ${namespace}_up for exporter status
]; $up = $registry->getOrRegisterGauge(
$namespace,
'up',
$help_texts['up'],
[]
);
$up->set(1, []);
// open a connection to the server $host = '192.168.178.55';
$ts3 = TeamSpeak3::factory($uri); $port = 10011;
$user = rawurlencode('serveradmin');
$password = rawurlencode('4NReP1Wf');
// loop over all virtualservers, because some statistics (e.g. packet loss) $uri = 'serverquery://'.$user.':'.$password.'@'.$host.':'.$port.'/';
// are tracked per virtualserver
foreach($ts3->serverList() as $virtualserver) {
$infos = array_map(function($info_raw) {
return is_object($info_raw) ? $info_raw->toString() : $info_raw;
}, $virtualserver->getInfo());
// fill $virtualserver_label_values try {
foreach($virtualserver_label_keys as $virtualserver_label_key) { // open a connection to the server
$value = $infos[$virtualserver_label_key]; $ts3 = TeamSpeak3::factory($uri);
$virtualserver_label_values[] = $infos[$virtualserver_label_key];
}
foreach($infos as $name => $data) { // loop over all virtualservers, because some statistics (e.g. packet loss)
// gauge only supports floats // are tracked per virtualserver
// $infos/$data is a mix of int, string and NULL foreach($ts3->serverList() as $virtualserver) {
if(preg_match('/^\d*(\.\d*)?$/', $data)) { $infos = array_map(function($info_raw) {
$gauge = $registry->getOrRegisterGauge( return is_object($info_raw) ? $info_raw->toString() : $info_raw;
$namespace, }, $virtualserver->getInfo());
$name,
array_key_exists($name, $help_texts) ? $help_texts[$name] : 'no help text available', // fill $virtualserver_label_values
preg_match('/^virtualserver_/', $name) ? $virtualserver_label_keys : [] foreach($virtualserver_label_keys as $virtualserver_label_key) {
); $virtualserver_label_values[] = $infos[$virtualserver_label_key];
$gauge->set( }
(float) $data,
preg_match('/^virtualserver_/', $name) ? $virtualserver_label_values : [] foreach($infos as $name => $data) {
); // gauge only supports floats
// $infos/$data is a mix of int, string and NULL
if(preg_match('/^\d*(\.\d*)?$/', $data)) {
$gauge = $registry->getOrRegisterGauge(
$namespace,
$name,
array_key_exists($name, $help_texts) ? $help_texts[$name] : 'no help text available',
preg_match('/^virtualserver_/', $name) ? $virtualserver_label_keys : []
);
$gauge->set(
(float) $data,
preg_match('/^virtualserver_/', $name) ? $virtualserver_label_values : []
);
}
} }
} }
} catch (Exception $e) {
// something failed, set ${namespace}_up to 0
$up->set(0, []);
} }
// render all metrics as HTML and output them // render all metrics and output them
$renderer = new \Prometheus\RenderTextFormat(); $renderer = new \Prometheus\RenderTextFormat();
$result = $renderer->render($registry->getMetricFamilySamples()); $result = $renderer->render($registry->getMetricFamilySamples());

Loading…
Cancel
Save