/
sysinfo.rb
281 lines (249 loc) · 9.44 KB
/
sysinfo.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
require 'socket'
require 'storable'
require 'time'
# = 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.
class SysInfo < Storable
unless defined?(IMPLEMENTATIONS)
VERSION = 0.5.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, :win32, :windows ],
[/windows/i, :win32, :windows ],
[/osx/i, :unix, :osx ],
# TODO: implement other windows matches: # /djgpp|(cyg|ms|bcc)win|mingw/ (from mongrel)
# 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, :win32, :windows ],
[/mingw/i, :win32, :mingw ],
[/bccwin/i, :win32, :bccwin ],
[/wince/i, :win32, :wince ],
[/vms/i, :vms, :vms ],
[/os2/i, :os2, :os2 ],
[nil, :unknown, :unknown ],
].freeze
ARCHITECTURES = [
[/(i\d86)/i, :i386 ],
[/x86_64/i, :x86_64 ],
[/x86/i, :i386 ], # JRuby
[/ia64/i, :ia64 ],
[/alpha/i, :alpha ],
[/sparc/i, :sparc ],
[/mips/i, :mips ],
[/powerpc/i, :powerpc ],
[/universal/i,:i386 ],
[nil, :unknown ],
].freeze
end
field :vm => String
field :os => String
field :impl => String
field :arch => String
field :hostname => String
field :ipaddress_internal => String
#field :ipaddress_external => String
field :uptime => Float
field :paths
field :tmpdir
field :home
field :shell
field :user
field :ruby
alias :implementation :impl
alias :architecture :arch
def initialize
@vm, @os, @impl, @arch = find_platform_info
@hostname, @ipaddress_internal, @uptime = find_network_info
require 'Win32API' if @os == :win32
end
# Returns [vm, os, impl, arch]
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 [hostname, ipaddr (internal), uptime]
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
# Return the hostname for the local machine
def find_hostname; Socket.gethostname; 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
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
# Return the local IP address which receives external traffic
# from: http://coderrr.wordpress.com/2008/05/28/get-your-local-ip-address/
# NOTE: This <em>does not</em> open a connection to the IP address.
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 a Symbol of the short platform descriptor in the format: VM-OS
# e.g. <tt>:java-unix</tt>
def platform
"#{@vm}-#{@os}".to_sym
end
# Returns a String of the full platform descriptor in the format: VM-OS-IMPL-ARCH
# e.g. <tt>java-unix-osx-x86_64</tt>
def to_s(*args)
"#{@vm}-#{@os}-#{@impl}-#{@arch}".to_sym
end
# Returns Ruby version as an Array of Integers. e.g. [1,9,1]
def ruby; RUBY_VERSION.split('.').collect { |v| v.to_i }; end
# Return the name of the current user
def user; ENV['USER']; end
# Returns the environment paths as an Array
def paths; execute_platform_specific(:paths); end
# Returns the path to the current user's home directory
def home; execute_platform_specific(:home); end
# Returns the name of the current shell
def shell; execute_platform_specific(:shell); end
# Returns the path to the current temp directory
def tmpdir; execute_platform_specific(:tmpdir); end
private
# Look for and execute a platform specific method.
# The name of the method will be in the format: +dtype-VM-OS-IMPL+.
# e.g. find_uptime_ruby_unix_osx
#
def execute_platform_specific(dtype)
criteria = [@vm, @os, @impl]
while !criteria.empty?
meth = [dtype, criteria].join('_').to_sym
return self.send(meth) if SysInfo.private_method_defined?(meth)
criteria.pop
end
raise "#{dtype}_#{@vm}_#{@os}_#{@impl} not implemented"
end
def paths_ruby_unix; (ENV['PATH'] || '').split(':'); end
def paths_ruby_win32; (ENV['PATH'] || '').split(';'); end # Not tested!
def paths_java
delim = @impl == :windows ? ';' : ':'
(ENV['PATH'] || '').split(delim)
end
def tmpdir_ruby_unix; (ENV['TMPDIR'] || '/tmp'); end
def tmpdir_ruby_win32; (ENV['TMPDIR'] || 'C:\\temp'); end
def tmpdir_java
default = @impl == :windows ? 'C:\\temp' : '/tmp'
(ENV['TMPDIR'] || default)
end
def shell_ruby_unix; (ENV['SHELL'] || 'bash').to_sym; end
def shell_ruby_win32; :dos; end
alias_method :shell_java_unix, :shell_ruby_unix
alias_method :shell_java_win32, :shell_ruby_win32
def home_ruby_unix; File.expand_path(ENV['HOME']); end
def home_ruby_win32; File.expand_path(ENV['USERPROFILE']); end
def home_java
if @impl == :windows
File.expand_path(ENV['USERPROFILE'])
else
File.expand_path(ENV['HOME'])
end
end
# Ya, this is kinda wack. Ruby -> Java -> Kernel32. See:
# http://www.oreillynet.com/ruby/blog/2008/01/jruby_meets_the_windows_api_1.html
# http://msdn.microsoft.com/en-us/library/ms724408(VS.85).aspx
# Ruby 1.9.1: Win32API is now deprecated in favor of using the DL library.
def find_uptime_java_win32_windows
kernel32 = com.sun.jna.NativeLibrary.getInstance('kernel32')
buf = java.nio.ByteBuffer.allocate(256)
(kernel32.getFunction('GetTickCount').invokeInt([256, buf].to_java).to_f / 1000).to_f
end
def find_uptime_ruby_win32_windows
# Win32API is required in self.guess
getTickCount = Win32API.new("kernel32", "GetTickCount", nil, 'L')
((getTickCount.call()).to_f / 1000).to_f
end
def find_uptime_ruby_unix_osx
# This is faster than "who" and could work on BSD also.
(Time.now.to_f - Time.at(`sysctl -b kern.boottime 2>/dev/null`.unpack('L').first).to_f).to_f
end
# This should work for most unix flavours.
def find_uptime_ruby_unix
# who is sloooooow. Use File.read('/proc/uptime')
(Time.now.to_i - Time.parse(`who -b 2>/dev/null`).to_f)
end
alias_method :find_uptime_java_unix_osx, :find_uptime_ruby_unix
# Determine the values for vm, os, impl, and arch when running on Java.
def guess_java
vm, os, impl, arch = :java, :unknown, :unknown, :unknown
require 'java'
include_class java.lang.System
osname = System.getProperty("os.name")
IMPLEMENTATIONS.each do |r, o, i|
next unless osname =~ r
os, impl = [o, i]
break
end
osarch = System.getProperty("os.arch")
ARCHITECTURES.each do |r, a|
next unless osarch =~ r
arch = a
break
end
[vm, os, impl, arch]
end
# Returns the local IP address based on the hostname.
# According to coderrr (see comments on blog link above), this implementation
# doesn't guarantee that it will return the address for the interface external
# traffic goes through. It's also possible the hostname isn't resolvable to the
# local IP.
#
# NOTE: This code predates the current ip_address_internal. It was just as well
# but the other code is cleaner. I'm keeping this old version here for now.
def ip_address_internal_alt
ipaddr = :unknown
begin
saddr = Socket.getaddrinfo( Socket.gethostname, nil, Socket::AF_UNSPEC, Socket::SOCK_STREAM, nil, Socket::AI_CANONNAME)
ipaddr = saddr.select{|type| type[0] == 'AF_INET' }[0][3]
rescue => ex
end
ipaddr
end
end
if $0 == __FILE__
puts SysInfo.new.to_yaml
end