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.7.3".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           ],
  [nil,         :unknown          ],
].freeze

Instance Method Summary collapse

Constructor Details

#initializeSysInfo

Returns a new instance of SysInfo.



77
78
79
80
81
82
83
# 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

Instance Method Details

#find_hostnameObject

Return the hostname for the local machine



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

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.



135
136
137
138
139
140
141
# 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_infoObject

Returns [hostname, ipaddr (internal), uptime]



102
103
104
105
106
107
108
109
110
111
# 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_infoObject

Returns [vm, os, impl, arch]



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# 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_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



120
121
122
123
124
125
126
127
128
129
# 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

#homeObject

Returns the path to the current user’s home directory



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

def home; execute_platform_specific(:home); end

#pathsObject

Returns the environment paths as an Array



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

def paths; execute_platform_specific(:paths); end

#platformObject

Returns a Symbol of the short platform descriptor in the format: VM-OS e.g. :java-unix



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

def platform
  "#{@vm}-#{@os}".to_sym
end

#shellObject

Returns the name of the current shell



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

def shell; execute_platform_specific(:shell); end

#tmpdirObject

Returns the path to the current temp directory



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

def tmpdir; execute_platform_specific(:tmpdir); end

#to_s(*args) ⇒ Object

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



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

def to_s(*args)
  "#{@vm}-#{@os}-#{@impl}-#{@arch}".to_sym
end