In Files

Parent

Class/Module Index [+]

Quicksearch

SysInfo

SysInfo

A container for the platform specific system information. Portions of this code were originally from Amazon's EC2 AMI tools, specifically lib/platform.rb.

Public Class Methods

new() click to toggle source
# File lib/sysinfo.rb, line 77
def initialize
  @vm, @os, @impl, @arch = find_platform_info
  @hostname, @ipaddress_internal, @uptime = find_network_info
  @ruby = RUBY_VERSION.split('.').collect { |v| v.to_i }
  @user = ENV['USER']
  require 'Win32API' if @os == :windows && @vm == :ruby
end

Public Instance Methods

find_hostname() click to toggle source

Return the hostname for the local machine

# File lib/sysinfo.rb, line 114
def find_hostname; Socket.gethostname; end
find_ipaddress_internal() click to toggle source

Return the local IP address which receives external traffic from: coderrr.wordpress.com/2008/05/28/get-your-local-ip-address/ NOTE: This does not open a connection to the IP address.

# File lib/sysinfo.rb, line 135
def find_ipaddress_internal
  # turn off reverse DNS resolution temporarily 
  orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true   
  UDPSocket.open {|s| s.connect('65.74.177.129', 1); s.addr.last } # GitHub IP
ensure  
  Socket.do_not_reverse_lookup = orig
end
find_network_info() click to toggle source

Returns [hostname, ipaddr (internal), uptime]

# File lib/sysinfo.rb, line 102
def find_network_info
  hostname, ipaddr, uptime = :unknown, :unknown, :unknown
  begin
    hostname = find_hostname
    ipaddr = find_ipaddress_internal
    uptime = find_uptime       
  rescue => ex # Be silent!
  end
  [hostname, ipaddr, uptime]
end
find_platform_info() click to toggle source

Returns [vm, os, impl, arch]

# File lib/sysinfo.rb, line 86
def find_platform_info
  vm, os, impl, arch = :ruby, :unknown, :unknown, :unknow
  IMPLEMENTATIONS.each do |r, o, i|
    next unless RUBY_PLATFORM =~ r
    os, impl = [o, i]
    break
  end
  ARCHITECTURES.each do |r, a|
    next unless RUBY_PLATFORM =~ r
    arch = a
    break
  end
  os == :java ? guess_java : [vm, os, impl, arch]
end
find_uptime() click to toggle source

Returns the local uptime in hours. Use Win32API in Windows, 'sysctl -b kern.boottime' os osx, and 'who -b' on unix. Based on Ruby Quiz solutions by: Matthias Reitinger On Windows, see also: net statistics server

# File lib/sysinfo.rb, line 120
def find_uptime
  hours = 0
  begin
    seconds = execute_platform_specific("find_uptime") || 0
    hours = seconds / 3600 # seconds to hours
  rescue => ex
    #puts ex.message  # TODO: implement debug?
  end
  hours
end
home() click to toggle source

Returns the path to the current user's home directory

# File lib/sysinfo.rb, line 152
def home; execute_platform_specific(:home); end
paths() click to toggle source

Returns the environment paths as an Array

# File lib/sysinfo.rb, line 150
def paths; execute_platform_specific(:paths); end
platform() click to toggle source

Returns a String of the full platform descriptor in the format: VM-OS-IMPL-ARCH e.g. java-unix-osx-x86_64

# File lib/sysinfo.rb, line 145
def platform
  "#{@vm}-#{@os}-#{@impl}-#{@arch}"
end
shell() click to toggle source

Returns the name of the current shell

# File lib/sysinfo.rb, line 154
def shell; execute_platform_specific(:shell); end
tmpdir() click to toggle source

Returns the path to the current temp directory

# File lib/sysinfo.rb, line 156
def tmpdir; execute_platform_specific(:tmpdir); end

[Validate]

Generated with the Darkfish Rdoc Generator 2.