WebHostingTalk

Creating Your Own Server Stat Script in PHP

by Joni Carlton

In this article, I will walk you through creating your very own basic server stat script.

The first step is opening your favorite editor, I prefer, Crimson Editor, or notepad, and starting your script.

The Information

Checking to make sure your ports are responding is very important. It let’s you know when one of your services are down, such as HTTP, MySQL, POP/SMTP, etc. In addition, other information that is important is your current server loads, and users. This can be a key indicator of a runaway script, or process.

So let’s start.

Remember that 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 = "yourdomain.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 .= "<b>HTTP</b>: Working<<br>";
}

else {
	$status .= "<b>HTTP</b>: Not Working<br>";
}

if ($ftp) {
	$status .= "<b>FTP</b>: Working<<br>";
}
else {
	$status .= "<b>FTP</b>: Not Working<br>";
}

if ($pop3) {
	$status .= "<b>POP3/SMTP</b>: Working<br>";
}
else {
	$status .= "<b>POP3/SMTP</b>: Not Working<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");
?>

What the above script displays

The above code when uploaded to your server will display the status of your services, your user and load averages, and your operating system.

You can of course work with this, and make it look however you want, add new ports, etc. Again, this is just a very basic stat script, and has many possibilities.

Key Issues

One of the key issues you need to be aware of, is that your host will need to allow the exec() funtion, in order for this to work properly. Without this function enabled, you will not be able to see user and load averages, nor the Operating System.


Posted Tuesday, May 29th, 2007. Filed under Features. Trackbacks/Pings Trackback URL


View News by Category