Page 1 of 1

System uptime on main page.

Posted: Wed Jul 18, 2007 3:17 am
by ryscar
I know this can be accomplished via the command line but, for those who are not granted access to this, it would be helpful. It would let someone know if the power had been shut off to the box.

Posted: Wed Jul 18, 2007 5:52 am
by apcyberax
a better idea is to use a UPS like me then it won't matter if the power fails. unless its down for hours

Posted: Thu Jul 19, 2007 8:33 am
by AmmarossDanan
Place this in the area of your zm_html_view_console.php script you want to display the system uptime. (Mine is right under the date/time in upper-left).
Starting with line 224:
! <td class="smallhead" style="line-height:11px" align="left"><?= preg_match( '/%/', DATE_FMT_CONSOLE_LONG )?strftime( DATE_FMT_CONSOLE_LONG ):date( DATE_FMT_CONSOLE_LONG ) ?><br/>
+ <? /* Display system uptime */
+ $uptime = `uptime`;
+ $start = strpos( $uptime, "up " );
+ $length = strpos( $uptime, ", ", $start )-$start;
+ print( substr( $uptime, $start, $length ) ); ?></td>
<td class="bighead" align="center"><strong><a href="http://www.zoneminder.com" ...
! <td class="smallhead" align="right" valign="top"><?= $zmSlangLoad ?>: <?= ...
I would use reg expressions, but I'm not the best at those just yet. :P Anyway, strpos is a faster routine.

Posted: Wed Oct 10, 2007 10:38 am
by KegRaider
Cool mod AmmarossDanan,

I just finished applying it, and looks great. Is there a way to get the hours too? I tried modifying the "+$length = " section, but ended up with all the info (users, load etc).

--> Troy.

Posted: Wed Oct 10, 2007 11:20 pm
by ammaross
Unfortunately, the method being used (the uptime command) only displays days of uptime, unless you've been up for less than a day, then it gives hours (or minutes if up for less than an hour).

If you want, you can pull the exact uptime (in seconds) from /proc/uptime and use that:

Code: Select all

if ( ($uptime = file("/proc/uptime")) != NULL )
{
   if ( ($base = explode( " ", $uptime[0] )) != NULL )
   {
        $days = floor($base[0]/(24*60*60));
        $base[0] -= ($days*24*60*60);
        $hours = floor($base[0]/(60*60));
        $base[0] -= ($hours*60*60);
        $minutes = floor($base[0]/60);
        $base[0] -= ($minutes*60);
        $seconds = floor($base[0]);
        if ($days > 0) print( "$days day" . ($days>1?"s":"") . ", " );
        print(" $hours hr" . ($hours>1?"s":"") . ",
                 $minutes min" . ($minutes>1?"s":"") . ",
                 $seconds sec" . ($seconds>1?"s":"") );

   }
}
else
{
   $uptime = `uptime`;
   $start = strpos( $uptime, "up " );
   $length = strpos( $uptime, ", ", $start )-$start;
   print( substr( $uptime, $start, $length ) );
}
Just plug that in in place of the other uptime code and it should work. If you have any problems, let me know.

($base[1] will contain the idle time of the CPU, in case you wanted to use that for anything as well.)