Storable
A container for the platform specific system information. Portions of this code were originally from Amazon's EC2 AMI tools, specifically lib/platform.rb.
# 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
Return the hostname for the local machine
# File lib/sysinfo.rb, line 114 def find_hostname; Socket.gethostname; end
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
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
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
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
Returns the path to the current user's home directory
# File lib/sysinfo.rb, line 152 def home; execute_platform_specific(:home); end
Returns the environment paths as an Array
# File lib/sysinfo.rb, line 150 def paths; execute_platform_specific(:paths); end
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
Returns the name of the current shell
# File lib/sysinfo.rb, line 154 def shell; execute_platform_specific(:shell); end
Generated with the Darkfish Rdoc Generator 2.