| 1 | <?php
|
|---|
| 2 |
|
|---|
| 3 | // Script executed by cron every 5 minutes
|
|---|
| 4 |
|
|---|
| 5 | include('../includes/config.php');
|
|---|
| 6 |
|
|---|
| 7 | $Database = new mysqli($db_config['host'], $db_config['user'], $db_config['pass'], $db_config['name']);
|
|---|
| 8 |
|
|---|
| 9 | if(mysqli_connect_errno())
|
|---|
| 10 | {
|
|---|
| 11 | printf("Connect failed: %s\n", mysqli_connect_error());
|
|---|
| 12 | exit();
|
|---|
| 13 | }
|
|---|
| 14 |
|
|---|
| 15 | function CheckPortStatus($Ip, $Port)
|
|---|
| 16 | {
|
|---|
| 17 | $Timeout = 1;
|
|---|
| 18 | if($Socket = @fsockopen($Ip, $Port, $ErrorNumber, $ErrorString, $Timeout))
|
|---|
| 19 | {
|
|---|
| 20 | fclose($Socket);
|
|---|
| 21 | return(1);
|
|---|
| 22 | } else
|
|---|
| 23 | {
|
|---|
| 24 | return(0);
|
|---|
| 25 | }
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | $TextState = array('Offline', 'Online');
|
|---|
| 29 |
|
|---|
| 30 | echo("Kontroluji stav serveru...<br>\n");
|
|---|
| 31 | $PlayersCount = 0;
|
|---|
| 32 | $OnlineCount = 0;
|
|---|
| 33 | $OfflineCount = 0;
|
|---|
| 34 | $Servers = $Database->query("SELECT `id`,`ip`,`port`,`name` FROM `servers`");
|
|---|
| 35 | while($Server = $Servers->fetch_array())
|
|---|
| 36 | {
|
|---|
| 37 | echo($Server['name'].' - ');
|
|---|
| 38 | $MeasureStart = microtime(TRUE);
|
|---|
| 39 | $OnlineState = CheckPortStatus($Server['ip'], $Server['port']);
|
|---|
| 40 | $Latency = (microtime(TRUE) - $MeasureStart) * 1000;
|
|---|
| 41 | $OnlineCount += $OnlineState;
|
|---|
| 42 | $OfflineCount += 1 - $OnlineState;
|
|---|
| 43 | // Insert new online state
|
|---|
| 44 | $Database->query("INSERT INTO `servers_online` (`server`, `time`, `state`, `players_count`, `latency`) VALUES (".$Server['id'].", NOW() , ".$OnlineState.", ".$PlayersCount.", ".$Latency.")");
|
|---|
| 45 |
|
|---|
| 46 | // Delete old records
|
|---|
| 47 | $Database->query("DELETE FROM `servers_online` WHERE `server` = ".$Server['id']." AND time < DATE_SUB(NOW(), INTERVAL ".$Config['OnlineStatePeriodLength']." DAY)");
|
|---|
| 48 |
|
|---|
| 49 | // Calculate latency average
|
|---|
| 50 | $DbResult = $Database->query("SELECT COALESCE(AVG(`latency`), 0) as `latency` FROM `servers_online` WHERE `server` = ".$Server['id']." AND `state` = 1 AND `latency` != 0");
|
|---|
| 51 | $DbRow = $DbResult->fetch_array();
|
|---|
| 52 | $Latency = $DbRow['latency'];
|
|---|
| 53 | $DbResult->free();
|
|---|
| 54 |
|
|---|
| 55 | // Calculate cached percentage online state
|
|---|
| 56 | $DbResult = $Database->query("SELECT COALESCE(COUNT(*), 1) as `count`, COALESCE(SUM(`state`), 0) as `online` FROM `servers_online` WHERE `server` = ".$Server['id']);
|
|---|
| 57 | $DbRow = $DbResult->fetch_array();
|
|---|
| 58 |
|
|---|
| 59 | // Update servers info
|
|---|
| 60 | $Database->query("UPDATE `servers` SET `online` = ".round($DbRow['online'] / $DbRow['count'] * 100, 2).", `latency` = ".$Latency." WHERE `id` = ".$Server['id']);
|
|---|
| 61 | $DbResult->free();
|
|---|
| 62 | echo($TextState[$OnlineState]."<br>\n");
|
|---|
| 63 | flush();
|
|---|
| 64 | }
|
|---|
| 65 | $Servers->free();
|
|---|
| 66 |
|
|---|
| 67 | echo('Total: '.($OnlineCount + $OfflineCount).', Online: '.$OnlineCount.', Offline: '.$OfflineCount."<br>\n");
|
|---|
| 68 |
|
|---|
| 69 | ?>
|
|---|