class PosixPsutil::System

Public Class Methods

boot_time() click to toggle source

return system boot time expressed in seconds since epoch

# File linux/system.rb, line 516
def self.boot_time
  @boot_at = PsutilHelper::boot_time() if @boot_at.nil?
  @boot_at
end
users() click to toggle source

Return currently connected users as a list of OpenStruct<#name, tty, host(hostname), started(the time logined in)>. Unlike psutil, started returned here is a DateTime instead of timestamp

# File linux/system.rb, line 477
def self.users
  users = []
  begin
    name = FFI::MemoryPointer.new(:char, 32)
    tty = FFI::MemoryPointer.new(:char, 32)
    host = FFI::MemoryPointer.new(:char, 256)
    tstamp = FFI::MemoryPointer.new(:int, 1)
    user_process = FFI::MemoryPointer.new(:short, 1)
    LibPosixPsutil::setutent()
    loop do
      status = LibPosixPsutil::get_user(name, tty, host, 
                                        tstamp, user_process)
      case status
      when -1
        break
      when 0
        next if user_process.read_short == 0
        # note: the underlying C function includes entries about
        # system boot, run level and others.  We might want
        # to use them in the future.
        hostname =  host.read_string
        hostname = 'localhost' if hostname == ':0' || hostname == ':0.0'
        # keep the timestamp in epoch format, 
        # let user define what they want, UTC or Local time
        ts = tstamp.read_int
        users.push(OpenStruct.new({
          name: name.read_string, terminal: tty.read_string || nil,
          host: hostname, started: ts}))
      else
        raise SystemCallError.new('in get_user', status)
      end
    end
  ensure
    LibPosixPsutil::endutent()
  end
  users
end