Web Hosting Talk







View Full Version : Checking Your Server Status


adaml
04-11-2005, 07:39 AM
This a pretty simple script that will check the status of your server, or it can be used so that anyone can enter the IP or domain of there server and it will check all the common ports.

Here is an example:
http://status.eX13.net

The Code:

<?php $up = @fsockopen("$ip", 80, $errno, $errstr, 30);
if($up)
{
echo 'Online';
}
else
{
echo 'Offline';
} ?>


ok well simple all this does is open a connection to "$ip" on port 80, and then if $up (if the connection was successful) it will echo Online if not it will echo Offline.

If you wanted to use the code for just one server you can replace $ip with either the domain name or the IP. If using the domain name you do not include http:// you would just type domainname.com

The $errno and $errstr are both standard error parameters. The last number "30" is how long the connection should stay open until it is classed as timed out.

If you wanted to make it so that anyone can check the status of there site all you would do is at the top of the page add:


$ip= $_POST['ip'];
if(is_null($ip))
{
die();
}
else
{
echo "Server Status Report For $ip";
}


What this does is retrieve the posted variable "$ip" and checks if the variable is empty if nothing was entered the script will terminate if something is entered the script will simply carry on what it is meant to be doing.

Now all that needs to be done is too add a form at the top of the page. The form needs a textbox field and a submit button.


<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<div align="center">
<p>
<input name="ip" type="text" value="">
<input type="submit" name="Submit" value="Submit">
</p>


This form will show a text box where anyone can enter any the domain or IP and then when the Submit button is click it will post the data into its self using the command:

action="<?php echp $_SERVER['PHP_SELF'] ?>"

Then the posted information will replace the variable $ip. You could use the code to show more than one port. By simply using the same "fsockopen" command but changing the Port number.

Heres a list of command ports:
HTTP - 80
FTP - 21
SSH - 22
MYSQL - 3306
CPANEL - 2082
WHM - 2086
POP3 - 110
SMTP - 25


Im sure this code will come in handy for someone.

adaml
04-12-2005, 06:23 AM
Did anyone find this useful?

VolkNet
04-22-2005, 03:07 AM
Thank you! This is a cool little trick. I always wondered how to do that! Thank you for writing this.

adaml
05-05-2005, 07:51 PM
No Probs :) Always happy to help!

error404
05-05-2005, 07:57 PM
With the given code, you're using a timeout of 30 seconds. In the case of a firewalled server or downed box, this function call will stall the drawing of your page for 30 seconds before failing. I'd recommend cutting the timeout down to one or two seconds so you don't inadvertently DOS yourself ;).

I'd be good practice and make it easier to use by dropping it into a function instead:

function checkService($ip, $port = 80) {
return @fsockopen($ip, $port, $errno, $errstr, 2);
}


Drop this at the top of your document, then where you want to display the status, do:

<?= (checkService("localhost") ? "Online" : "Offline") ?>


Cheers!

BurstChris
05-12-2005, 12:18 AM
you can also install sysmon and like tools instead of reinventing the wheel :)

they are more rich in features and better quality, worth checking out, as it can do more than just tcp connect()'s :)

skyDesigner
06-14-2005, 03:03 PM
Wow...great, i will install it right now...at least..i will try...
It's good that still there is some people who wants to help others 4 free :)
Best regards

adaml
06-22-2005, 10:47 AM
TCP Connects work :) And its only for simple use! For the people who need a quick bit of code.

Im glad some people like it :)

ozone_mark
07-07-2005, 02:52 PM
BurstChris,

Can you give the site where i can download the Sysmon script ?

Thanks.

jamesyeeoc
07-08-2005, 08:53 PM
Home page for sysmon:
http://www.sysmon.org/

Bonza
07-09-2005, 10:35 AM
Excellent, I'm about to go install right now..!

linux-tech
07-10-2005, 03:02 AM
Sysmon is great for the first few months. After that, you actually want reports, and to be able to customize things.
Realistically, if you know php, then program your own app, this is always the best way. Then you can use your own app to do what you want it to do. If you don't, use one of the commercial applications such as alertra or whatnot that actually keep storage of data properly.

I used sysmon for about 6 months until it became too much of a hassle. People want shiny images and to be able to see what's going on, and verify that the server's actually up ;)

jamesyeeoc
07-10-2005, 04:59 AM
I used sysmon for about 6 months until it became too much of a hassle. People want shiny images and to be able to see what's going on, and verify that the server's actually upYes, I agree, but to each their own :) It's always a learning curve...

McJeff215
07-11-2005, 01:23 AM
Don't forget about OpenNMS, it will give you some of the more advanced NMS related features as opposed to a simple "up/down" message.

Also:

SiteScope
ipMonitor
Big Brother

The latter three will give you more of an up-or-down synthetic transaction apporoach, while the first will give you more of a classic NMS approach.

I've spent time at places where we've built NMS systems and at places where we've bought them. I feel a rant coming on, so if anyone is really interested, let me know. ;-)

TrophyHost
07-16-2005, 12:39 PM
Instead of saying "online" & "Offline" how do you get it to show images?

adaml
07-17-2005, 07:39 AM
Which method are you using?

You should just be able to replace the offline and online parts with:

<img src="yourimage.com/source.jpg">

HE-Heeren
08-22-2005, 02:12 AM
this thread was definately helpful... saves me the time trying to write my own script... I had no idea about those other apps either

anjanesh
08-28-2005, 01:56 AM
Instead of saying "online" & "Offline" how do you get it to show images?

if($up)
{
echo '<img src="online.gif"/>';
}
else
{
echo '<img src="offline.gif"/>';
}


[EDIT] : I didnt realize there was a page 2 in this thread - I just read the last post in page1.

teendragon
12-24-2005, 08:48 PM
wow i was trying to find out how to do this thanks!

Real-Hosts
02-11-2006, 01:50 PM
I think opening a socket is OK,
but how about those times when a server is jammed, and it will open a socket, and you can ping the port - but it's still not serving pages, FTP never logs in, and SSH just hangs there smiling at you...

What you need is to get some Nagios-like scripts, which just check if it can actually connect, and is custom made per service, and waits for things like HTTP OK messages and can tell what FTP login is... That is class.

linux-tech
02-11-2006, 01:55 PM
Actually, you can check the port for a response in php, and you can check it for the correct response.
Sysmon doesn't do that, you're correct. It's incredibly bad in that respect. All it does in most cases is check the port, nothing else huge.

Jamez226
03-19-2006, 01:12 PM
Also if your on a linux server you could show the reboot times etc. these are in a file in /proc/ but i cannot remember the exact files.

Jamez

adaml
03-21-2006, 06:05 PM
Its only a simple PHP Script to check the status of a remote server, nothing fancy! There are hundreds of applications out there that are good for monitoring your server, including the temperature, cpu usage, ports, bandwidth, hard drive status and more.. I dont think all of this would be possible with PHP though, well it would just slower and more complex.

flamingo
05-13-2006, 06:29 PM
thnx mate for sharing a nice code here..:)

openone
05-14-2006, 12:22 PM
Thanks adam , i was looking for something like this. Thanks again :D

ProXHosting
06-25-2006, 05:11 AM
Heres another server sats script in more details like it tells you mysql ect is up, i found this on another forms and i found it useful so ill share it here:

<?php
/*
* +------------------------------------------------------------------------------+
* CPANEL STATUS SCRIPT
* +------------------------------------------------------------------------------+
* Copyright Notice(s)
* +------------------------------------------------------------------------------+
* Disclaimer Notice(s)
* ex: This code is freely given to you and given "AS IS", SO if it damages
* your computer, formats your HDs, or burns your house I am not the one to
* blame.
* Moreover, don't forget to include my copyright notices and name.
* +------------------------------------------------------------------------------+
* Author(s): Crooty.co.uk (Adam C)
* +------------------------------------------------------------------------------+
*/

$data .= "
<style>
td,body
{
font-family: Arial, Helvetica, sans-serif;
font-size: 8pt;
color: #444444;
}
</style>
<br>
<center>
<div style=\"border-bottom:1px #999999 solid;width:480;\"><b>
<font size='1' color='#3896CC'>Service Status</font></b>
</div>
</center>
<br>";

//configure script
$timeout = "1";

//set service checks
$port[1] = "80"; $service[1] = "Apache"; $ip[1] ="";
$port[2] = "21"; $service[2] = "FTP"; $ip[2] ="";
$port[3] = "3306"; $service[3] = "MYSQL"; $ip[3] ="";
$port[4] = "25"; $service[4] = "Email(POP3)"; $ip[4] ="";
$port[5] = "143"; $service[5] = "Email(IMAP)"; $ip[5] ="";
$port[6] = "2095"; $service[6] = "Webmail"; $ip[6] ="";
$port[7] = "2082"; $service[7] = "Cpanel"; $ip[7] ="";
$port[8] = "80"; $service[8] = "Internet Connection"; $ip[8] ="google.com";
$port[9] = "2086"; $service[9] = "WHM"; $ip[9] ="";

//
// NO NEED TO EDIT BEYOND HERE
// UNLESS YOU WISH TO CHANGE STYLE OF RESULTS
//

//count arrays
$ports = count($port);
$ports = $ports + 1;
$count = 1;

//beggin table for status
$data .= "<table width='480' border='1' cellspacing='0' cellpadding='3' style='border-collapse:collapse' bordercolor='#333333' align='center'>";

while($count < $ports){

if($ip[$count]==""){
$ip[$count] = "localhost";
}

$fp = @fsockopen("$ip[$count]", $port[$count], $errno, $errstr, $timeout);
if (!$fp) {
$data .= "<tr><td>$service[$count]</td><td bgcolor='#FFC6C6'>Offline </td></tr>";
} else {
$data .= "<tr><td>$service[$count]</td><td bgcolor='#D9FFB3'>Online</td></tr>";
fclose($fp);
}
$count++;
fclose($fp);

}

//close table
$data .= "</table>";

echo $data;
?>
installation: copy to web server THATS IT!

if you wish to display server uptime and server loads on the script add this to the code (REMEMBER: it must be placed inside the <? and ?> for it to work

//
// SERVER INFORMATION
//

$data1 .= "
<br>
<center>
<div style=\"border-bottom:1px #999999 solid;width:480;\"><b>
<font size='1' color='#3896CC'>Server Information</font></b>
</div>
</center><BR>";

$data1 .= "<table width='480' border='1' cellspacing='0' cellpadding='3' style='border-collapse:collapse'

bordercolor='#333333' align='center'>";

//GET SERVER LOADS
$loadresult = @exec('uptime');
preg_match("/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/",$loadresult,$avgs);


//GET SERVER UPTIME
$uptime = explode(' up ', $loadresult);
$uptime = explode(',', $uptime[1]);
$uptime = $uptime[0].', '.$uptime[1];

$data1 .= "<tr><td>Server Load Averages </td><td>$avgs[1], $avgs[2], $avgs[3]</td>\n";
$data1 .= "<tr><td>Server Uptime </td><td>$uptime </td></tr>";
$data1 .= "</table>";
echo $data1;

wiredhosting
06-30-2006, 06:25 PM
What can happen if i remove the " fclose($fp) "


Thanks!

jdk
06-30-2006, 06:59 PM
$count++;
fclose($fp);

}

This fclose($fp); is not needed. Nothing happens when you remove it except gets rid of of the error.

wiredhosting
06-30-2006, 07:02 PM
Thanksss :D

I looks Great this Server Status script! I recommend it

GoodFellas
01-07-2007, 01:11 AM
ProXHosting,

Thanks for sharing that, i will be implementing that in my site

linux-tech
01-07-2007, 08:00 AM
ProXHosting,

Thanks for sharing that, i will be implementing that in my site

Please note that the above script will NOT tell you if a service is "up". It will merely tell you if something is listening on that port.


For true determination, you need to separate the checks into proper categories, and investigate the responses made by the checks to determine that the service is really up or down.

Most times (note:most) , a check to said port will do fine. however, there are times when said check will NOT do fine. For example:

Exim has a connection limit. While the port will still respond, if the connection limit is reached, the error code will change

Pure-FTP will lock out users at a certain disk usage percentage. Again, the port will still respond, but the status code will change

Apache may be running,but does that mean that it's serving up web pages? Not always. Again, you can have something RUNNING (ie: apache) on port 80, but that doesn't mean that it's going to serve up the web page.


All of these can cause pretty serious issues if not investigated properly, and can (of course) lead to some pretty upset customers.

comdexxsoftwarell
03-06-2007, 03:51 PM
Simple, Easy way to check your server status. Bravo!

Mr-Max
04-27-2007, 10:25 AM
Thank You >>>>>>

Mr-Max
04-27-2007, 10:32 AM
Thanks Alot >>>>>

start
05-24-2007, 07:42 PM
How can you ping a website/server every 15 minutes?

kevinharris
06-08-2007, 01:26 PM
Nice, thanks.

RedHart
06-19-2007, 09:43 AM
heh, very nice :]

YBHosting
06-28-2007, 05:53 PM
Pretty simple script, but very useful. Thanks

vastitech
06-29-2007, 03:30 AM
Very useful and simple script :)

E-Learning
06-29-2007, 07:44 AM
i get error in this file ?? what the problem for it ?


Warning: fclose(): 2 is not a valid stream resource in /home/s/public_html/server.php on line 76

Warning: fclose(): 3 is not a valid stream resource in /home/s/public_html/server.php on line 76

Warning: fclose(): 4 is not a valid stream resource in /home/s/public_html/server.php on line 76

Warning: fclose(): 5 is not a valid stream resource in /home/s/public_html/server.php on line 76

Warning: fclose(): 6 is not a valid stream resource in /home/s/public_html/server.php on line 76

Warning: fclose(): 7 is not a valid stream resource in /home/s/public_html/server.php on line 76

Warning: fclose(): 8 is not a valid stream resource in /home/s/public_html/server.php on line 76

Warning: fclose(): 9 is not a valid stream resource in /home/s/public_html/server.php on line 76

Warning: fclose(): 10 is not a valid stream resource in /home/s/public_html/server.php on line 76.

the script is
<?php
/*
* +------------------------------------------------------------------------------+
* CPANEL STATUS SCRIPT
* +------------------------------------------------------------------------------+
* Copyright Notice(s)
* +------------------------------------------------------------------------------+
* Disclaimer Notice(s)
* ex: This code is freely given to you and given "AS IS", SO if it damages
* your computer, formats your HDs, or burns your house I am not the one to
* blame.
* Moreover, don't forget to include my copyright notices and name.
* +------------------------------------------------------------------------------+
* Author(s): Crooty.co.uk (Adam C)
* +------------------------------------------------------------------------------+
*/

$data .= "
<style>
td,body
{
font-family: Arial, Helvetica, sans-serif;
font-size: 8pt;
color: #444444;
}
</style>
<br>
<center>
<div style=\"border-bottom:1px #999999 solid;width:480;\"><b>
<font size='1' color='#3896CC'>Service Status</font></b>
</div>
</center>
<br>";

//configure script
$timeout = "1";

//set service checks
$port[1] = "80"; $service[1] = "Apache"; $ip[1] ="";
$port[2] = "21"; $service[2] = "FTP"; $ip[2] ="";
$port[3] = "3306"; $service[3] = "MYSQL"; $ip[3] ="";
$port[4] = "25"; $service[4] = "Email(POP3)"; $ip[4] ="";
$port[5] = "143"; $service[5] = "Email(IMAP)"; $ip[5] ="";
$port[6] = "2095"; $service[6] = "Webmail"; $ip[6] ="";
$port[7] = "2082"; $service[7] = "Cpanel"; $ip[7] ="";
$port[8] = "80"; $service[8] = "Internet Connection"; $ip[8] ="google.com";
$port[9] = "2086"; $service[9] = "WHM"; $ip[9] ="";

//
// NO NEED TO EDIT BEYOND HERE
// UNLESS YOU WISH TO CHANGE STYLE OF RESULTS
//

//count arrays
$ports = count($port);
$ports = $ports + 1;
$count = 1;

//beggin table for status
$data .= "<table width='480' border='1' cellspacing='0' cellpadding='3' style='border-collapse:collapse' bordercolor='#333333' align='center'>";

while($count < $ports){

if($ip[$count]==""){
$ip[$count] = "localhost";
}

$fp = @fsockopen("$ip[$count]", $port[$count], $errno, $errstr, $timeout);
if (!$fp) {
$data .= "<tr><td>$service[$count]</td><td bgcolor='#FFC6C6'>Offline </td></tr>";
} else {
$data .= "<tr><td>$service[$count]</td><td bgcolor='#D9FFB3'>Online</td></tr>";
fclose($fp);
}
$count++;
fclose($fp);

}
$count++;
fclose($fp);

}
//close table
$data .= "</table>";

echo $data;
//
//
// SERVER INFORMATION
//

$data1 .= "
<br>
<center>
<div style=\"border-bottom:1px #999999 solid;width:480;\"><b>
<font size='1' color='#3896CC'>Server Information</font></b>
</div>
</center><BR>";

$data1 .= "<table width='480' border='1' cellspacing='0' cellpadding='3' style='border-collapse:collapse'

bordercolor='#333333' align='center'>";

//GET SERVER LOADS
$loadresult = @exec('uptime');
preg_match("/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/",$loadresult,$avgs);


//GET SERVER UPTIME
$uptime = explode(' up ', $loadresult);
$uptime = explode(',', $uptime[1]);
$uptime = $uptime[0].', '.$uptime[1];

$data1 .= "<tr><td>Server Load Averages </td><td>$avgs[1], $avgs[2], $avgs[3]</td>\n";
$data1 .= "<tr><td>Server Uptime </td><td>$uptime </td></tr>";
$data1 .= "</table>";
echo $data1;
?> thanks ...

wapster
11-10-2007, 02:24 PM
thank you ;)

aldo
12-28-2007, 05:28 AM
If you wanted to see the status of port 80 you could easily do:

<?php
echo "HTTP is obviously up!";
?>
lol. But the other ports are more useful :)

aldo
12-28-2007, 06:34 AM
Cant edit my above post :-\ Also I cant post a new topic...
Anyways, here is a somewhat advanced I guess you could say server status script:

<?php $ftp = @fsockopen("localhost", 21, $errno, $errstr, 30);
if($ftp) { $ftp=true; } else { $ftp=false; } ?>
<?php $http = @fsockopen("localhost", 80, $errno, $errstr, 30);
if($http) { $http=true; } else { $http=false; } ?>
<?php $smtp = @fsockopen("localhost", 25, $errno, $errstr, 30);
if($smtp) { $smtp=true; } else { $smtp=false; } ?>
<?php $pop3 = @fsockopen("localhost", 110, $errno, $errstr, 30);
if($pop3) { $pop3=true; } else { $pop3=false; } ?>
<?php $mysql = @fsockopen("localhost", 3306, $errno, $errstr, 30);
if($mysql) { $mysql=true; } else { $mysql=false; } ?>
<?php $webmail = @fsockopen("localhost", 2095, $errno, $errstr, 30);
if($webmail) { $webmail=true; } else { $webmail=false; } ?>
<div align="center">
<table width="250px">
<tr>
<td>HTTP:</td>
<?php if($http) { ?>
<td bgcolor='#D9FFB3'>Online (Obviously!)</td>
<?php } else { ?>
<td bgcolor='#FFC6C6'>Offline</td>
<?php } ?>
</tr>
<?php
$load = exec('uptime');
$load = split('load average:', $load);
$load = split(', ', $load[1]);
$load = $load[0];
?>
<tr>
<td>Server Load:</td>
<?php
if($load<=4) { ?>
<td bgcolor='#D9FFB3'><?php echo $load; ?></td>
<?php } else { if($load<=7) { ?>
<td bgcolor='#fff9c6'><?php echo $load; ?></td>
<?php } else { ?>
<td bgcolor='#FFC6C6'><?php echo $load; ?></td>
<?php } } ?>
</tr>
<tr>
<td>FTP:</td>
<?php if($ftp) { ?>
<td bgcolor='#D9FFB3'>Online</td>
<?php } else { ?>
<td bgcolor='#FFC6C6'>Offline</td>
<?php } ?>
</tr>
<tr>
<td>Webmail:</td>
<?php if($webmail) { ?>
<td bgcolor='#D9FFB3'>Online</td>
<?php } else { ?>
<td bgcolor='#FFC6C6'>Offline</td>
<?php } ?>
</tr>
<tr>
<td>SMTP:</td>
<?php if($smtp) { ?>
<td bgcolor='#D9FFB3'>Online</td>
<?php } else { ?>
<td bgcolor='#FFC6C6'>Offline</td>
<?php } ?>
</tr>
<tr>
<td>POP3:</td>
<?php if($pop3) { ?>
<td bgcolor='#D9FFB3'>Online</td>
<?php } else { ?>
<td bgcolor='#FFC6C6'>Offline</td>
<?php } ?>
</tr>
<tr>
<td>MySQL:</td>
<?php if($mysql) { ?>
<td bgcolor='#D9FFB3'>Online</td>
<?php } else { ?>
<td bgcolor='#FFC6C6'>Offline</td>
<?php } ?>
</tr>
</table>
</div>A bit messy sure, but cool none the less :) Enjoy!

I5KI Bones
04-20-2008, 07:37 PM
This is really helpful, thanks.