Skip to content

Instantly share code, notes, and snippets.

@thakyZ
Forked from dkesberg/status.php
Last active January 31, 2021 20:30
Show Gist options
  • Save thakyZ/1db21b30ceee42b3b0cb968a5b204888 to your computer and use it in GitHub Desktop.
Save thakyZ/1db21b30ceee42b3b0cb968a5b204888 to your computer and use it in GitHub Desktop.
A short server info page, showing server status and player info
<?php
/**
* @author Daniel Kesberg <kesberg@gmail.com>
* @copyright (c) 2013, Daniel Kesberg
*/
error_reporting(E_ALL);
ini_set('display_errors', false);
$parseTimeStart = microtime(1);
// load config
// Set paths.log to the full path of the log file
$config = array(
'server' => array(
'hostname' => '127.0.0.1',
'port' => 21024
),
'paths' => array(
'log' => '/home/sbserver/serverfiles/storage/starbound_server.log'
)
);
// read logfile
$log = file_get_contents($config['paths']['log']);
// split into lines
$logLines = explode("\n", $log);
// only keep lines with client info
$logClients = array_filter($logLines, function($line) {
return preg_match('/^\[[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}\] \[Info\] UniverseServer: Client/', $line);
});
// extract clients
$clients = array();
foreach ($logClients as $line) {
if (preg_match('/.*?\\\'(.*)\\\'.*?\(.*?\)(.*)/i', $line, $matches) == 1) {
if (strlen($matches[1])) {
$clientName = htmlentities(trim($matches[1]));
$clients[$clientName] = trim($matches[2]);
}
}
}
// only keep "connected" status
$clients = array_filter($clients, function($status) {
return trim($status) == 'connected';
});
// sort
ksort($clients);
// get server version
// [11:05:34.847] [Info] Server Version 1.4.4 (linux x86_64) Source ID: 8cbe6faf22282659828a194e06a08999f213769e Protocol: 747
$logVersion = array_filter($logLines, function($line) {
return preg_match('/^\[[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}\] \[Info\] Server Version/', $line);
});
// only look at the last line
$logVersion = end($logVersion);
$tmp = explode(" ", $logVersion);
$logVersion = $tmp[4];
// server is running ?
// edit: removed shell cmd, stole fsockopen from https://gist.github.com/lagonnebula/7928214 ;)
$fp = fsockopen($config['server']['hostname'], $config['server']['port'], $errno, $errstr, 30);
if ($fp){
$serverIsRunning = 1;
fclose($fp);
} else {
$serverIsRunning = 0;
}
// output
?>
<html>
<head>
<title>Starbound Server Info</title>
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css">
<style type="text/css">
body {
padding-top: 70px;
}
table > tbody > tr > td.server-status {
vertical-align: middle;
}
</style>
</head>
<body>
<div class="navbar navbar-dark bg-dark fixed-top" role="navigation">
<div class="container">
<a class="navbar-brand" href="#">Starbound Server Info</a>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="card mb-3">
<div class="card-header"><span><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M1.543 7.25h2.733c.144-2.074.866-3.756 1.58-4.948.12-.197.237-.381.353-.552a6.506 6.506 0 00-4.666 5.5zm2.733 1.5H1.543a6.506 6.506 0 004.666 5.5 11.13 11.13 0 01-.352-.552c-.715-1.192-1.437-2.874-1.581-4.948zm1.504 0h4.44a9.637 9.637 0 01-1.363 4.177c-.306.51-.612.919-.857 1.215a9.978 9.978 0 01-.857-1.215A9.637 9.637 0 015.78 8.75zm4.44-1.5H5.78a9.637 9.637 0 011.363-4.177c.306-.51.612-.919.857-1.215.245.296.55.705.857 1.215A9.638 9.638 0 0110.22 7.25zm1.504 1.5c-.144 2.074-.866 3.756-1.58 4.948-.12.197-.237.381-.353.552a6.506 6.506 0 004.666-5.5h-2.733zm2.733-1.5h-2.733c-.144-2.074-.866-3.756-1.58-4.948a11.738 11.738 0 00-.353-.552 6.506 6.506 0 014.666 5.5zM8 0a8 8 0 100 16A8 8 0 008 0z"></path></svg></span> Server</div>
<div class="card-body">
<table class="table table-condensed table-bordered">
<tbody>
<tr>
<th>Status</th>
<td class="server-status">
<span class="badge bg-<?php echo ($serverIsRunning == 1) ? 'success' : 'danger' ; ?>">
<?php echo ($serverIsRunning == 1) ? 'Online' : 'Offline' ; ?>
</span>
</td>
</tr>
<tr>
<th>Version</th>
<td><?php echo $logVersion; ?></td>
</tr>
<tr>
<th>IP</th>
<td><?php echo $_SERVER['SERVER_ADDR']; ?></td>
</tr>
<tr>
<th>Players Online</th>
<td><?php echo count($clients); ?></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="card mb-3">
<div class="card-header"><span><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M10.5 5a2.5 2.5 0 11-5 0 2.5 2.5 0 015 0zm.061 3.073a4 4 0 10-5.123 0 6.004 6.004 0 00-3.431 5.142.75.75 0 001.498.07 4.5 4.5 0 018.99 0 .75.75 0 101.498-.07 6.005 6.005 0 00-3.432-5.142z"></path></svg></span> Players</div>
<div class="card-body">
<?php if (count($clients)): ?>
<table class="table table-condensed table-bordered">
<thead>
<tr>
<th>Playername</th>
</tr>
</thead>
<tbody>
<?php foreach ($clients as $client => $status): ?>
<tr>
<td>
<?php echo $client; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php else: ?>
No active players
<?php endif; ?>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<span class="badge bg-secondary">
Parse time: <?php echo microtime(1) - $parseTimeStart; ?> seconds.
</span>
</div>
</div>
</div>
</body>
</html>
@thakyZ
Copy link
Author

thakyZ commented Jan 31, 2021

Updated the script to work with Bootstrap 5 and fixed log fetching issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment