you are in: codestackercodes [RSS] → tag: linux [RSS]

get server uptime with php Delicious Email

show/hide lines
   1  <?php 
   2  // format the uptime in case the browser doesn't support dhtml/javascript
   3  // static uptime string
   4  function format_uptime($seconds) {
   5    $secs = intval($seconds % 60);
   6    $mins = intval($seconds / 60 % 60);
   7    $hours = intval($seconds / 3600 % 24);
   8    $days = intval($seconds / 86400);
   9    
  10    if ($days > 0) {
  11      $uptimeString .= $days;
  12      $uptimeString .= (($days == 1) ? " day" : " days");
  13    }
  14    if ($hours > 0) {
  15      $uptimeString .= (($days > 0) ? ", " : "") . $hours;
  16      $uptimeString .= (($hours == 1) ? " hour" : " hours");
  17    }
  18    if ($mins > 0) {
  19      $uptimeString .= (($days > 0 || $hours > 0) ? ", " : "") . $mins;
  20      $uptimeString .= (($mins == 1) ? " minute" : " minutes");
  21    }
  22    if ($secs > 0) {
  23      $uptimeString .= (($days > 0 || $hours > 0 || $mins > 0) ? ", " : "") . $secs;
  24      $uptimeString .= (($secs == 1) ? " second" : " seconds");
  25    }
  26    return $uptimeString;
  27  }
  28  
  29  // read in the uptime (using exec)
  30  $uptime = exec("cat /proc/uptime");
  31  $uptime = split(" ",$uptime);
  32  $uptimeSecs = $uptime[0];
  33  
  34  // get the static uptime
  35  $staticUptime = "Uptime: ".format_uptime($uptimeSecs);
  36  ?>
  37  <html>
  38  <head>
  39  <script language="javascript">
  40  <!--
  41  var upSeconds=<?php echo $uptimeSecs; ?>;
  42  function doUptime() {
  43  var uptimeString = "Uptime: ";
  44  var secs = parseInt(upSeconds % 60);
  45  var mins = parseInt(upSeconds / 60 % 60);
  46  var hours = parseInt(upSeconds / 3600 % 24);
  47  var days = parseInt(upSeconds / 86400);
  48  if (days > 0) {
  49    uptimeString += days;
  50    uptimeString += ((days == 1) ? " day" : " days");
  51  }
  52  if (hours > 0) {
  53    uptimeString += ((days > 0) ? ", " : "") + hours;
  54    uptimeString += ((hours == 1) ? " hour" : " hours");
  55  }
  56  if (mins > 0) {
  57    uptimeString += ((days > 0 || hours > 0) ? ", " : "") + mins;
  58    uptimeString += ((mins == 1) ? " minute" : " minutes");
  59  }
  60  if (secs > 0) {
  61    uptimeString += ((days > 0 || hours > 0 || mins > 0) ? ", " : "") + secs;
  62    uptimeString += ((secs == 1) ? " second" : " seconds");
  63  }
  64  var span_el = document.getElementById("uptime");
  65  var replaceWith = document.createTextNode(uptimeString);
  66  span_el.replaceChild(replaceWith, span_el.childNodes[0]);
  67  upSeconds++;
  68  setTimeout("doUptime()",1000);
  69  }
  70  // -->
  71  </script>
  72  </head>
  73  <body onLoad="doUptime();">
  74  
  75  <div id="uptime"><?php echo $staticUptime; ?></div>
  76  
  77  </body>
  78  </html>
created by leozera — 20 January 2011 — get a short url — tags: linux php uptime embed