[PHP] Server Status Script

Results 1 to 10 of 10
  1. #1
    I can has title? hamisgood87 is offline
    MemberRank
    Feb 2012 Join Date
    GoogleLocation
    447Posts

    [PHP] Server Status Script

    Hey guys, I started working on a server status script for fiesta. Basically the game uses services which broadcast to specific ports when they are running. My only question is, How does this look? So basically i was just wondering if this method is the right one to use and if there is any major flaws I should look into. I am not asking for any answers just hints to point me in the right direction.

    PHP Code:
    <?php
    function check_port($port) {
        
    $conn = @fsockopen($_SERVER['SERVER_ADDR'], $port$errno$errstr0.2);
        if (
    $conn) {
            
    fclose($conn);
            return 
    true;
        }
    }

    function 
    server_report() {
        
    $report = array();
        
    $svcs = array('9110'=>'World',
        
    '9120'=>'Zone1',
        
    '9122'=>'Zone2',
        
    '9124'=>'Zone3',
        
    '9010'=>'Login');
        
        foreach (
    $svcs as $port=>$service) {
            
    $report[$service] = check_port($port);
        }
        return 
    $report;
    }

    $report server_report();
    echo
    "<table>
        <tr>
            <td>Server</td>
            <td>Status</td>
      <tr>
            <td>All Servers</td>
            <td>"
    ; echo $report['World']&$report['Zone1']&$report['Zone2']&$report['Zone3']&$report['Login'] ? 'Online' 'Offline'; echo "</td>
        </tr>
    </table>"
    ;
    ?>


  2. #2
    Omega Ron is offline
    MemberRank
    Apr 2005 Join Date
    Location
    8,990Posts

    Re: [PHP] Server Status Script

    Ideally you don't want this to run every time someone visits your website. You'd get constant requests going out of your server on each page load this is called on, which can eventually have a performance impact.

    I would suggest caching the online/offline return and have a cron running every 5 minutes or so to update the status. Now obviously it wont show offline as soon as the server is down, but there is really no reason to have it update any more less 5 minutes between each call.

    Otherwise you included the timeout which people seem to leave out, so everything else looks OK to me really other than "$_SERVER['SERVER_ADDR']". Is there any reason you're using this over just setting 'localhost' assuming this is on the same box as the game server?

    edit-
    Moving to showcase as there isn't really an "issue" to be fixed.
    Last edited by Ron; 10-07-13 at 09:04 PM.

  3. #3
    I can has title? hamisgood87 is offline
    MemberRank
    Feb 2012 Join Date
    GoogleLocation
    447Posts

    Re: [PHP] Server Status Script

    I tried to change it to localhost but it always returns offline. Leaving "$_SERVER['SERVER_ADDR']" should be good though right?

    Also would i use php headers to cache? i haven't done much work with caching.

  4. #4
    Praise the Sun! Solaire is offline
    MemberRank
    Dec 2007 Join Date
    Undead BurgLocation
    2,862Posts

    Re: [PHP] Server Status Script

    Quote Originally Posted by hamisgood87 View Post
    I tried to change it to localhost but it always returns offline. Leaving "$_SERVER['SERVER_ADDR']" should be good though right?
    Try and change the timeout to 3 seconds instead of 0.2.

    Quote Originally Posted by hamisgood87 View Post
    Also would i use php headers to cache? i haven't done much work with caching.
    You'd be using a simple textfile that contains the data. Serializing and deserializing it would do just fine. Here's a sample:
    PHP Code:
    <?php
    // Cache timeout in milliseconds
    define("CACHE_TIMEOUT",    300);

    function 
    buildCache() {
        
    // Should re-check the serverstatus here, using static stuff for now
        
    $rgServerStatus = array(
            
    "server1" => "offline",
            
    "server2" => "online"
        
    );
        
        
    $pFile fopen("cache.txt""w");
        
        if (
    $pFile !== false) {
            
    fputs($pFileserialize($rgServerStatus));
            
    fclose($pFile);
        }
        
        return 
    $rgServerStatus;
    }

    function 
    loadCache() {
        
    $pFile fopen("cache.txt""r");
        
    $rgServerStatus = array();
        
        if (
    $pFile !== false) {
            
    $rgServerStatus deserialize(fgets($pFile));
            
    fclose($pFile);
        }
        
        return 
    $rgServerStatus;
    }

    // Check if cache expired and either load cache or rebuild cache
    $rgServerStatus = (filemtime("cache.txt") < (time() + CACHE_TIMEOUT) ? buildCache() : loadCache());
    var_dump($rgServerStatus);
    ?>

  5. #5
    Banned bugme is offline
    BannedRank
    Feb 2007 Join Date
    1,380Posts

    Re: [PHP] Server Status Script

    Just a stupid question from me.. Can you like also use this for your VPS or website or anything else if so can you give the code to that i would like to try it out.

    No i can't code PHP

    Verstuurd van mijn GT-I9070 met Tapatalk

  6. #6
    Praise the Sun! Solaire is offline
    MemberRank
    Dec 2007 Join Date
    Undead BurgLocation
    2,862Posts

    Re: [PHP] Server Status Script

    Quote Originally Posted by Notice View Post
    Just a stupid question from me.. Can you like also use this for your VPS or website or anything else if so can you give the code to that i would like to try it out.

    No i can't code PHP

    Verstuurd van mijn GT-I9070 met Tapatalk
    You mean modifying the script so it would monitor a website / VPS?

  7. #7
    Banned bugme is offline
    BannedRank
    Feb 2007 Join Date
    1,380Posts

    Re: [PHP] Server Status Script

    Quote Originally Posted by Wizkidje View Post
    You mean modifying the script so it would monitor a website / VPS?
    That sir is correct so yes.. So i can monitor all my VPS servers and Reseller server.

    Verstuurd van mijn GT-I9070 met Tapatalk

  8. #8
    Praise the Sun! Solaire is offline
    MemberRank
    Dec 2007 Join Date
    Undead BurgLocation
    2,862Posts

    Re: [PHP] Server Status Script

    Quote Originally Posted by Notice View Post
    That sir is correct so yes.. So i can monitor all my VPS servers and Reseller server.

    Verstuurd van mijn GT-I9070 met Tapatalk
    That'd work, but if you're referring to VPS servers as if you're hosting them, then there's already plenty of other scripts that do that for you such as WHCMS.

  9. #9
    Banned bugme is offline
    BannedRank
    Feb 2007 Join Date
    1,380Posts

    Re: [PHP] Server Status Script

    Thank you for that but i just want a single code and I'm sure soneone here can provide it and if not then it's fine too.

    Verstuurd van mijn GT-I9070 met Tapatalk

  10. #10
    Praise the Sun! Solaire is offline
    MemberRank
    Dec 2007 Join Date
    Undead BurgLocation
    2,862Posts

    Re: [PHP] Server Status Script

    Quote Originally Posted by Notice View Post
    Thank you for that but i just want a single code and I'm sure soneone here can provide it and if not then it's fine too.

    Verstuurd van mijn GT-I9070 met Tapatalk
    Create your own thread and I'm sure you'll get what you need.



Advertisement