Class: SysInfo

Inherits:
Storable
  • Object
show all
Defined in:
lib/sysinfo.rb

Overview

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.

Constant Summary collapse

VERSION =
"0.10.0".freeze
IMPLEMENTATIONS =
[

  # These are for JRuby, System.getproperty('os.name').
  # For a list of all values, see: http://lopica.sourceforge.net/os.html

  #regexp matcher       os        implementation
  [/mac\s*os\s*x/i,     :unix,    :osx              ],
  [/sunos/i,            :unix,    :solaris          ],
  [/windows\s*ce/i,     :windows, :wince            ],
  [/windows/i,          :windows, :windows          ],
  [/osx/i,              :unix,    :osx              ],

  # These are for RUBY_PLATFORM and JRuby
  [/java/i,             :java,    :java             ],
  [/darwin/i,           :unix,    :osx              ],
  [/linux/i,            :unix,    :linux            ],
  [/freebsd/i,          :unix,    :freebsd          ],
  [/netbsd/i,           :unix,    :netbsd           ],
  [/solaris/i,          :unix,    :solaris          ],
  [/irix/i,             :unix,    :irix             ],
  [/cygwin/i,           :unix,    :cygwin           ],
  [/mswin/i,            :windows, :windows          ],
  [/djgpp/i,            :windows, :djgpp            ],
  [/mingw/i,            :windows, :mingw            ],
  [/bccwin/i,           :windows, :bccwin           ],
  [/wince/i,            :windows, :wince            ],
  [/vms/i,              :vms,     :vms              ],
  [/os2/i,              :os2,     :os2              ],
  [nil,                 :unknown, :unknown          ],
].freeze
ARCHITECTURES =
[
  [/(i\d86)/i,  :x86              ],
  [/x86_64/i,   :x86_64           ],
  [/x86/i,      :x86              ],  # JRuby
  [/ia64/i,     :ia64             ],
  [/alpha/i,    :alpha            ],
  [/sparc/i,    :sparc            ],
  [/mips/i,     :mips             ],
  [/powerpc/i,  :powerpc          ],
  [/universal/i,:x86_64           ],
  [/arm64/i,    :arm64            ],
  [nil,         :unknown          ],
].freeze

Instance Method Summary collapse

Constructor Details

#initializeSysInfo

Returns a new instance of SysInfo.



78
79
80
81
82
83
84
# File 'lib/sysinfo.rb', line 78

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 = getpwattr(:name) || ENV['USER']
  require 'Win32API' if @os == :windows && @vm == :ruby
end

Instance Method Details

#find_hostnameObject

Return the hostname for the local machine



115
# File 'lib/sysinfo.rb', line 115

def find_hostname; Socket.gethostname; end

#find_ipaddress_internalObject

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.



136
137
138
139
140
141
142
# File 'lib/sysinfo.rb', line 136

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_infoObject

Returns [hostname, ipaddr (internal), uptime]



103
104
105
106
107
108
109
110
111
112
# File 'lib/sysinfo.rb', line 103

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_infoObject

Returns [vm, os, impl, arch]



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/sysinfo.rb', line 87

def find_platform_info
  vm, os, impl, arch = :ruby, :unknown, :unknown, :unknown
  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_uptimeObject

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



121
122
123
124
125
126
127
128
129
130
# File 'lib/sysinfo.rb', line 121

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

#homeObject

Returns the path to the current user’s home directory



153
# File 'lib/sysinfo.rb', line 153

def home; execute_platform_specific(:home); end

#pathsObject

Returns the environment paths as an Array



151
# File 'lib/sysinfo.rb', line 151

def paths; execute_platform_specific(:paths); end

#platformObject

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



146
147
148
# File 'lib/sysinfo.rb', line 146

def platform
  "#{@vm}-#{@os}-#{@impl}-#{@arch}"
end

#shellObject

Returns the name of the current shell



155
# File 'lib/sysinfo.rb', line 155

def shell; execute_platform_specific(:shell); end

#tmpdirObject

Returns the path to the current temp directory



157
# File 'lib/sysinfo.rb', line 157

def tmpdir; execute_platform_specific(:tmpdir); end