ts-website/api/status.php

86 lines
2.5 KiB
PHP
Raw Normal View History

2016-06-28 21:18:59 +00:00
<?php
// error_reporting(0);
header('Content-Type: application/json');
set_error_handler("exception_error_handler", E_ALL);
require_once __DIR__ . "/../include/tsutils.php";
require_once __DIR__ . "/../lib/phpfastcache/autoload.php";
date_default_timezone_set($config["general"]["timezone"]);
2016-06-28 21:18:59 +00:00
use phpFastCache\Util;
use phpFastCache\CacheManager;
Util\Languages::setEncoding("UTF-8");
$cache = CacheManager::Files();
$serverstatus = $cache->get('serverstatus');
// $cache->clean();
if (is_null($serverstatus)) {
$serverstatus = getResult();
$cache->set('serverstatus', $serverstatus, 10);
}
die ($serverstatus);
// *********
// METHODS
// *********
function getResult() {
try {
$start = microtime(true);
2016-07-01 20:34:55 +00:00
2016-06-28 21:18:59 +00:00
$tsstatus = getTeamspeakServerStatus();
2016-07-01 20:34:55 +00:00
2016-06-28 21:18:59 +00:00
$stop = microtime(true);
2016-07-01 20:34:55 +00:00
2016-06-28 21:18:59 +00:00
return json_encode(array(
"tsstatus" => $tsstatus,
"generated" => date('d-m-Y H:i:s')
));
} catch (Exception $e) {
scriptFail($e);
}
}
function scriptFail($error) {
die(json_encode(array(
"success" => false,
"id" => "script_error",
"message" => "There has been an error while retrieving the server status",
"error" => $error
2016-06-28 21:18:59 +00:00
)));
}
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
scriptFail("[$errfile @ $errline] " . $errstr);
}
function getTeamspeakServerStatus() {
2016-07-01 20:34:55 +00:00
2016-06-28 21:18:59 +00:00
$response = pingTeamspeakServerFromConfig();
2016-07-01 20:34:55 +00:00
2016-06-28 21:18:59 +00:00
if ($response) {
return array(
"success" => $response["virtualserver_status"]->toString() == "online",
"name" => $response["virtualserver_name"]->toString(),
"clientsonline" => $response["virtualserver_clientsonline"] - $response["virtualserver_queryclientsonline"],
"maxclients" => $response["virtualserver_maxclients"],
"version" => TeamSpeak3_Helper_Convert::versionShort($response["virtualserver_version"]->toString())->toString(),
"platform" => $response["virtualserver_platform"]->toString(),
"uptime" => TeamSpeak3_Helper_Convert::seconds($response["virtualserver_uptime"], false, "%dd %02dh %02dm"),
"averagePacketloss" => $response["virtualserver_total_packetloss_total"]->toString(),
"averagePing" => $response["virtualserver_total_ping"]->toString()
);
} else {
return array(
"success" => false,
"id" => "not_responding",
"message" => "Server is not responding"
);
}
}