Server Reboot

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben Allen

    #1

    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
  • Peter van Schie

    #2
    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:



    --

    Comment

    • Peter van Schie

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

      --

      Comment

      • Ben Allen

        #4
        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

        Comment

        • Peter van Schie

          #5
          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?


          --

          Comment

          • Gordon Burditt

            #6
            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

            Comment

            • Ben Allen

              #7
              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="#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;
              ?>

              Comment

              • Peter van Schie

                #8
                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.


                --

                Comment

                • Jerry Stuckle

                  #9
                  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@attgl obal.net
                  =============== ===

                  Comment

                  Working...