• Embed Doc
  • Readcast
  • Collections
  • CommentGo Back
Download
 
Server Statistics
If you’ve ever wanted to generate your own server statistics such as those you get with most sharedhosting 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 probablyknow 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 StatisticsWe will start with showing the average server load and server uptime in days. Most of this can be donewith the exec() function. Basically, we will be sending the server the uptime command which will thenreturn 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 loadand then the uptime in days. This may seem very simple. Well, to be honest this whole tutorial is quieteasy actually. In the next sections we will be applying the same principles as this code. In the nextsection we will be looking at how to show a percentage of how much of the year the server has beenonline. 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 sectionthey will be used to work out what percentage of the year so far the server has been online. All we dois 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 secondssince 1/1/(the current year) and then we aquire today’s date using the same function. To work out howlong in seconds the server has been online we take $start from $end. Now to work out the days wedivide 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 theserver 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 postedit. 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" b&lt;tr&gt;&lt;td width="50%" bgcolor="#3973AC" style="border: 1 solid #000000"&gt;&lt;font colDetails&lt;/font&gt;&lt;/td&gt;&lt;td width="50%" bgcolor="#3973AC" style="border: 1 solid #000000"&gt;&lt;font colStatistics&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;&l&lt;td width="50%" bgcolor="#7CA8D3" style="border: 1 solid #000000"&gt;&amp;nbsp;&l&lt;/tr&gt;&lt;tr&gt;&lt;td width="50%" bgcolor="#7CA8D3" style="border: 1 solid #000000"&gt;&lt;b&gt;Nam&lt;/td&gt;&lt;td width="50%" bgcolor="#7CA8D3" style="border: 1 solid #000000"&gt;&lt;b&gt;Upt(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;Por'.getenv('SERVER_PORT').'&lt;/td&gt; &lt;td width="50%" bgcolor="#7CA8D3" style="border: 1 solid #000000"&gt;&lt;b&gt;Upt&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;Sof&lt;/b&gt;'.getenv('SERVER_SOFTWARE').'&lt;/td&gt; &lt;td width="50%" bgcolor="#7CA8D3" style="border: 1 solid #000000"&gt;&lt;b&gt;Loa&lt;/tr&gt;&lt;tr&gt;&lt;td width="50%" bgcolor="#7CA8D3" style="border: 1 solid #000000"&gt;&lt;b&gt;Pro&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;Gat&lt;/tr&gt;&lt;tr&gt;&lt;td width="50%" bgcolor="#7CA8D3" style="border: 1 solid #000000"&gt;&amp;nbsp;&l&lt;/tr&gt;&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;';echo$page;/* Print the page to browserDone*/?> 
Server Statistics - Conclusion
This may not be the best of server statistics scripts around but it certainly shows you how to intergratea 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 averageresults then mail them to the site Administrator/Webmaster. I hope this fairly short tutorial has been of some help to you.
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...