Parent

Sys::Uptime

The Uptime class encapsulates various bits of information regarding your system's uptime, including boot time.


The Uptime class encapsulates various bits of information regarding your system's uptime, including boot time.

Constants

BOOT_TIME
CTL_KERN
KERN_BOOTTIME
TICKS
VERSION

The version of the sys-uptime library

Public Class Methods

boot_time() click to toggle source

Returns a Time object indicating the time the system was last booted.

Example:

Sys::Uptime.boot_time # => Mon Jul 13 06:08:25 -0600 2009
# File lib/unix/sys/uptime.rb, line 95
def self.boot_time
  if Config::CONFIG['host_os'] =~ /linux/
    Time.now - self.seconds
  elsif respond_to?(:sysctl, true)
    tv = Timeval.new
    mib  = FFI::MemoryPointer.new(:int, 2).write_array_of_int([CTL_KERN, KERN_BOOTTIME])
    size = FFI::MemoryPointer.new(:long, 1).write_int(tv.size)

    if sysctl(mib, 2, tv, size, nil, 0) != 0
      raise SystemCallError, 'sysctl() - ' + strerror(FFI.errno)
    end

    Time.at(tv[:tv_sec], tv[:tv_usec])
  else
    begin
      setutxent()
      while ent = Utmpx.new(getutxent())
        if ent[:ut_type] == BOOT_TIME
          time = Time.at(ent[:ut_tv][:tv_sec], ent[:ut_tv][:tv_usec])
          break
        end
      end
    ensure
      endutxent()
    end
    time
  end
end
days() click to toggle source

Returns the total number of days of uptime.

Example:

Sys::Uptime.days # => 2
# File lib/unix/sys/uptime.rb, line 179
def self.days
  seconds / 86400
end
dhms() click to toggle source

Returns the uptime as a four element array, including days, hours, minutes and seconds.

Example:

Sys::Uptime.dhms # => [1,9,24,57]
# File lib/unix/sys/uptime.rb, line 209
def self.dhms
  uptime.split(':')
end
hours() click to toggle source

Returns the total number of hours of uptime.

Example:

Sys::Uptime.hours # => 31
# File lib/unix/sys/uptime.rb, line 169
def self.hours
  seconds / 3600
end
minutes() click to toggle source

Returns the total number of minutes of uptime.

Example:

Sys::Uptime.minutes # => 678
# File lib/unix/sys/uptime.rb, line 159
def self.minutes
  seconds / 60
end
seconds() click to toggle source

Returns the total number of seconds of uptime.

Example:

Sys::Uptime.seconds => 118800
# File lib/unix/sys/uptime.rb, line 130
def self.seconds
  if Config::CONFIG['host_os'] =~ /linux/
    begin
      IO.read('/proc/uptime').split.first.to_i
    rescue Exception => err
      raise Error, err
    end
  elsif respond_to?(:sysctl, true)
    tv   = Timeval.new
    mib  = FFI::MemoryPointer.new(:int, 2).write_array_of_int([CTL_KERN, KERN_BOOTTIME])
    size = FFI::MemoryPointer.new(:long, 1).write_int(tv.size)

    if sysctl(mib, 2, tv, size, nil, 0) != 0
      raise SystemCallError, 'sysctl() - ' + strerror(FFI.errno)
    end

    time(nil) - tv[:tv_sec]
  else
    tms = Tms.new
    times(tms) / TICKS
  end
end
uptime() click to toggle source

Returns the uptime as a colon separated string, including days, hours, minutes and seconds.

Example:

Sys::Uptime.uptime # => "1:9:24:57"
# File lib/unix/sys/uptime.rb, line 190
def self.uptime
  secs  = seconds
  days  = secs / 86400
  secs  -= days * 86400
  hours = secs / 3600
  secs  -= hours * 3600
  mins  = secs / 60
  secs  -= mins * 60

  "#{days}:#{hours}:#{mins}:#{secs}"
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.