class 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/sys/unix/sys/uptime.rb, line 92
def self.boot_time
  if RbConfig::CONFIG['host_os'] =~ /linux/i
    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/sys/unix/sys/uptime.rb, line 176
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/sys/unix/sys/uptime.rb, line 206
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/sys/unix/sys/uptime.rb, line 166
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/sys/unix/sys/uptime.rb, line 156
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/sys/unix/sys/uptime.rb, line 127
def self.seconds
  if RbConfig::CONFIG['host_os'] =~ /linux/i
    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/sys/unix/sys/uptime.rb, line 187
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

Private Class Methods

get_dhms(host) click to toggle source

Get the actual days, hours, minutes and seconds since boot using WMI.

# File lib/sys/windows/sys/uptime.rb, line 124
def self.get_dhms(host)
  seconds = get_seconds(host)

  days = (seconds / 86400).to_i
  seconds -= days * 86400
  hours = seconds / 3600
  seconds -= hours * 3600
  minutes = seconds / 60
  seconds -= minutes * 60

  [days, hours, minutes, seconds]
end
get_seconds(host) click to toggle source

Returns the number of seconds since boot.

# File lib/sys/windows/sys/uptime.rb, line 141
def self.get_seconds(host)
  (Time.now - boot_time).to_i
end
parse_ms_date(str) click to toggle source

Converts a string in the format '20040703074625.015625-360' into a Ruby Time object.

# File lib/sys/windows/sys/uptime.rb, line 115
def self.parse_ms_date(str)
  return if str.nil?
  return Time.parse(str.split('.').first)
end