Server Statistics
(Page 1 of 4 )
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.
Next: Uptime in Percentage >>
More Programming Basics Articles
More By Andrew Walsh