Advertisement
Guest User

server stat

a guest
Dec 16th, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.90 KB | None | 0 0
  1. <?php
  2. /**
  3. * @author Daniel Kesberg <kesberg@ebene3.com>
  4. * @copyright (c) 2013, Daniel Kesberg
  5. */
  6.  
  7. error_reporting(E_ALL);
  8. ini_set('display_errors', false);
  9.  
  10. $parseTimeStart = microtime();
  11.  
  12. // load config
  13. // Set paths.log to the full path of the log file
  14. $config = array(
  15. 'server' => array(
  16. 'hostname' => '127.0.0.1',
  17. 'port' => 21025
  18. ),
  19. 'paths' => array(
  20. 'log' => 'd:/games/Steam/steamApps/common/Starbound/starbound_server.log'
  21. )
  22. );
  23.  
  24. // read logfile
  25. $log = file_get_contents($config['paths']['log']);
  26.  
  27. // split into lines
  28. $logLines = explode("\n", $log);
  29.  
  30. // split into lines
  31. $logChatLines = explode("\n", $log);
  32.  
  33. // only keep lines with client info
  34. $logClients = array_filter($logLines, function($line) {
  35. return preg_match('/^Info:.*Client/', $line);
  36. });
  37.  
  38. // '/!Info\:\s*<([^>]{2,})>\s?(.*)!i/'
  39. // only keep lines with client info
  40. $chatStore = array_filter($logLines, function($line) {
  41. return preg_match('/^Info\:\s\s/', $line);
  42. });
  43.  
  44. // extract chat
  45.  
  46.  
  47. // extract clients
  48. $clients = array();
  49. foreach ($logClients as $line) {
  50. if (preg_match('/.*?\\\'(.*)\\\'.*?\(.*?\)(.*)/is', $line, $matches) == 1) {
  51. if (strlen($matches[1])) {
  52. $clients[$matches[1]] = trim($matches[2]);
  53. }
  54. }
  55. }
  56.  
  57.  
  58. // only keep "connected" status
  59. $clients = array_filter($clients, function($status) {
  60. return trim($status) == 'connected';
  61. });
  62.  
  63. // sort
  64. ksort($clients);
  65.  
  66. // get server version
  67. $logVersion = array_filter($logLines, function($line) {
  68. return preg_match('/^Info: Server version/', $line);
  69. });
  70.  
  71. // only look at the last line
  72. $logVersion = end($logVersion);
  73. $tmp = explode("'", $logVersion);
  74. $logVersion = $tmp[1];
  75.  
  76. // server is running ?
  77. // edit: removed shell cmd, stole fsockopen from https://gist.github.com/lagonnebula/7928214 ;)
  78. $fp = fsockopen($config['server']['hostname'], $config['server']['port'], $errno, $errstr, 30);
  79. if ($fp){
  80. $serverIsRunning = 1;
  81. fclose($fp);
  82. } else {
  83. $serverIsRunning = 0;
  84. }
  85.  
  86. // output
  87. ?>
  88. <html>
  89. <head>
  90. <title>Starbound Server Info</title>
  91. <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
  92. <style type="text/css">
  93. body {
  94. padding-top: 70px;
  95. }
  96. table > tbody > tr > td.server-status {
  97. vertical-align: middle;
  98. }
  99. </style>
  100. <script>
  101. window.onload=function() {
  102. var elem = document.getElementById('users');
  103. var elem = document.getElementById('chat');
  104. elem.scrollTop = elem.scrollHeight;
  105. };
  106. </script>
  107. <META HTTP-EQUIV=Refresh CONTENT="10">
  108. </head>
  109. <body>
  110. <div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
  111. <div class="container">
  112. <div class="navbar-header">
  113. <a class="navbar-brand" href="#">Starbound Server Info</a>
  114. </div>
  115. </div>
  116. </div>
  117.  
  118. <div class="container">
  119. <div class="row">
  120. <div class="col-md-12">
  121. <div class="panel panel-default">
  122. <div class="panel-heading"><span class="glyphicon glyphicon-globe"></span> Server</div>
  123. <div class="panel-body">
  124. <table class="table table-condensed table-bordered">
  125. <tbody>
  126. <tr>
  127. <th>Status</th>
  128. <td class="server-status">
  129. <span class="label label-<?php echo ($serverIsRunning == 1) ? 'success' : 'danger' ; ?>">
  130. <?php echo ($serverIsRunning == 1) ? 'Online' : 'Offline';
  131.  
  132. ?>
  133. </span>
  134. </td>
  135. </tr>
  136. <tr>
  137. <th>Version</th>
  138. <td><?php echo $logVersion; ?></td>
  139. </tr>
  140. <tr>
  141. <th>IP</th>
  142. <td><?php echo 'www.colony-nyan.com'; ?></td>
  143. </tr>
  144. <tr>
  145. <th>Players Online</th>
  146. <td><?php echo count($clients); ?></td>
  147. </tr>
  148. </tbody>
  149. </table>
  150. </div>
  151. </div>
  152. </div>
  153. </div>
  154. <div class="row">
  155. <div class="col-md-12">
  156. <div class="panel panel-default" style="float:left;width:300;">
  157. <div class="panel-heading"><span class="glyphicon glyphicon-user"></span> Players</div>
  158. <div class="panel-body" style="height:400px;overflow:auto;overflow-y:scroll;" id="users">
  159. <?php if (count($clients)): ?>
  160. <table class="table table-condensed table-bordered">
  161. <thead>
  162. </thead>
  163. <tbody>
  164. <?php foreach ($clients as $client => $status): ?>
  165. <tr>
  166. <td>
  167. <?php echo htmlspecialchars($client); ?>
  168. </td>
  169. </tr>
  170. <?php endforeach; ?>
  171. </tbody>
  172. </table>
  173. <?php else: ?>
  174. No active players
  175. <?php endif; ?>
  176. </div>
  177. </div>
  178. <div class="panel panel-default">
  179. <div class="panel-heading">&nbsp&nbsp&nbsp<span class="glyphicon glyphicon-user"></span> Chat</div>
  180. <div class="panel-body" style="height:400px;overflow:auto;overflow-y:scroll;" id="chat">
  181. <?php if (count($clients)): ?>
  182. <table class="table table-condensed table-bordered">
  183. <thead>
  184. </thead>
  185. <tbody>
  186. <?php foreach ($chatStore as $Chats): ?>
  187. <tr>
  188. <td>
  189. <?php echo htmlspecialchars($Chats); ?>
  190. </td>
  191. </tr>
  192. <?php endforeach; ?>
  193. </tbody>
  194. </table>
  195. <?php else: ?>
  196. No active players
  197. <?php endif; ?>
  198. </div>
  199. </div>
  200. </div>
  201. </div>
  202. <div class="row">
  203. <div class="col-md-12">
  204. <span class="label label-default">
  205. Parse time: <?php echo microtime() - $parseTimeStart; ?> seconds.
  206. </span>
  207. </div>
  208. </div>
  209. </div>
  210. </body>
  211. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement