Gordon Burditt wrote:
<?php
$uptime = shell_exec("cut -d. -f1 /proc/uptime");
$days = floor($uptime / 60 / 60 / 24);
$hours = $uptime / 60 / 60 % 24;
$mins = $uptime / 60 % 60;
$secs = $uptime % 60;
echo 'Uptime is: ' . $days . ' days ' . $hours . ' hours ' . $mins
minutes . ' and ' . $secs . ' seconds';
?>
Thanks, that code (with a few tweaks involving the minutes text-oops)
gives me:
Uptime is: 5 days 21 hours 34 minutes and 44 seconds
which is the same as what my other script was giving me and is the
uptime since the last reboot.
If you want the uptime in just seconds, $uptime in the above
script gives you that.
Obviously this posses problems when trying
to calculate a percentage.
What percentage? What problems? Since you haven't said what you
want, nothing is obvious.
Gordon L. Burditt
Sorry, basically, I wanted to calculate uptime as a percentage
(preferably over the year), however, I then found the $uptime variable
only counted since the last reboot, therefore I wanted to show when the
server was last rebooted and also divide uptime by the server reboot
time. This is the code i was using, please note, i have added variables
into the 'start date' so I can set the reboot date manually. I wondered
if there was a way to do this manually.
<?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];
$sday = 12;
$smonth = 07;
$syear = 2005;
$start=mktime(0 , 0, 0, $smonth, $sday, $syear, 0);
$end=mktime(0, 0, 0, date("m"), date("d"), date("Y"), 0);
$diff=$end-$start;
$days=$diff/86400;
$percentage= round(($uptime/$days) * 100, 2);
#$load=$avgs[1].",".$avgs[2].",".$avgs[3]."";
$page='<style type="text/css">
<!--
..style1 {
color: #000000;
font-weight: bold;
}
..style2 {font-size: 12px}
-->
</style>
<body>
<table border="0" align="center" cellpadding="0" cellspacing="0"
style="border: 1 solid #000000">
<tr>
<td width="150" bgcolor="#F4F4F 4" style="border: 1 solid
#000000"><div align="center"> <span class="style1"> <b>U</b>ptime:
<strong>'.$upti me.'</strong></span><br>
</div></td>
</tr>
<tr>
<td width="150" bgcolor="#F4F4F 4" style="border: 1 solid
#000000"><div align="center">
<p><span
class="style1"> <strong>'.$perc entage.'%</strong></span><strong><b r>
</strong>(since last reboot)</p>
</div></td>
</tr>
<tr>
<td width="150" bgcolor="#F4F4F 4" style="border: 1 solid
#000000"><div align="center"> <span class="style2"> Server Last Rebooted
on 12/07/2005 </span></div></td>
</tr>
</table>
</body>';
echo $page;
?>