SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast Bhjorth's Avatar
    Join Date
    Mar 2011
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with server status function

    Hello all,
    I am programing a function that checks if a gameserver is up or down. Showing a Green arrow if the server is up and a red arrow if the server is down. THIS i have fixed and it works BUT I need to make it faster and better, and most important less load time.

    As it stands now it takes the webpage 7 seconds to load becuse I am checking 4 servers, So I need some good ideas and feedback.

    Here is the code so far.

    sidebar.php
    PHP Code:
    <?php
    require_once('CServerStatus.php');

    $ServerStatus = new CServerStatus();


    $ServerStatus->serverstatuscheck('random IP','random socket','random name');

    ?>
    CServerStatus.php
    PHP Code:
    <?php


    class CServerStatus {
        
        
    //-- Funktionen som kollar om serven är uppe eller nere ------------------------
    //------------------------------------------------------------------------------

            
    public function serverstatuscheck($ip,$port,$servername) {
                
                if (
    $check=@fsockopen($ip,$port,$ERROR_NO,$ERROR_STR,(float)0.5))
                {
                
    fclose($check);
                echo 
    '
                <div class="serverstyle">
                <img src=statusUP.gif" title="'
    .$servername.' is UP!" alt="'.$servername.' is UP!" >'.$servername.' is Up
                </div>'
    ;
                }
                else
                {
                echo 
    '
                <div class="serverstyle">
                <img src="statusDOWN.gif" title="'
    .$servername.' is DOWN!" alt="'.$servername.' is DOWN!">'.$servername.' is Down
                </div>'
    ;
                }
            
            }
    }
    ?>
    So please help me with some brainstorming, how should I go about to not give an extra load time for the user ?

    First thought was a Cron job - But have no idea how much power running a Cron job every 2 minutes takes, anyone know?

    Then I thought maybe save the info and time checked in to a table and when a user enter when the timer was set 2 minutes ago re connect to the servers and save the new time and the new data. then only one user gets the long load time every 2 minutes ?

    is there maybe a way to put a wait timer on the function and make it load AFTER the page has been loaded?

    Help please

    PS: I am no master programer and I am trying to make it OOP just because I am reading a online course about it atm.

  2. #2
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found this article for you:
    Easy Parallel Processing in PHP | PHP Everywhere

    It's kind of a crash course in parallel connection.. except it isn't really in parallel. It just basically calls the same function a few times, writes out via http, returns from the function and then gets the result a second or so later from each resource. I'm not convinced it will do you any favours though as it still depends on the actual connection to a port itself. I also found a result on process forking too but this article says not to use it in a webserver though it doesn't explain why (i can only imagine that if several people call the same url that it could lead to cpu lock):
    Process Forking with PHP

    I think personally i would just run a looping php script which would insert the current timestamp into a database on the main system every second - where your main php script can check it with a tolerance of 2 seconds. You could run this (call via browser and then click stop) to run this in a loop:

    PHP Code:
    //No time limit
    set_time_limit(0);

    //Ignore the browser closing connection
    ignore_user_abort(true);

    while(!
    file_exists('stopfile.txt'))
       {
       
    //Get timestamp and
       //Call to main machine
       
    file_get_contents('http://www.main-machine.com/record-time-in-db.php?time=' .time());

       
    //Anti CPU locking
       
    sleep(1);
       } 
    That would basically make each machine independantly report to your main server every second. In your main script you would use the IP of the incoming request and the time from the request and insert them into the db where you main script can then check the IP and time is within the last 2 seconds.
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  3. #3
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    I think it's better to use the cron job here.

    The problem with refreshing the info once every two minutes (when it is requested by a client) is that if another client comes along while the script is still running for the first client, the second client will also refresh the info, if you understand what I mean (this is a good example of a 'race condition').

    With the cron job you can simply serve the data that's in the database. If the cron job is still refreshing the data, clients will simply continue to see the 'old' info.

    We do something similar at work. We fetch bullion rates every 30 seconds from an external data supplier, and write those to the database. When the website or any other processes need them, they simply get the most current rates from the db. Pretty much the same thing as you're attempting here.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •