Connecting Tech Pros Worldwide Help | Site Map

Server Reboot

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 05:05 PM
Ben Allen
Guest
 
Posts: n/a
Default Server Reboot

Is there a way (through a variable) to show the date of the last server
reboot i.e. when PHP last started running? As I am trying to show the
server uptime, which of course only works from the last reboot,
therefore my %ages are very low.

Cheers,
Ben

  #2  
Old July 17th, 2005, 08:55 PM
Peter van Schie
Guest
 
Posts: n/a
Default Re: Server Reboot

Ben Allen wrote:[color=blue]
> Is there a way (through a variable) to show the date of the last server
> reboot i.e. when PHP last started running? As I am trying to show the
> server uptime, which of course only works from the last reboot,
> therefore my %ages are very low.[/color]


You could use /proc/uptime to retrieve it.
For a php example, check this out:

http://www.xenocafe.com/tutorials/ph...time/index.php

--
http://www.phpforums.nl
  #3  
Old July 17th, 2005, 08:55 PM
Peter van Schie
Guest
 
Posts: n/a
Default Re: Server Reboot

Peter van Schie wrote:
[color=blue]
> You could use /proc/uptime to retrieve it.
> For a php example, check this out:
>
> http://www.xenocafe.com/tutorials/ph...time/index.php
>[/color]

I just noticed that that tutorial has a lot of unnecessary mumbo jumbo
in it.
Here's a simpler code snippet of how to use /proc/uptime:

<?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';
?>

--
http://www.phpforums.nl
  #4  
Old July 17th, 2005, 08:55 PM
Ben Allen
Guest
 
Posts: n/a
Default Re: Server Reboot

Peter van Schie wrote:[color=blue]
> Peter van Schie wrote:
>[color=green]
>> You could use /proc/uptime to retrieve it.
>> For a php example, check this out:
>>
>> http://www.xenocafe.com/tutorials/ph...time/index.php
>>[/color]
>
> I just noticed that that tutorial has a lot of unnecessary mumbo jumbo
> in it.
> Here's a simpler code snippet of how to use /proc/uptime:
>
> <?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';
> ?>
>[/color]
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. Obviously this posses problems when trying
to calculate a percentage.

Thanks
Ben
  #5  
Old July 17th, 2005, 09:25 PM
Peter van Schie
Guest
 
Posts: n/a
Default Re: Server Reboot

Ben Allen wrote:
[color=blue]
> 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. Obviously this posses problems when trying
> to calculate a percentage.[/color]

Sorry I must have misinterpreted your question then. What percentage are
you trying to calculate?


--
http://www.phpforums.nl
  #6  
Old July 17th, 2005, 09:25 PM
Gordon Burditt
Guest
 
Posts: n/a
Default Re: Server Reboot

>> <?php[color=blue][color=green]
>> $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';
>> ?>
>>[/color]
>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.[/color]

If you want the uptime in just seconds, $uptime in the above
script gives you that.
[color=blue]
>Obviously this posses problems when trying
>to calculate a percentage.[/color]

What percentage? What problems? Since you haven't said what you
want, nothing is obvious.

Gordon L. Burditt
  #7  
Old July 18th, 2005, 08:05 AM
Ben Allen
Guest
 
Posts: n/a
Default Re: Server Reboot

Gordon Burditt wrote:[color=blue][color=green][color=darkred]
>>><?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';
>>>?>
>>>[/color]
>>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.[/color]
>
>
> If you want the uptime in just seconds, $uptime in the above
> script gives you that.
>
>[color=green]
>>Obviously this posses problems when trying
>>to calculate a percentage.[/color]
>
>
> What percentage? What problems? Since you haven't said what you
> want, nothing is obvious.
>
> Gordon L. Burditt[/color]

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="#F4F4F4" style="border: 1 solid
#000000"><div align="center"><span class="style1"><b>U</b>ptime:
<strong>'.$uptime.'</strong></span><br>
</div></td>
</tr>
<tr>
<td width="150" bgcolor="#F4F4F4" style="border: 1 solid
#000000"><div align="center">
<p><span
class="style1"><strong>'.$percentage.'%</strong></span><strong><br>
</strong>(since last reboot)</p>
</div></td>
</tr>
<tr>
<td width="150" bgcolor="#F4F4F4" 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;
?>
  #8  
Old July 18th, 2005, 10:15 AM
Peter van Schie
Guest
 
Posts: n/a
Default Re: Server Reboot

Ben Allen wrote:
[color=blue]
> 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.[/color]

I don't think that's possible, because to calculate that you'd need to
know for how long the server has been down before the last reboot.
I can't think of any file/device to retrieve that on a Linux box, I
might be wrong though.


--
http://www.phpforums.nl
  #9  
Old July 18th, 2005, 11:15 AM
Jerry Stuckle
Guest
 
Posts: n/a
Default Re: Server Reboot

Ben,

Won't work.

All you can determine is how long the machine has been up since the last reboot.
You can't tell how long it was down.

I see two ways around it. Either append the current date and time to a file
every minute (depending on the accuracy you want) and parse the file to find the
missing times, or monitor the site from a remote system.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.