Creating Your Own Server Stat Script in PHP
Before you create your own server stat script in PHP, it is must to check your ports are responding. You should aware when one of your services are down, such as HTTP, MySQL, POP/SMTP, etc. Also, other information you should know the current server loads, and users which will be the chief indicator of a runaway script, or process.
So let’s start.
Make sure your php script ALWAYS must start with <?php this tells your server that it is in fact a php script
<?php
//You can replace the domain with an IP if you wish
$site = “mysite.com”; //this is the site you wish to check
// Let’s check our common ports 80, 21, and 110
$http = fsockopen($site, 80);
$ftp = fsockopen($site, 21);
$pop3 = fsockopen($site, 110);
if ($http) {
$status .= “<font face=\”Arial\”><font size=\”2\”><b>HTTP</b>: Working</font></font><br>”;
}
else {
$status .= “<font face=\”Arial\”><font size=\”2\”><b>HTTP</b>: Not Working</font></font><br>”;
}
if ($ftp) {
$status .= “<font face=\”Arial\”><font size=\”2\”><b>FTP</b>: Working</font></font><br>”;
}
else {
$status .= “<font face=\”Arial\”><font size=\”2\”><b>FTP</b>: Not Working</font></font><br>”;
}
if ($pop3) {
$status .= “<font face=\”Arial\”><font size=\”2\”><b>POP3/SMTP</b>: Working</font></font><br>”;
}
else {
$status .= “<font face=\”Arial\”><font size=\”2\”><b>POP3/SMTP</b>: Not Working</font></font><br>”;
}
echo(”$status”);
echo(”<hr>”);
// Users and load information
$reguptime = trim(exec(”uptime”));
if ($reguptime) {
if (preg_match(”/, *(\d) (users?), .*: (.*), (.*), (.*)/”, $reguptime, $uptime)) {
$users[0] = $uptime[1];
$users[1] = $uptime[2];
$loadnow = $uptime[3];
$load15 = $uptime[4];
$load30 = $uptime[5];
}
} else {
$users[0] = “Unavailable”;
$users[1] = “–”;
$loadnow = “Unavailable”;
$load15 = “–”;
$load30 = “–”;
}
echo(”<b>Current Users:</b> $users[0]<br>
<b>Current Load:</b> $loadnow<br><b>Load 15 mins ago:</b> $load15<br><b>Load 15 mins ago:</b> $load30<br><hr>”);
// Operating system
$fp = @fopen(”/proc/version”, “r”);
if ($fp) {
$temp = fgets($fp);
fclose($fp);
if (preg_match(”/version (.*?) /”, $temp, $osarray)) {
$kernel = $osarray[1];
preg_match(”/[0-9]{5,} (\((.* *)\)\))/”, $temp, $osarray);
$flavour = $osarray[2];
$operatingsystem = $flavour.” (”.PHP_OS.” “.$kernel.”)”;
if (preg_match(”/SMP/”, $buf)) {
$operatingsystem .= ” (SMP)”;
}
} else {
$result = “(N/A)”;
}
} else {
$result = “(N/A)”;
}
echo(”<b>Operating System:</b><br>$operatingsystem”);
?>
The above code when uploaded to your server, it will display the status of your services, your user and load averages.
Now you can work with this, and make it look however you want, add new ports, etc.
Remember, this is just a very basic stat script.
Key Issues
One of the key issues you should aware that your host will need to allow the exec() funtion, in order for this to work properly.


August 11th, 2010 at 12:42 pm
The company was founded after seeing a niche in the market for high quality, high availability hosting, and is one of the most respected hosting companies in the marketplace. We have always been focussed on two things specifically; providing high technical quality services and making sure our clients are completely happy with our services.
October 15th, 2010 at 7:09 am
Snow
Thank
Very good interesting article.
October 29th, 2010 at 5:29 am
Tool Tracking
Thank you for this list of plugins!
November 1st, 2010 at 4:16 am
celebrity gossip
Thank you for this list of plugins!
November 1st, 2010 at 11:08 pm
if you’re gonna copy code, be sure you make it right and clean up the code!
December 25th, 2010 at 3:37 pm
Custom Web Design
believe that its a very nice & informative post about [Creating Your Own Server Stat Script in PHP]