Class: Sys::Uptime

Inherits:
Object
  • Object
show all
Extended by:
FFI::Library
Defined in:
lib/sys/uptime.rb,
lib/sys/unix/sys/uptime.rb,
lib/sys/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

Constant Summary collapse

VERSION =

The version of the sys-uptime library

'0.8.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


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/sys/unix/sys/uptime.rb', line 117

def self.boot_time
  if RbConfig::CONFIG['host_os'] =~ /linux/i
    Time.now - 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 function failed: #{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


203
204
205
# File 'lib/sys/unix/sys/uptime.rb', line 203

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]


233
234
235
# File 'lib/sys/unix/sys/uptime.rb', line 233

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


193
194
195
# File 'lib/sys/unix/sys/uptime.rb', line 193

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


183
184
185
# File 'lib/sys/unix/sys/uptime.rb', line 183

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


152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/sys/unix/sys/uptime.rb', line 152

def self.seconds
  if RbConfig::CONFIG['host_os'] =~ /linux/i
    # rubocop:disable Lint/RescueException
    begin
      File.read('/proc/uptime').split.first.to_i
    rescue Exception => err
      raise Error, err
    end
    # rubocop:enable Lint/RescueException
  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 function failed: #{strerror(FFI.errno)}"
    end

    time(nil) - tv[:tv_sec]
  else
    tms = Tms.new
    times(tms) / ticks_per_second
  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"


214
215
216
217
218
219
220
221
222
223
224
# File 'lib/sys/unix/sys/uptime.rb', line 214

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