Skip to content

Instantly share code, notes, and snippets.

@Samuell1
Forked from dkesberg/status.php
Last active August 29, 2015 14:14
Show Gist options
  • Save Samuell1/23b4fd527f46cdca1e63 to your computer and use it in GitHub Desktop.
Save Samuell1/23b4fd527f46cdca1e63 to your computer and use it in GitHub Desktop.

Revisions

  1. @dkesberg dkesberg revised this gist Dec 15, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion status.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    <?php
    /**
    * @author Daniel Kesberg <kesberg@ebene3.com>
    * @author Daniel Kesberg <kesberg@gmail.com>
    * @copyright (c) 2013, Daniel Kesberg
    */

  2. @dkesberg dkesberg revised this gist Dec 15, 2013. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions status.php
    Original file line number Diff line number Diff line change
    @@ -29,15 +29,16 @@

    // only keep lines with client info
    $logClients = array_filter($logLines, function($line) {
    return preg_match('/^Info:.*<User/', $line);
    return preg_match('/^Info:.*Client/', $line);
    });

    // extract clients
    $clients = array();
    foreach ($logClients as $line) {
    if (preg_match('/<User: ([0-9a-zA-Z-\s]*)>\s+([a-z].*)/i', $line, $matches) == 1) {
    if (preg_match('/.*?\\\'(.*)\\\'.*?\(.*?\)(.*)/i', $line, $matches) == 1) {
    if (strlen($matches[1])) {
    $clients[$matches[1]] = trim($matches[2]);
    $clientName = htmlentities(trim($matches[1]));
    $clients[$clientName] = trim($matches[2]);
    }
    }
    }
  3. @dkesberg dkesberg revised this gist Dec 13, 2013. 1 changed file with 32 additions and 32 deletions.
    64 changes: 32 additions & 32 deletions status.php
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@
    'server' => array(
    'hostname' => '127.0.0.1',
    'port' => 21025
    ),
    ),
    'paths' => array(
    'log' => '/home/starbound-server/Steam/SteamApps/common/Starbound/linux64/starbound_server.log'
    )
    @@ -29,22 +29,22 @@

    // only keep lines with client info
    $logClients = array_filter($logLines, function($line) {
    return preg_match('/^Info:.*<User/', $line);
    return preg_match('/^Info:.*<User/', $line);
    });

    // extract clients
    $clients = array();
    foreach ($logClients as $line) {
    if (preg_match('/<User: ([0-9a-zA-Z-\s]*)>\s+([a-z].*)/i', $line, $matches) == 1) {
    if (strlen($matches[1])) {
    $clients[$matches[1]] = trim($matches[2]);
    }
    }
    if (preg_match('/<User: ([0-9a-zA-Z-\s]*)>\s+([a-z].*)/i', $line, $matches) == 1) {
    if (strlen($matches[1])) {
    $clients[$matches[1]] = trim($matches[2]);
    }
    }
    }

    // only keep "connected" status
    $clients = array_filter($clients, function($status) {
    return trim($status) == 'connected';
    return trim($status) == 'connected';
    });

    // sort
    @@ -105,22 +105,22 @@
    <tr>
    <th>Status</th>
    <td class="server-status">
    <span class="label label-<?= ($serverIsRunning == 1) ? 'success' : 'danger' ; ?>">
    <?= ($serverIsRunning == 1) ? 'Online' : 'Offline' ; ?>
    <span class="label label-<?php echo ($serverIsRunning == 1) ? 'success' : 'danger' ; ?>">
    <?php echo ($serverIsRunning == 1) ? 'Online' : 'Offline' ; ?>
    </span>
    </td>
    </tr>
    <tr>
    <th>Version</th>
    <td><?= $logVersion; ?></td>
    </tr>
    <td><?php echo $logVersion; ?></td>
    </tr>
    <tr>
    <th>IP</th>
    <td><?= $_SERVER['SERVER_ADDR']; ?></td>
    <td><?php echo $_SERVER['SERVER_ADDR']; ?></td>
    </tr>
    <tr>
    <th>Players Online</th>
    <td><?= count($clients); ?></td>
    <td><?php echo count($clients); ?></td>
    </tr>
    </tbody>
    </table>
    @@ -133,23 +133,23 @@
    <div class="panel panel-default">
    <div class="panel-heading"><span class="glyphicon glyphicon-user"></span> Players</div>
    <div class="panel-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>
    <?= $client; ?>
    </td>
    </tr>
    <?php endforeach; ?>
    </tbody>
    </table>
    <?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; ?>
    @@ -160,7 +160,7 @@
    <div class="row">
    <div class="col-md-12">
    <span class="label label-default">
    Parse time: <?= microtime() - $parseTimeStart; ?> seconds.
    Parse time: <?php echo microtime() - $parseTimeStart; ?> seconds.
    </span>
    </div>
    </div>
  4. @dkesberg dkesberg revised this gist Dec 12, 2013. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions status.php
    Original file line number Diff line number Diff line change
    @@ -47,6 +47,9 @@
    return trim($status) == 'connected';
    });

    // sort
    ksort($clients);

    // get server version
    $logVersion = array_filter($logLines, function($line) {
    return preg_match('/^Info: Server version/', $line);
  5. @dkesberg dkesberg revised this gist Dec 12, 2013. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions status.php
    Original file line number Diff line number Diff line change
    @@ -37,14 +37,14 @@
    foreach ($logClients as $line) {
    if (preg_match('/<User: ([0-9a-zA-Z-\s]*)>\s+([a-z].*)/i', $line, $matches) == 1) {
    if (strlen($matches[1])) {
    $clients[$matches[1]] = $matches[2];
    $clients[$matches[1]] = trim($matches[2]);
    }
    }
    }

    // only keep "connected" status
    $clients = array_filter($clients, function($status) {
    return $status == 'connected';
    return trim($status) == 'connected';
    });

    // get server version
    @@ -110,6 +110,10 @@
    <tr>
    <th>Version</th>
    <td><?= $logVersion; ?></td>
    </tr>
    <tr>
    <th>IP</th>
    <td><?= $_SERVER['SERVER_ADDR']; ?></td>
    </tr>
    <tr>
    <th>Players Online</th>
  6. @dkesberg dkesberg revised this gist Dec 12, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion status.php
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    */

    error_reporting(E_ALL);
    ini_set('display_errors', true);
    ini_set('display_errors', false);

    $parseTimeStart = microtime();

  7. @dkesberg dkesberg renamed this gist Dec 12, 2013. 1 changed file with 7 additions and 8 deletions.
    15 changes: 7 additions & 8 deletions clients.php → status.php
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,8 @@
    * @copyright (c) 2013, Daniel Kesberg
    */

    ini_set('display_errors', false);
    error_reporting(E_ALL);
    ini_set('display_errors', true);

    $parseTimeStart = microtime();

    @@ -34,13 +35,11 @@
    // extract clients
    $clients = array();
    foreach ($logClients as $line) {
    $tmp = explode('<User: ', $line);
    $tmp = str_replace('>','', $tmp[1]);
    $tmp = explode(' ', $tmp);

    if (count($tmp) == 2) {
    $clients[$tmp[0]] = $tmp[1];
    }
    if (preg_match('/<User: ([0-9a-zA-Z-\s]*)>\s+([a-z].*)/i', $line, $matches) == 1) {
    if (strlen($matches[1])) {
    $clients[$matches[1]] = $matches[2];
    }
    }
    }

    // only keep "connected" status
  8. @dkesberg dkesberg revised this gist Dec 12, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions clients.php
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    * @copyright (c) 2013, Daniel Kesberg
    */

    error_reporting(0);
    ini_set('display_errors', false);

    $parseTimeStart = microtime();

    @@ -103,7 +103,7 @@
    <tr>
    <th>Status</th>
    <td class="server-status">
    <span class="label label-<?= ($serverIsRunning == 1) ? 'success' : 'error' ; ?>">
    <span class="label label-<?= ($serverIsRunning == 1) ? 'success' : 'danger' ; ?>">
    <?= ($serverIsRunning == 1) ? 'Online' : 'Offline' ; ?>
    </span>
    </td>
  9. @dkesberg dkesberg revised this gist Dec 12, 2013. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions clients.php
    Original file line number Diff line number Diff line change
    @@ -4,8 +4,7 @@
    * @copyright (c) 2013, Daniel Kesberg
    */

    error_reporting(E_ALL);
    ini_set('display_errors', true);
    error_reporting(0);

    $parseTimeStart = microtime();

  10. @dkesberg dkesberg revised this gist Dec 12, 2013. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion clients.php
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,9 @@
    * @copyright (c) 2013, Daniel Kesberg
    */

    error_reporting(E_ALL);
    ini_set('display_errors', true);

    $parseTimeStart = microtime();

    // load config
    @@ -76,6 +79,9 @@
    body {
    padding-top: 70px;
    }
    table > tbody > tr > td.server-status {
    vertical-align: middle;
    }
    </style>
    </head>
    <body>
    @@ -97,7 +103,11 @@
    <tbody>
    <tr>
    <th>Status</th>
    <td><?= ($serverIsRunning == 1) ? 'Online' : 'Offline' ; ?></td>
    <td class="server-status">
    <span class="label label-<?= ($serverIsRunning == 1) ? 'success' : 'error' ; ?>">
    <?= ($serverIsRunning == 1) ? 'Online' : 'Offline' ; ?>
    </span>
    </td>
    </tr>
    <tr>
    <th>Version</th>
  11. @dkesberg dkesberg revised this gist Dec 12, 2013. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions clients.php
    Original file line number Diff line number Diff line change
    @@ -7,8 +7,7 @@
    $parseTimeStart = microtime();

    // load config
    // $config = require_once __DIR__ . '/../app/config/app.php';
    // Set this to the full path of the log file
    // Set paths.log to the full path of the log file
    $config = array(
    'server' => array(
    'hostname' => '127.0.0.1',
  12. @dkesberg dkesberg revised this gist Dec 12, 2013. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions clients.php
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,11 @@
    // $config = require_once __DIR__ . '/../app/config/app.php';
    // Set this to the full path of the log file
    $config = array(
    'paths' => array(
    'server' => array(
    'hostname' => '127.0.0.1',
    'port' => 21025
    ),
    'paths' => array(
    'log' => '/home/starbound-server/Steam/SteamApps/common/Starbound/linux64/starbound_server.log'
    )
    );
    @@ -55,7 +59,7 @@

    // server is running ?
    // edit: removed shell cmd, stole fsockopen from https://gist.github.com/lagonnebula/7928214 ;)
    $fp = fsockopen('127.0.0.1', 21025, $errno, $errstr, 30);
    $fp = fsockopen($config['server']['hostname'], $config['server']['port'], $errno, $errstr, 30);
    if ($fp){
    $serverIsRunning = 1;
    fclose($fp);
  13. @dkesberg dkesberg revised this gist Dec 12, 2013. 1 changed file with 8 additions and 10 deletions.
    18 changes: 8 additions & 10 deletions clients.php
    Original file line number Diff line number Diff line change
    @@ -54,16 +54,14 @@
    $logVersion = $tmp[1];

    // server is running ?
    // this is just a quick and dirty way
    // you dont actually want to exec shell commands in php
    // and if your server is in safe_mode (which it should be) it might not even be possible to run this
    // the better way would be to set up a cron job with:
    // netstat -at | grep :21025 | grep LISTEN | wc -l > path/to/log/directory/server_status.log
    // and then have the php script read the log file
    // $serverIsRunning = intval(trim(file_get_contents('path/to/log/directory/server_status.log')));
    $cmdRunning = 'netstat -at | grep :21025 | grep LISTEN | wc -l';
    exec($cmdRunning, $serverIsRunning);
    $serverIsRunning = intval($serverIsRunning[0]);
    // edit: removed shell cmd, stole fsockopen from https://gist.github.com/lagonnebula/7928214 ;)
    $fp = fsockopen('127.0.0.1', 21025, $errno, $errstr, 30);
    if ($fp){
    $serverIsRunning = 1;
    fclose($fp);
    } else {
    $serverIsRunning = 0;
    }

    // output
    ?>
  14. @dkesberg dkesberg revised this gist Dec 12, 2013. 1 changed file with 73 additions and 26 deletions.
    99 changes: 73 additions & 26 deletions clients.php
    Original file line number Diff line number Diff line change
    @@ -15,21 +15,20 @@
    )
    );

    $clients = array();

    // read logfile
    $log = file_get_contents($config['paths']['log']);

    // split into lines
    $log = explode("\n", $log);
    $logLines = explode("\n", $log);

    // only keep lines with client info
    $log = array_filter($log, function($line) {
    $logClients = array_filter($logLines, function($line) {
    return preg_match('/^Info:.*<User/', $line);
    });

    // extract clients
    foreach ($log as $line) {
    $clients = array();
    foreach ($logClients as $line) {
    $tmp = explode('<User: ', $line);
    $tmp = str_replace('>','', $tmp[1]);
    $tmp = explode(' ', $tmp);
    @@ -44,16 +43,37 @@
    return $status == 'connected';
    });

    // get server version
    $logVersion = array_filter($logLines, function($line) {
    return preg_match('/^Info: Server version/', $line);
    });

    // only look at the last line
    $logVersion = end($logVersion);
    $tmp = explode("'", $logVersion);
    $logVersion = $tmp[1];

    // server is running ?
    // this is just a quick and dirty way
    // you dont actually want to exec shell commands in php
    // and if your server is in safe_mode (which it should be) it might not even be possible to run this
    // the better way would be to set up a cron job with:
    // netstat -at | grep :21025 | grep LISTEN | wc -l > path/to/log/directory/server_status.log
    // and then have the php script read the log file
    // $serverIsRunning = intval(trim(file_get_contents('path/to/log/directory/server_status.log')));
    $cmdRunning = 'netstat -at | grep :21025 | grep LISTEN | wc -l';
    exec($cmdRunning, $serverIsRunning);
    $serverIsRunning = intval($serverIsRunning[0]);

    // output
    // var_dump($clients);
    ?>
    <html>
    <head>
    <title>Starbound Server Info</title>
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
    <style type="text/css">
    body {
    padding-top: 50px;
    padding-top: 70px;
    }
    </style>
    </head>
    @@ -69,31 +89,58 @@
    <div class="container">
    <div class="row">
    <div class="col-md-12">
    <h3>Active Players: <?= count($clients); ?></h3>
    <div class="panel panel-default">
    <div class="panel-heading"><span class="glyphicon glyphicon-globe"></span> Server</div>
    <div class="panel-body">
    <table class="table table-condensed table-bordered">
    <tbody>
    <tr>
    <th>Status</th>
    <td><?= ($serverIsRunning == 1) ? 'Online' : 'Offline' ; ?></td>
    </tr>
    <tr>
    <th>Version</th>
    <td><?= $logVersion; ?></td>
    </tr>
    <tr>
    <th>Players Online</th>
    <td><?= count($clients); ?></td>
    </tr>
    </tbody>
    </table>
    </div>
    </div>
    </div>
    </div>
    <?php if (count($clients)): ?>
    <div class="row">
    <div class="col-md-12">
    <table class="table table-striped table-bordered">
    <thead>
    <tr>
    <th>Playername</th>
    </tr>
    </thead>
    <tbody>
    <?php foreach ($clients as $client => $status): ?>
    <tr>
    <td>
    <?= $client; ?>
    </td>
    </tr>
    <?php endforeach; ?>
    </tbody>
    </table>
    <div class="panel panel-default">
    <div class="panel-heading"><span class="glyphicon glyphicon-user"></span> Players</div>
    <div class="panel-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>
    <?= $client; ?>
    </td>
    </tr>
    <?php endforeach; ?>
    </tbody>
    </table>
    <?php else: ?>
    No active players
    <?php endif; ?>
    </div>
    </div>
    </div>
    </div>
    <?php endif; ?>
    <div class="row">
    <div class="col-md-12">
    <span class="label label-default">
  15. @dkesberg dkesberg created this gist Dec 12, 2013.
    106 changes: 106 additions & 0 deletions clients.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,106 @@
    <?php
    /**
    * @author Daniel Kesberg <[email protected]>
    * @copyright (c) 2013, Daniel Kesberg
    */

    $parseTimeStart = microtime();

    // load config
    // $config = require_once __DIR__ . '/../app/config/app.php';
    // Set this to the full path of the log file
    $config = array(
    'paths' => array(
    'log' => '/home/starbound-server/Steam/SteamApps/common/Starbound/linux64/starbound_server.log'
    )
    );

    $clients = array();

    // read logfile
    $log = file_get_contents($config['paths']['log']);

    // split into lines
    $log = explode("\n", $log);

    // only keep lines with client info
    $log = array_filter($log, function($line) {
    return preg_match('/^Info:.*<User/', $line);
    });

    // extract clients
    foreach ($log as $line) {
    $tmp = explode('<User: ', $line);
    $tmp = str_replace('>','', $tmp[1]);
    $tmp = explode(' ', $tmp);

    if (count($tmp) == 2) {
    $clients[$tmp[0]] = $tmp[1];
    }
    }

    // only keep "connected" status
    $clients = array_filter($clients, function($status) {
    return $status == 'connected';
    });

    // output
    // var_dump($clients);
    ?>
    <html>
    <head>
    <title>Starbound Server Info</title>
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
    <style type="text/css">
    body {
    padding-top: 50px;
    }
    </style>
    </head>
    <body>
    <div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
    <div class="container">
    <div class="navbar-header">
    <a class="navbar-brand" href="#">Starbound Server Info</a>
    </div>
    </div>
    </div>

    <div class="container">
    <div class="row">
    <div class="col-md-12">
    <h3>Active Players: <?= count($clients); ?></h3>
    </div>
    </div>
    <?php if (count($clients)): ?>
    <div class="row">
    <div class="col-md-12">
    <table class="table table-striped table-bordered">
    <thead>
    <tr>
    <th>Playername</th>
    </tr>
    </thead>
    <tbody>
    <?php foreach ($clients as $client => $status): ?>
    <tr>
    <td>
    <?= $client; ?>
    </td>
    </tr>
    <?php endforeach; ?>
    </tbody>
    </table>
    </div>
    </div>
    <?php endif; ?>
    <div class="row">
    <div class="col-md-12">
    <span class="label label-default">
    Parse time: <?= microtime() - $parseTimeStart; ?> seconds.
    </span>
    </div>
    </div>
    </div>
    </body>
    </html>