FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Server uptime tracker





JustaMin
Hi,

I have a script (php) which tracks the uptime of the frihost 2 server. It looks like this:

Code:

<?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]);
  $hrsmins = explode(':', $uptime[1]);
  $uptime = $uptime[0].', '.$hrsmins[0].' hours '.$hrsmins[1].' mins';
$start=mktime(0, 0, 0, date("m"), (date("j")-7), date("Y"), 0);
$end=mktime(0, 0, 0, date("m"), date("j"), date("y"), 0);

$diff=$end-$start;
$days=$diff/86400;
$percentage= round(($uptime/$days) * 100,2).'% uptime in last 7 days';

echo $uptime;
echo $percentage;
?>


That shows the total uptime and the percentage uptime in last 7 days. my problem is, if the server is rebooted that's going to set the uptime to 0, which means my % uptime for the last 7 days will be wrong. Can anyone think of a solution to this. At the moment i'm thinking of storing the uptime in a database table as a means of tracking it, but is there an easier way?

Thanks Smile
thnn
Do you mind if I use your script? It's is something I would like to put on my website to show my admins the site uptime and I have not being able to find one. To your problem I cannot think of another way other than using a database. You could write it to a file as a variable and use it like that but other than that I do not know.
JustaMin
elp yoursel mate, but do bear in mind this problem. You'll need to mod the file to work with the db to get it working. My thoughts at the moment are running the script as a cron job that adds the daily uptime to a database and then just pulling the last x days from it with the actual display script to display the data.

But yeah, help yourself Smile
n0obie4life
I've made myself two. It appears one shows it wrongly and one shows it correctly Wink.

Code:
$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];
echo 'Uptime - Since Last Reboot: '.$uptime;


I think that's the whole script.

Demo - http://bondings-world.frihost.net/test.php

Note that
Code:
Uptime - Since Start:
is wrong Wink
JustaMin
OK here's a much better version, the last one was in the middle of being played with when I posted it.

Code:

function format_uptime($seconds) {
  $secs = intval($seconds % 60);
  $mins = intval($seconds / 60 % 60);
  $hours = intval($seconds / 3600 % 24);
  $days = intval($seconds / 86400);
 
  if ($days > 0) {
    $uptimeString .= $days;
    $uptimeString .= (($days == 1) ? " day" : " days");
  }
  if ($hours > 0) {
    $uptimeString .= (($days > 0) ? ", " : "") . $hours;
    $uptimeString .= (($hours == 1) ? " hour" : " hours");
  }
  if ($mins > 0) {
    $uptimeString .= (($days > 0 || $hours > 0) ? ", " : "") . $mins;
    $uptimeString .= (($mins == 1) ? " minute" : " minutes");
  }
  if ($secs > 0) {
    $uptimeString .= (($days > 0 || $hours > 0 || $mins > 0) ? ", " : "") . $secs;
    $uptimeString .= (($secs == 1) ? " second" : " seconds");
  }
  return $uptimeString;
}


$uptime = exec("cat /proc/uptime");
$uptime = split(" ",$uptime);
$uptimeSecs = $uptime[0];

$dailyUptimeSecs = $uptimeSecs-($uptimeSecs - 86400);
$percentage= round($dailyUptimeSecs/86400 * 100,2).'%';

$staticUptime = "Server Uptime: ".format_uptime($uptimeSecs);

echo $staticUptime;



Outputs the server's uptime since the last outage, as well as gives vars containing daily (or whatever you set it to) uptime percentage. I'm working on the more accurate cumulative uptime script now, i'll post it for anyone who wants it when it's done.
JustaMin
OK this is largely untested but...

This script is run every 24 hours via a cron job and saves the daily uptime seconds in a database table called 'uptime' with the following structure:

datetime (type: datetime) (primary key)
up (type: int) (length: 5)

obviously you have to change the reference to connect.php to your own database connection script. Remember to use full server path as it will be run as a cron job.

Code:
<?
//Includes
include_once('/home/justamin/domains/justaminute.me.uk/public_html/connect.php');

$uptime = exec("cat /proc/uptime");
$uptime = split(" ",$uptime);
$uptimeSecs = $uptime[0];

$dailyUptimeSecs = $uptimeSecs-($uptimeSecs - 86400);

$query     = "INSERT INTO uptime (datetime,up)
         VALUES (NOW(),'$dailyUptimeSecs')";
$result = mysql_query($query);
?>


This file works out the total uptime since the script was implemented:

Code:
<?
include_once('connect.php');

function format_uptime($seconds) {
  $secs = intval($seconds % 60);
  $mins = intval($seconds / 60 % 60);
  $hours = intval($seconds / 3600 % 24);
  $days = intval($seconds / 86400);
 
  if ($days > 0) {
    $uptimeString .= $days;
    $uptimeString .= (($days == 1) ? " day" : " days");
  }
  if ($hours > 0) {
    $uptimeString .= (($days > 0) ? ", " : "") . $hours;
    $uptimeString .= (($hours == 1) ? " hour" : " hours");
  }
  if ($mins > 0) {
    $uptimeString .= (($days > 0 || $hours > 0) ? ", " : "") . $mins;
    $uptimeString .= (($mins == 1) ? " minute" : " minutes");
  }
  if ($secs > 0) {
    $uptimeString .= (($days > 0 || $hours > 0 || $mins > 0) ? ", " : "") . $secs;
    $uptimeString .= (($secs == 1) ? " second" : " seconds");
  }
  return $uptimeString;
}

$query = "SELECT datetime
        FROM uptime
        ORDER BY datetime
        LIMIT 1
        ";
$result = mysql_query($query);
$row = mysql_fetch_array($result,MYSQL_ASSOC);
extract($row);
$start = $datetime;

$query = "SELECT up
        FROM uptime
        ";
$result = mysql_query($query);
$numdays =mysql_num_rows($result);

while ( $row = mysql_fetch_array($result))
{
   extract($row);
   $totalUptime = $totalUptime + $up;
}

$percentage = round($totalUptime/($numdays*86400) * 100,2).'%';

$string = "Total uptime since $start: ".format_uptime($totalUptime)." ($percentage)";
echo $string;
?>


Should work fine but lt me know if you see any problems Smile
izcool
I use the Uptimes Project at http://uptimes.hostingwired.com/ since I don't like the code that I wrote for uptimes tracking. Wink

- Mike.
buruk
This is a full featured e-mail newsletter management system. Members can subscribe or unsubscribe.You can Compose A Newsletter in plan text OR HTML and also Preview HTML in a pop up Window.You can Edit your Header and Footer for the Subscribe and Unsubscribe pages to reflect your sites look. The script is easy to install
Related topics
751 Useful Windows XP Files
Uptime
Server uptime
Tips on making GOOD siggys
Files and databases (server 1)
Server status script
Live Web Server Status
server 2 down to 93% uptime for the week
DNS nameserver - zoneedit, other free service - unreliable ?
Server Uptime Sig
[AD] GamezEventosMu Season IV
Server 4 currently down (old topic, it it working again)
System overload - Server 1
Server 3 Uptime
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.