Class: PosixPsutil::System

Inherits:
Object
  • Object
show all
Defined in:
lib/posixpsutil/linux/system.rb

Class Method Summary collapse

Class Method Details

.boot_timeObject

return system boot time expressed in seconds since epoch



516
517
518
519
# File 'lib/posixpsutil/linux/system.rb', line 516

def self.boot_time
  @boot_at = PsutilHelper::boot_time() if @boot_at.nil?
  @boot_at
end

.usersObject

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



477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/posixpsutil/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