Class: Sys::Uptime

Inherits:
Object
  • Object
show all
Extended by:
FFI::Library
Defined in:
lib/unix/sys/uptime.rb,
lib/windows/sys/uptime.rb

Overview

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

Defined Under Namespace

Classes: Error, ExitStatus, Timeval, Tms, Utmpx

Constant Summary collapse

VERSION =

The version of the sys-uptime library.

'0.7.0'

Class Method Summary collapse

Class Method Details

.boot_time(host = Socket.gethostname) ⇒ Object

Returns the boot time as a Time object.

Example:

Sys::Uptime.boot_time # => Fri Dec 12 20:18:58 -0700 2008


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/unix/sys/uptime.rb', line 95

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(host = Socket.gethostname) ⇒ Object

Returns the total number of days the system has been up on host, or the localhost if no host is provided.

Example:

Sys::Uptime.days # => 1


179
180
181
# File 'lib/unix/sys/uptime.rb', line 179

def self.days
  seconds / 86400
end

.dhms(host = Socket.gethostname) ⇒ Object

Calculates and returns the number of days, hours, minutes and seconds the host has been running as a four-element Array. The localhost is used if no host is provided.

Example:

Sys::Uptime.dhms # => [1, 9, 55, 11]


209
210
211
# File 'lib/unix/sys/uptime.rb', line 209

def self.dhms
  uptime.split(':')
end

.hours(host = Socket.gethostname) ⇒ Object

Returns the total number of hours the system has been up on host, or the localhost if no host is provided.

Example:

Sys::Uptime.hours # => 33


169
170
171
# File 'lib/unix/sys/uptime.rb', line 169

def self.hours
  seconds / 3600
end

.minutes(host = Socket.gethostname) ⇒ Object

Returns the total number of minutes the system has been up on host, or the localhost if no host is provided.

Example:

Sys::Uptime.minutes # => 1980


159
160
161
# File 'lib/unix/sys/uptime.rb', line 159

def self.minutes
  seconds / 60
end

.seconds(host = Socket.gethostname) ⇒ Object

Returns the total number of seconds the system has been up on host, or the localhost if no host is provided.

Example:

Sys::Uptime.seconds # => 118800


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/unix/sys/uptime.rb', line 130

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(host = Socket.gethostname) ⇒ Object

Calculates and returns the number of days, hours, minutes and seconds the host has been running as a colon-separated string.

The localhost is used if no host is provided.

Example:

Sys::Uptime.uptime # => "1:9:55:11"


190
191
192
193
194
195
196
197
198
199
200
# 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