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.
TS-Website/api/status.php

77 lines
2.4 KiB

9 years ago
<?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";
9 years ago
date_default_timezone_set($config["general"]["timezone"]);
$cacheutils = new CacheUtils('serverstatus');
9 years ago
if($cacheutils->isExpired()) {
$cacheutils->setValue(getResult(), 60);
9 years ago
}
die ($cacheutils->getValue());
9 years ago
// *********
// METHODS
// *********
function getResult() {
try {
$start = microtime(true);
9 years ago
$tsstatus = getTeamspeakServerStatus();
9 years ago
$stop = microtime(true);
9 years ago
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
9 years ago
)));
}
function exception_error_handler($errno, $errstr, $errfile, $errline) {
9 years ago
scriptFail("[$errfile @ $errline] " . $errstr);
}
function getTeamspeakServerStatus() {
9 years ago
$response = pingTeamspeakServerFromConfig();
9 years ago
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"
);
}
}