Class: CzSystemInfo::Uptime

Inherits:
Object
  • Object
show all
Extended by:
FFI::Library
Defined in:
lib/cz_system_info/uptime.rb

Overview

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

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Class Method Details

.boot_timeObject

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

Example:

CzSystemInfo::Uptime.boot_time # => Mon Jul 13 06:08:25 -0600 2009


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/cz_system_info/uptime.rb', line 102

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

.daysObject

Returns the total number of days of uptime.

Example:

CzSystemInfo::Uptime.days # => 2


188
189
190
# File 'lib/cz_system_info/uptime.rb', line 188

def self.days
  seconds / 86400
end

.dhmsObject

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

Example:

CzSystemInfo::Uptime.dhms # => [1,9,24,57]


218
219
220
# File 'lib/cz_system_info/uptime.rb', line 218

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

.hoursObject

Returns the total number of hours of uptime.

Example:

CzSystemInfo::Uptime.hours # => 31


178
179
180
# File 'lib/cz_system_info/uptime.rb', line 178

def self.hours
  seconds / 3600
end

.minutesObject

Returns the total number of minutes of uptime.

Example:

CzSystemInfo::Uptime.minutes # => 678


168
169
170
# File 'lib/cz_system_info/uptime.rb', line 168

def self.minutes
  seconds / 60
end

.secondsObject

Returns the total number of seconds of uptime.

Example:

CzSystemInfo::Uptime.seconds => 118800


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/cz_system_info/uptime.rb', line 137

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
  end
end

.uptimeObject

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

Example:

CzSystemInfo::Uptime.uptime # => "1:9:24:57"


199
200
201
202
203
204
205
206
207
208
209
# File 'lib/cz_system_info/uptime.rb', line 199

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