ts-website/api/status.php

77 lines
2.4 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__ . "/../include/cacheutils.class.php";
2016-06-28 21:18:59 +00:00
date_default_timezone_set($config["general"]["timezone"]);
$cacheutils = new CacheUtils('serverstatus');
2016-06-28 21:18:59 +00:00
if($cacheutils->isExpired()) {
$cacheutils->setValue(getResult(), 10);
2016-06-28 21:18:59 +00:00
}
die ($cacheutils->getValue());
2016-06-28 21:18:59 +00:00
// *********
// 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) {
2016-06-28 21:18:59 +00:00
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"
);
}
}