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.



$ServerStatus = new CServerStatus();

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




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))
			echo '
			<div class="serverstyle">
			<img src=statusUP.gif" title="'.$servername.' is UP!" alt="'.$servername.' is UP!" >'.$servername.' is Up
			echo '
			<div class="serverstyle">
			<img src="statusDOWN.gif" title="'.$servername.' is DOWN!" alt="'.$servername.' is DOWN!">'.$servername.' is Down

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 ? :rolleyes:

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 :stuck_out_tongue:

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.

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:

//No time limit

//Ignore the browser closing connection

   //Get timestamp and
   //Call to main machine
   file_get_contents('' .time());

   //Anti CPU locking

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 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. :slight_smile: