Sie sind auf Seite 1von 4

Server Statistics

If you’ve ever wanted to generate your own server statistics such as those you get with most shared
hosting environments, then this tutorial is for you.

If you have ever had shared hosting, which I am sure that most of you have, then you will probably
know how these hosts pack your account with loads of server statistics tools. This is all well and good
but have you ever wanted to make one for yourself? If you do then this tutorial is probably for you.

Basic Statistics

We will start with showing the average server load and server uptime in days. Most of this can be done
with the exec() function. Basically, we will be sending the server the uptime command which will then
return the information we need to form our basic server stats script.

<?php
$uptime = @exec('uptime');
preg_match("/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-
9\.]+)/",$uptime,$avgs);
$uptime = explode(' up ', $uptime);
$uptime = explode(',', $uptime[1]);
$uptime = $uptime[0].', '.$uptime[1];
$start=mktime(0, 0, 0, 1, 1, date("Y"), 0);
$end=mktime(0, 0, 0, date("m"), date("j"), date("y"), 0);
$diff=$end-$start;
$days=$diff/86400;
$percentage=($uptime/$days) * 100;
$load=$avgs[1].",".$avgs[2].",".$avgs[3]."";
echo 'Average Load: '.$load;
echo 'Uptime: '.$uptime;
?>

If you where to run that script on your website then you would get two statistics back, the server load
and then the uptime in days. This may seem very simple. Well, to be honest this whole tutorial is quiet
easy actually. In the next sections we will be applying the same principles as this code. In the next
section we will be looking at how to show a percentage of how much of the year the server has been
online. The basis is already there in this script.

Server Statistics - Uptime in Percentage


If you looked at the last code you saw two redundant and useless date() functions. Well in this section
they will be used to work out what percentage of the year so far the server has been online. All we do
is add a line to calculate the percentage uptime.

Here is the whole block of code to work out the percentage of the year so far.

<?php
$start=mktime(0, 0, 0, 1, 1, date("Y"), 0);
/* Make date 1/1/(current year) */
$end=mktime(0, 0, 0, date("m"), date("j"), date("y"), 0);
/* Make todays date */
$diff=$end-$start;
$days=$diff/86400;
$percentage=($uptime/$days) * 100;
/* Work out percentage */
echo $percentage;
/* Print out percentage */
?>

This could be used for anything, firstly we use $start and mktime to acquire the number of seconds
since 1/1/(the current year) and then we aquire today’s date using the same function. To work out how
long in seconds the server has been online we take $start from $end. Now to work out the days we
divide the result by 86400 and we will get a number of days which then we divide by $uptime
(obtained from the first code snippet) and multiply by 100 to get a percentage. Simple wasn’t it?

Next we will work on a more complex version, this will include listing of server variables like the
server name, IP, port, software and gateway.

Server Statistics - Final Script


As I promised here is a more advanced version which displays server name, IP, port, software,
gateway, and a few others. You may recognize this from the codewalkers code gallery where I posted
it. This has a nice little html template used for displaying the results so it does look alot nicer than
previous scripts.

<?php
$uptime = @exec('uptime');
/* Get uptime from uptime command */
preg_match("/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/",$uptime,$avgs);
/* Break up result */
$uptime = explode(' up ', $uptime);
$uptime = explode(',', $uptime[1]);
$uptime = $uptime[0].', '.$uptime[1];
$start=mktime(0, 0, 0, 1, 1, date("Y"), 0);
$end=mktime(0, 0, 0, date("m"), date("j"), date("y"), 0);
/* Make dates */
$diff=$end-$start;
$days=$diff/86400;
$percentage=($uptime/$days) * 100;
/* Work out percentages */
$load=$avgs[1].",".$avgs[2].",".$avgs[3]."";
/* Grab average uptime into string */
/* Format page */
$page='&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Server Statistics For '.getenv('SERVER_NAME').'&lt;/title&gt;
&lt;style type="text/css"&gt;
td{
border-style: solid;
border-width: 1px;
color: #000000;
}
table{
border-style: solid;
border-width: 1px;
color: #000000;
}
tr{
border-style: solid;
border-width: 1px;
color: #000000;
}
&lt;/style&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;table width="100%" cellspacing="0" cellpadding="0" style="border: 1 solid #000000" bo
&lt;tr&gt;
&lt;td width="50%" bgcolor="#3973AC" style="border: 1 solid #000000"&gt;&lt;font colo
Details&lt;/font&gt;&lt;/td&gt;
&lt;td width="50%" bgcolor="#3973AC" style="border: 1 solid #000000"&gt;&lt;font colo
Statistics&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="50%" bgcolor="#7CA8D3" style="border: 1 solid #000000"&gt;&amp;nbsp;&lt
&lt;td width="50%" bgcolor="#7CA8D3" style="border: 1 solid #000000"&gt;&amp;nbsp;&lt
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="50%" bgcolor="#7CA8D3" style="border: 1 solid #000000"&gt;&lt;b&gt;Name
&lt;/td&gt;
&lt;td width="50%" bgcolor="#7CA8D3" style="border: 1 solid #000000"&gt;&lt;b&gt;Upti
(days): '.$uptime.'&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="50%" bgcolor="#7CA8D3" style="border: 1 solid #000000"&gt;&lt;b&gt;Port
'.getenv('SERVER_PORT').'&lt;/td&gt;
&lt;td width="50%" bgcolor="#7CA8D3" style="border: 1 solid #000000"&gt;&lt;b&gt;Upti
&lt;/b&gt;
(%): '.$percentage.'%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="50%" bgcolor="#7CA8D3" style="border: 1 solid #000000"&gt;&lt;b&gt;Soft
&lt;/b&gt;
'.getenv('SERVER_SOFTWARE').'&lt;/td&gt;
&lt;td width="50%" bgcolor="#7CA8D3" style="border: 1 solid #000000"&gt;&lt;b&gt;Load
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="50%" bgcolor="#7CA8D3" style="border: 1 solid #000000"&gt;&lt;b&gt;Prot

&lt;td width="50%" bgcolor="#7CA8D3" rowspan="3" style="border: 1 solid #000000"&gt;&


&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="50%" bgcolor="#7CA8D3" style="border: 1 solid #000000"&gt;&lt;b&gt;Gate

&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="50%" bgcolor="#7CA8D3" style="border: 1 solid #000000"&gt;&amp;nbsp;&lt
&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;

&lt;/html&gt;';
echo $page;
/* Print the page to browser
Done
*/
?>

Server Statistics - Conclusion


This may not be the best of server statistics scripts around but it certainly shows you how to intergrate
a unix/linux command into a PHP script to return results. (This will not work on Windows.) The
possibilities of improving this script are endless, for example you could add a daily digest of average
results then mail them to the site Administrator/Webmaster. I hope this fairly short tutorial has been of
some help to you.
About the Author

Andrew Walsh lives in the UK and is engaged in his secondary education. He wishes to pursue a
higher education in computer studies. He started programming at the age of 13 years, currently he has
knowledge of PHP/MySQL, (X)HTML, CSS and JavaScript.

Das könnte Ihnen auch gefallen