You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

76 lines
2.4 KiB

<?php
require_once('../vendor/autoload.php');
require_once('help.php'); // provides $help_texts
// initialize the Prometheus registry
$registry = new \Prometheus\CollectorRegistry(new \Prometheus\Storage\InMemory());
$namespace = 'ts3';
$virtualserver_label_keys = [
'virtualserver_id',
'virtualserver_port',
'virtualserver_unique_identifier'
];
// set ${namespace}_up for exporter status
$up = $registry->getOrRegisterGauge(
$namespace,
'up',
$help_texts['up'],
[]
);
$up->set(1, []);
$host = getenv('QUERY_HOST');
$port = getenv('QUERY_PORT');
$user = rawurlencode(getenv('QUERY_USER_NAME'));
$password = rawurlencode(trim(file_get_contents(getenv('QUERY_USER_PASSWORD_FILE'))));
$uri = 'serverquery://'.$user.':'.$password.'@'.$host.':'.$port.'/';
try {
// open a connection to the server
$ts3 = TeamSpeak3::factory($uri);
// loop over all virtualservers, because some statistics (e.g. packet loss)
// 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
foreach($virtualserver_label_keys as $virtualserver_label_key) {
$virtualserver_label_values[] = $infos[$virtualserver_label_key];
}
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 and output them
$renderer = new \Prometheus\RenderTextFormat();
$result = $renderer->render($registry->getMetricFamilySamples());
header('Content-type: ' . \Prometheus\RenderTextFormat::MIME_TYPE);
echo $result;