sysinfo 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES.txt +8 -0
- data/README.rdoc +65 -1
- data/Rakefile +1 -1
- data/bin/sysinfo +77 -0
- data/lib/sysinfo.rb +162 -188
- data/sysinfo.gemspec +4 -4
- metadata +14 -3
data/CHANGES.txt
CHANGED
@@ -1,6 +1,14 @@
|
|
1
1
|
SYSINFO, CHANGES
|
2
2
|
|
3
3
|
|
4
|
+
#### 0.5.1 (2009-05-07) ###############################
|
5
|
+
|
6
|
+
* CHANGE: Hella cleanup in preparation for future expansion.
|
7
|
+
* CHANGE: Modified platform, now returns VM-OS instead of OS-IMPL
|
8
|
+
* ADDED: to_s which returns a full platform string
|
9
|
+
* ADDED: sysinfo executable
|
10
|
+
|
11
|
+
|
4
12
|
#### 0.5 (2009-05-07) ###############################
|
5
13
|
|
6
14
|
* First public release. See commit history for solutious-stella, solutious-rudy,
|
data/README.rdoc
CHANGED
@@ -2,6 +2,65 @@
|
|
2
2
|
|
3
3
|
All your system-independent infoz in one handy class.
|
4
4
|
|
5
|
+
SysInfo does a takes a very quick glance at the system it's running on and exposes the results as YAML, JSON, CSV, or TSV. It also determines a platform identifier for the system that takes the form: VM-OS-IMPLEMENTATION-ARCHITECTURE.
|
6
|
+
|
7
|
+
=== Platform Identifier Examples
|
8
|
+
|
9
|
+
ruby-unix-osx-i386
|
10
|
+
ruby-unix-osx-powerpc
|
11
|
+
ruby-unix-linux-x86_64
|
12
|
+
java-win32-windows-i386
|
13
|
+
java-win32-mingw-i386
|
14
|
+
|
15
|
+
For the complete list of operating systems, implementations and architectures that SysInfo is aware of, see:
|
16
|
+
|
17
|
+
* <tt>$ sysinfo os</tt>
|
18
|
+
* <tt>$ sysinfo impl</tt>
|
19
|
+
* <tt>$ sysinfo arch</tt>
|
20
|
+
|
21
|
+
== Usage -- Library
|
22
|
+
|
23
|
+
sysinfo = SysInfo.new
|
24
|
+
p sysinfo.vm # => ruby
|
25
|
+
p sysinfo.os # => unix
|
26
|
+
p sysinfo.impl # => osx
|
27
|
+
p sysinfo.arch # => i386
|
28
|
+
p sysinfo.platform # => ruby-unix
|
29
|
+
p sysinfo.to_s # => ruby-unix-osx-i386
|
30
|
+
|
31
|
+
p sysinfo.user # => delano
|
32
|
+
p sysinfo.home # => /Users/delano
|
33
|
+
p sysinfo.uptime # => 290.429 (hours)
|
34
|
+
p sysinfo.shell # => /bin/bash
|
35
|
+
p sysinfo.paths # => [/sbin, /bin, /usr/bin, ...]
|
36
|
+
|
37
|
+
p sysinfo.hostname # => walter
|
38
|
+
p sysinfo.ipaddress_internal # => 10.0.1.2
|
39
|
+
p sysinfo.uptime # => 290.573655656974
|
40
|
+
p sysinfo.ruby # => [1,9,1]
|
41
|
+
|
42
|
+
== Usage -- Executable
|
43
|
+
|
44
|
+
$ sysinfo
|
45
|
+
ruby-unix-osx-i386
|
46
|
+
|
47
|
+
$ /usr/jruby/bin/sysinfo
|
48
|
+
java-unix-osx-x86_64
|
49
|
+
|
50
|
+
$ sysinfo -f yaml
|
51
|
+
:vm: :ruby
|
52
|
+
:os: :unix
|
53
|
+
:impl: :osx
|
54
|
+
...
|
55
|
+
:shell: :"/bin/bash"
|
56
|
+
:user: delano
|
57
|
+
|
58
|
+
$ sysinfo -f json
|
59
|
+
{"vm":"ruby","os":"unix","impl":"osx", ..., "shell":"\/bin\/bash","user":"delano"}
|
60
|
+
|
61
|
+
$ sysinfo -f csv
|
62
|
+
ruby,unix,osx, ... /bin/bash,delano
|
63
|
+
|
5
64
|
== Installation
|
6
65
|
|
7
66
|
Via Rubygems, one of:
|
@@ -12,11 +71,16 @@ Via Rubygems, one of:
|
|
12
71
|
or via download:
|
13
72
|
* sysinfo-latest.tar.gz[http://github.com/delano/sysinfo/tarball/latest]
|
14
73
|
* sysinfo-latest.zip[http://github.com/delano/sysinfo/zipball/latest]
|
15
|
-
|
74
|
+
|
75
|
+
== Prerequisites
|
76
|
+
|
77
|
+
* Ruby 1.8, Ruby 1.9, or JRuby 1.2
|
78
|
+
* Storable[http://github.com/delano/storable]
|
16
79
|
|
17
80
|
== Credits
|
18
81
|
|
19
82
|
* Delano Mandelbaum (delano@solutious.com)
|
83
|
+
* Portions of this code were originally from Amazon's EC2 AMI tools, specifically lib/platform.rb.
|
20
84
|
|
21
85
|
== License
|
22
86
|
|
data/Rakefile
CHANGED
data/bin/sysinfo
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
# SysInfo -- A Command Line Interface
|
4
|
+
#
|
5
|
+
# If your reading this via the rdocs you won't be able to see the code
|
6
|
+
# See: http://github.com/delano/sysinfo/blob/master/bin/sysinfo
|
7
|
+
#
|
8
|
+
# Usage: bin/sysinfo
|
9
|
+
#
|
10
|
+
|
11
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) # Make sure our local lib is first in line
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'drydock'
|
15
|
+
require 'sysinfo'
|
16
|
+
|
17
|
+
module SysInfoCLI #:nodoc:
|
18
|
+
extend Drydock
|
19
|
+
|
20
|
+
debug :off
|
21
|
+
default :info
|
22
|
+
|
23
|
+
global :f, :format, String, "Output format. One of: yaml, json, csv, tsv, string (default)"
|
24
|
+
global :v, :verbose, "Output everything I know"
|
25
|
+
global :V, :version, "Display version number" do
|
26
|
+
puts "SysInfo version: #{SysInfo::VERSION}"
|
27
|
+
exit 0
|
28
|
+
end
|
29
|
+
|
30
|
+
about "Display system information"
|
31
|
+
command :info do |obj|
|
32
|
+
format = obj.global.format || :string
|
33
|
+
format = :yaml if obj.global.verbose == true
|
34
|
+
si = SysInfo.new
|
35
|
+
puts si.dump(format)
|
36
|
+
end
|
37
|
+
|
38
|
+
about "Display list of known architectures"
|
39
|
+
command :arch do |obj|
|
40
|
+
arch = SysInfo::ARCHITECTURES.collect { |arch|
|
41
|
+
next if arch[0].nil?
|
42
|
+
obj.global.verbose == true ? "%-15s -> %s" % [arch[1], arch[0].source] : arch[1]
|
43
|
+
}
|
44
|
+
puts arch.compact
|
45
|
+
end
|
46
|
+
command_alias :arch, :architectures
|
47
|
+
|
48
|
+
about "Display list of known OS implementations"
|
49
|
+
command :impl do |obj|
|
50
|
+
reorg = {}
|
51
|
+
SysInfo::IMPLEMENTATIONS.each do |arch|
|
52
|
+
next if arch[0].nil?
|
53
|
+
reorg.store(arch[2].to_s, arch[0])
|
54
|
+
end
|
55
|
+
impl = reorg.keys.sort.collect do |key|
|
56
|
+
obj.global.verbose == true ? "%-15s -> %s" % [key, reorg[key].source] : key
|
57
|
+
end
|
58
|
+
puts impl.compact.uniq
|
59
|
+
end
|
60
|
+
command_alias :impl, :implementations
|
61
|
+
|
62
|
+
about "Display list of known operating systems (OS)"
|
63
|
+
command :os do |obj|
|
64
|
+
reorg = {}
|
65
|
+
SysInfo::IMPLEMENTATIONS.each do |arch|
|
66
|
+
next if arch[0].nil?
|
67
|
+
reorg.store(arch[1].to_s, arch[0])
|
68
|
+
end
|
69
|
+
impl = reorg.keys.sort.collect do |key|
|
70
|
+
obj.global.verbose == true ? "%-15s -> %s" % [key, reorg[key].source] : key
|
71
|
+
end
|
72
|
+
puts impl.compact.uniq
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
|
data/lib/sysinfo.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
require 'socket'
|
2
|
+
require 'storable'
|
3
|
+
require 'time'
|
2
4
|
|
3
5
|
# = SysInfo
|
4
6
|
#
|
@@ -7,7 +9,7 @@ require 'socket'
|
|
7
9
|
# specifically lib/platform.rb.
|
8
10
|
class SysInfo < Storable
|
9
11
|
unless defined?(IMPLEMENTATIONS)
|
10
|
-
VERSION = 5.freeze
|
12
|
+
VERSION = 0.5.freeze
|
11
13
|
IMPLEMENTATIONS = [
|
12
14
|
|
13
15
|
# These are for JRuby, System.getproperty('os.name').
|
@@ -48,257 +50,229 @@ class SysInfo < Storable
|
|
48
50
|
[/sparc/i, :sparc ],
|
49
51
|
[/mips/i, :mips ],
|
50
52
|
[/powerpc/i, :powerpc ],
|
51
|
-
[/universal/i,:
|
53
|
+
[/universal/i,:i386 ],
|
52
54
|
[nil, :unknown ],
|
53
55
|
].freeze
|
54
56
|
end
|
55
57
|
|
56
|
-
|
58
|
+
field :vm => String
|
57
59
|
field :os => String
|
58
|
-
field :
|
59
|
-
field :
|
60
|
+
field :impl => String
|
61
|
+
field :arch => String
|
60
62
|
field :hostname => String
|
61
|
-
field :
|
62
|
-
field :ipaddress_external => String
|
63
|
+
field :ipaddress_internal => String
|
64
|
+
#field :ipaddress_external => String
|
63
65
|
field :uptime => Float
|
64
66
|
|
67
|
+
field :paths
|
68
|
+
field :tmpdir
|
69
|
+
field :home
|
70
|
+
field :shell
|
71
|
+
field :user
|
72
|
+
field :ruby
|
65
73
|
|
66
|
-
alias :
|
67
|
-
alias :
|
74
|
+
alias :implementation :impl
|
75
|
+
alias :architecture :arch
|
68
76
|
|
69
|
-
|
70
77
|
def initialize
|
71
|
-
@os, @
|
72
|
-
@hostname, @
|
78
|
+
@vm, @os, @impl, @arch = find_platform_info
|
79
|
+
@hostname, @ipaddress_internal, @uptime = find_network_info
|
80
|
+
require 'Win32API' if @os == :win32
|
73
81
|
end
|
74
82
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
# Returns [os, impl, arch]
|
79
|
-
def guess
|
80
|
-
os = :unknown
|
81
|
-
impl = :unknown
|
82
|
-
arch = :unknown
|
83
|
+
# Returns [vm, os, impl, arch]
|
84
|
+
def find_platform_info
|
85
|
+
vm, os, impl, arch = :ruby, :unknown, :unknown, :unknow
|
83
86
|
IMPLEMENTATIONS.each do |r, o, i|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
end
|
87
|
+
next unless RUBY_PLATFORM =~ r
|
88
|
+
os, impl = [o, i]
|
89
|
+
break
|
88
90
|
end
|
89
91
|
ARCHITECTURES.each do |r, a|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
#
|
97
|
-
if os == :win32
|
98
|
-
#require 'Win32API'
|
99
|
-
|
100
|
-
# If we're running in java, we'll need to look elsewhere
|
101
|
-
# for the implementation and architecture.
|
102
|
-
# We'll replace IMPL and ARCH with what we find.
|
103
|
-
elsif os == :java
|
104
|
-
require 'java'
|
105
|
-
include_class java.lang.System
|
106
|
-
|
107
|
-
osname = System.getProperty("os.name")
|
108
|
-
IMPLEMENTATIONS.each do |r, o, i|
|
109
|
-
if r and osname =~ r
|
110
|
-
impl = i
|
111
|
-
break
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
osarch = System.getProperty("os.arch")
|
116
|
-
ARCHITECTURES.each do |r, a|
|
117
|
-
if r and osarch =~ r
|
118
|
-
arch = a
|
119
|
-
break
|
120
|
-
end
|
121
|
-
end
|
122
|
-
|
92
|
+
next unless RUBY_PLATFORM =~ r
|
93
|
+
arch = a
|
94
|
+
break
|
123
95
|
end
|
124
|
-
|
125
|
-
[os, impl, arch]
|
96
|
+
os == :java ? guess_java : [vm, os, impl, arch]
|
126
97
|
end
|
127
98
|
|
128
|
-
# Returns [hostname, ipaddr, uptime]
|
129
|
-
def
|
130
|
-
hostname = :unknown
|
131
|
-
ipaddr = :unknown
|
132
|
-
uptime = :unknown
|
133
|
-
|
99
|
+
# Returns [hostname, ipaddr (internal), uptime]
|
100
|
+
def find_network_info
|
101
|
+
hostname, ipaddr, uptime = :unknown, :unknown, :unknown
|
134
102
|
begin
|
135
|
-
hostname =
|
136
|
-
ipaddr =
|
137
|
-
uptime =
|
138
|
-
rescue => ex
|
139
|
-
# Be silent!
|
103
|
+
hostname = find_hostname
|
104
|
+
ipaddr = find_ipaddress_internal
|
105
|
+
uptime = find_uptime
|
106
|
+
rescue => ex # Be silent!
|
140
107
|
end
|
141
|
-
|
142
108
|
[hostname, ipaddr, uptime]
|
143
109
|
end
|
144
110
|
|
145
|
-
|
146
|
-
def
|
147
|
-
Socket.gethostname
|
148
|
-
end
|
149
|
-
|
111
|
+
# Return the hostname for the local machine
|
112
|
+
def find_hostname; Socket.gethostname; end
|
150
113
|
|
151
114
|
# Returns the local uptime in hours. Use Win32API in Windows,
|
152
115
|
# 'sysctl -b kern.boottime' os osx, and 'who -b' on unix.
|
153
116
|
# Based on Ruby Quiz solutions by: Matthias Reitinger
|
154
117
|
# On Windows, see also: net statistics server
|
155
|
-
def
|
156
|
-
|
157
|
-
# Each method must return uptime in seconds
|
158
|
-
methods = {
|
159
|
-
|
160
|
-
:win32_windows => lambda {
|
161
|
-
# Win32API is required in self.guess
|
162
|
-
getTickCount = Win32API.new("kernel32", "GetTickCount", nil, 'L')
|
163
|
-
((getTickCount.call()).to_f / 1000).to_f
|
164
|
-
},
|
165
|
-
|
166
|
-
# Ya, this is kinda wack. Ruby -> Java -> Kernel32. See:
|
167
|
-
# http://www.oreillynet.com/ruby/blog/2008/01/jruby_meets_the_windows_api_1.html
|
168
|
-
# http://msdn.microsoft.com/en-us/library/ms724408(VS.85).aspx
|
169
|
-
# Ruby 1.9.1: Win32API is now deprecated in favor of using the DL library.
|
170
|
-
:java_windows => lambda {
|
171
|
-
kernel32 = com.sun.jna.NativeLibrary.getInstance('kernel32')
|
172
|
-
buf = java.nio.ByteBuffer.allocate(256)
|
173
|
-
(kernel32.getFunction('GetTickCount').invokeInt([256, buf].to_java).to_f / 1000).to_f
|
174
|
-
},
|
175
|
-
|
176
|
-
:unix_osx => lambda {
|
177
|
-
# This is faster than who and could work on BSD also.
|
178
|
-
(Time.now.to_f - Time.at(`sysctl -b kern.boottime 2>/dev/null`.unpack('L').first).to_f).to_f
|
179
|
-
},
|
180
|
-
# This should work for most unix flavours.
|
181
|
-
:unix => lambda {
|
182
|
-
# who is sloooooow. Use File.read('/proc/uptime')
|
183
|
-
(Time.now.to_f - Time.parse(`who -b 2>/dev/null`).to_f)
|
184
|
-
}
|
185
|
-
}
|
186
|
-
|
118
|
+
def find_uptime
|
187
119
|
hours = 0
|
188
|
-
|
189
120
|
begin
|
190
|
-
|
191
|
-
|
192
|
-
hours = (method.call) / 3600 # seconds to hours
|
121
|
+
seconds = execute_platform_specific("find_uptime") || 0
|
122
|
+
hours = seconds / 3600 # seconds to hours
|
193
123
|
rescue => ex
|
124
|
+
#puts ex.message # TODO: implement debug?
|
194
125
|
end
|
195
126
|
hours
|
196
127
|
end
|
197
128
|
|
198
|
-
|
199
|
-
#
|
129
|
+
|
200
130
|
# Return the local IP address which receives external traffic
|
201
131
|
# from: http://coderrr.wordpress.com/2008/05/28/get-your-local-ip-address/
|
202
132
|
# NOTE: This <em>does not</em> open a connection to the IP address.
|
203
|
-
def
|
133
|
+
def find_ipaddress_internal
|
204
134
|
# turn off reverse DNS resolution temporarily
|
205
135
|
orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true
|
206
|
-
UDPSocket.open {|s| s.connect('
|
136
|
+
UDPSocket.open {|s| s.connect('65.74.177.129', 1); s.addr.last } # GitHub IP
|
207
137
|
ensure
|
208
138
|
Socket.do_not_reverse_lookup = orig
|
209
139
|
end
|
210
|
-
|
211
|
-
#
|
212
|
-
#
|
213
|
-
# According to coderrr (see comments on blog link above), this implementation
|
214
|
-
# doesn't guarantee that it will return the address for the interface external
|
215
|
-
# traffic goes through. It's also possible the hostname isn't resolvable to the
|
216
|
-
# local IP.
|
217
|
-
def local_ip_address_alt
|
218
|
-
ipaddr = :unknown
|
219
|
-
begin
|
220
|
-
saddr = Socket.getaddrinfo( Socket.gethostname, nil, Socket::AF_UNSPEC, Socket::SOCK_STREAM, nil, Socket::AI_CANONNAME)
|
221
|
-
ipaddr = saddr.select{|type| type[0] == 'AF_INET' }[0][3]
|
222
|
-
rescue => ex
|
223
|
-
end
|
224
|
-
ipaddr
|
225
|
-
end
|
226
|
-
|
227
|
-
# returns a symbol in the form: os_implementation. This is used throughout Stella
|
228
|
-
# for platform specific support.
|
140
|
+
|
141
|
+
# Returns a Symbol of the short platform descriptor in the format: VM-OS
|
142
|
+
# e.g. <tt>:java-unix</tt>
|
229
143
|
def platform
|
230
|
-
"#{@
|
144
|
+
"#{@vm}-#{@os}".to_sym
|
231
145
|
end
|
232
146
|
|
233
|
-
# Returns
|
234
|
-
|
235
|
-
|
147
|
+
# Returns a String of the full platform descriptor in the format: VM-OS-IMPL-ARCH
|
148
|
+
# e.g. <tt>java-unix-osx-x86_64</tt>
|
149
|
+
def to_s(*args)
|
150
|
+
"#{@vm}-#{@os}-#{@impl}-#{@arch}".to_sym
|
236
151
|
end
|
237
152
|
|
238
|
-
|
239
|
-
def
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
153
|
+
# Returns Ruby version as an Array of Integers. e.g. [1,9,1]
|
154
|
+
def ruby; RUBY_VERSION.split('.').collect { |v| v.to_i }; end
|
155
|
+
# Return the name of the current user
|
156
|
+
def user; ENV['USER']; end
|
157
|
+
# Returns the environment paths as an Array
|
158
|
+
def paths; execute_platform_specific(:paths); end
|
159
|
+
# Returns the path to the current user's home directory
|
160
|
+
def home; execute_platform_specific(:home); end
|
161
|
+
# Returns the name of the current shell
|
162
|
+
def shell; execute_platform_specific(:shell); end
|
163
|
+
# Returns the path to the current temp directory
|
164
|
+
def tmpdir; execute_platform_specific(:tmpdir); end
|
165
|
+
|
166
|
+
private
|
167
|
+
|
168
|
+
# Look for and execute a platform specific method.
|
169
|
+
# The name of the method will be in the format: +dtype-VM-OS-IMPL+.
|
170
|
+
# e.g. find_uptime_ruby_unix_osx
|
171
|
+
#
|
172
|
+
def execute_platform_specific(dtype)
|
173
|
+
criteria = [@vm, @os, @impl]
|
174
|
+
while !criteria.empty?
|
175
|
+
meth = [dtype, criteria].join('_').to_sym
|
176
|
+
return self.send(meth) if SysInfo.private_method_defined?(meth)
|
177
|
+
criteria.pop
|
249
178
|
end
|
179
|
+
raise "#{dtype}_#{@vm}_#{@os}_#{@impl} not implemented"
|
250
180
|
end
|
251
181
|
|
252
|
-
def
|
253
|
-
|
182
|
+
def paths_ruby_unix; (ENV['PATH'] || '').split(':'); end
|
183
|
+
def paths_ruby_win32; (ENV['PATH'] || '').split(';'); end # Not tested!
|
184
|
+
def paths_java
|
185
|
+
delim = @impl == :windows ? ';' : ':'
|
186
|
+
(ENV['PATH'] || '').split(delim)
|
254
187
|
end
|
255
188
|
|
256
|
-
def
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
elsif @os == :java
|
262
|
-
if @impl == :windows
|
263
|
-
File.expand_path(ENV['USERPROFILE'])
|
264
|
-
else
|
265
|
-
File.expand_path(ENV['HOME'])
|
266
|
-
end
|
267
|
-
else
|
268
|
-
raise "paths not implemented for: #{@os}"
|
269
|
-
end
|
189
|
+
def tmpdir_ruby_unix; (ENV['TMPDIR'] || '/tmp'); end
|
190
|
+
def tmpdir_ruby_win32; (ENV['TMPDIR'] || 'C:\\temp'); end
|
191
|
+
def tmpdir_java
|
192
|
+
default = @impl == :windows ? 'C:\\temp' : '/tmp'
|
193
|
+
(ENV['TMPDIR'] || default)
|
270
194
|
end
|
271
195
|
|
272
|
-
def
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
196
|
+
def shell_ruby_unix; (ENV['SHELL'] || 'bash').to_sym; end
|
197
|
+
def shell_ruby_win32; :dos; end
|
198
|
+
alias_method :shell_java_unix, :shell_ruby_unix
|
199
|
+
alias_method :shell_java_win32, :shell_ruby_win32
|
200
|
+
|
201
|
+
def home_ruby_unix; File.expand_path(ENV['HOME']); end
|
202
|
+
def home_ruby_win32; File.expand_path(ENV['USERPROFILE']); end
|
203
|
+
def home_java
|
204
|
+
if @impl == :windows
|
205
|
+
File.expand_path(ENV['USERPROFILE'])
|
277
206
|
else
|
278
|
-
|
207
|
+
File.expand_path(ENV['HOME'])
|
279
208
|
end
|
280
209
|
end
|
281
210
|
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
211
|
+
# Ya, this is kinda wack. Ruby -> Java -> Kernel32. See:
|
212
|
+
# http://www.oreillynet.com/ruby/blog/2008/01/jruby_meets_the_windows_api_1.html
|
213
|
+
# http://msdn.microsoft.com/en-us/library/ms724408(VS.85).aspx
|
214
|
+
# Ruby 1.9.1: Win32API is now deprecated in favor of using the DL library.
|
215
|
+
def find_uptime_java_win32_windows
|
216
|
+
kernel32 = com.sun.jna.NativeLibrary.getInstance('kernel32')
|
217
|
+
buf = java.nio.ByteBuffer.allocate(256)
|
218
|
+
(kernel32.getFunction('GetTickCount').invokeInt([256, buf].to_java).to_f / 1000).to_f
|
219
|
+
end
|
220
|
+
def find_uptime_ruby_win32_windows
|
221
|
+
# Win32API is required in self.guess
|
222
|
+
getTickCount = Win32API.new("kernel32", "GetTickCount", nil, 'L')
|
223
|
+
((getTickCount.call()).to_f / 1000).to_f
|
224
|
+
end
|
225
|
+
def find_uptime_ruby_unix_osx
|
226
|
+
# This is faster than "who" and could work on BSD also.
|
227
|
+
(Time.now.to_f - Time.at(`sysctl -b kern.boottime 2>/dev/null`.unpack('L').first).to_f).to_f
|
293
228
|
end
|
294
229
|
|
295
|
-
#
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
230
|
+
# This should work for most unix flavours.
|
231
|
+
def find_uptime_ruby_unix
|
232
|
+
# who is sloooooow. Use File.read('/proc/uptime')
|
233
|
+
(Time.now.to_i - Time.parse(`who -b 2>/dev/null`).to_f)
|
234
|
+
end
|
235
|
+
alias_method :find_uptime_java_unix_osx, :find_uptime_ruby_unix
|
300
236
|
|
237
|
+
# Determine the values for vm, os, impl, and arch when running on Java.
|
238
|
+
def guess_java
|
239
|
+
vm, os, impl, arch = :java, :unknown, :unknown, :unknown
|
240
|
+
require 'java'
|
241
|
+
include_class java.lang.System
|
242
|
+
|
243
|
+
osname = System.getProperty("os.name")
|
244
|
+
IMPLEMENTATIONS.each do |r, o, i|
|
245
|
+
next unless osname =~ r
|
246
|
+
os, impl = [o, i]
|
247
|
+
break
|
248
|
+
end
|
249
|
+
|
250
|
+
osarch = System.getProperty("os.arch")
|
251
|
+
ARCHITECTURES.each do |r, a|
|
252
|
+
next unless osarch =~ r
|
253
|
+
arch = a
|
254
|
+
break
|
255
|
+
end
|
256
|
+
[vm, os, impl, arch]
|
257
|
+
end
|
301
258
|
|
259
|
+
# Returns the local IP address based on the hostname.
|
260
|
+
# According to coderrr (see comments on blog link above), this implementation
|
261
|
+
# doesn't guarantee that it will return the address for the interface external
|
262
|
+
# traffic goes through. It's also possible the hostname isn't resolvable to the
|
263
|
+
# local IP.
|
264
|
+
#
|
265
|
+
# NOTE: This code predates the current ip_address_internal. It was just as well
|
266
|
+
# but the other code is cleaner. I'm keeping this old version here for now.
|
267
|
+
def ip_address_internal_alt
|
268
|
+
ipaddr = :unknown
|
269
|
+
begin
|
270
|
+
saddr = Socket.getaddrinfo( Socket.gethostname, nil, Socket::AF_UNSPEC, Socket::SOCK_STREAM, nil, Socket::AI_CANONNAME)
|
271
|
+
ipaddr = saddr.select{|type| type[0] == 'AF_INET' }[0][3]
|
272
|
+
rescue => ex
|
273
|
+
end
|
274
|
+
ipaddr
|
275
|
+
end
|
302
276
|
end
|
303
277
|
|
304
278
|
|
data/sysinfo.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
@spec = Gem::Specification.new do |s|
|
2
2
|
s.name = "sysinfo"
|
3
3
|
s.rubyforge_project = "sysinfo"
|
4
|
-
s.version = "0.5.
|
4
|
+
s.version = "0.5.1"
|
5
5
|
s.summary = "SysInfo: All your system-independent infoz in one handy class. "
|
6
6
|
s.description = s.summary
|
7
7
|
s.author = "Delano Mandelbaum"
|
@@ -12,12 +12,11 @@
|
|
12
12
|
# = EXECUTABLES =
|
13
13
|
# The list of executables in your project (if any). Don't include the path,
|
14
14
|
# just the base filename.
|
15
|
-
s.executables = %w[]
|
15
|
+
s.executables = %w[sysinfo]
|
16
16
|
|
17
17
|
# = DEPENDENCIES =
|
18
18
|
# Add all gem dependencies
|
19
|
-
|
20
|
-
#s.add_dependency '', '>= 0.0'
|
19
|
+
s.add_dependency 'storable', '>= 0.5.1'
|
21
20
|
|
22
21
|
# = MANIFEST =
|
23
22
|
# The complete list of files to be included in the release. When GitHub packages your gem,
|
@@ -30,6 +29,7 @@
|
|
30
29
|
LICENSE.txt
|
31
30
|
README.rdoc
|
32
31
|
Rakefile
|
32
|
+
bin/sysinfo
|
33
33
|
lib/sysinfo.rb
|
34
34
|
sysinfo.gemspec
|
35
35
|
)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sysinfo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Delano Mandelbaum
|
@@ -12,6 +12,16 @@ cert_chain: []
|
|
12
12
|
date: 2009-05-07 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: storable
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.5.1
|
24
|
+
version:
|
15
25
|
- !ruby/object:Gem::Dependency
|
16
26
|
name: RedCloth
|
17
27
|
type: :runtime
|
@@ -24,8 +34,8 @@ dependencies:
|
|
24
34
|
version:
|
25
35
|
description: "SysInfo: All your system-independent infoz in one handy class."
|
26
36
|
email: delano@solutious.com
|
27
|
-
executables:
|
28
|
-
|
37
|
+
executables:
|
38
|
+
- sysinfo
|
29
39
|
extensions: []
|
30
40
|
|
31
41
|
extra_rdoc_files:
|
@@ -36,6 +46,7 @@ files:
|
|
36
46
|
- LICENSE.txt
|
37
47
|
- README.rdoc
|
38
48
|
- Rakefile
|
49
|
+
- bin/sysinfo
|
39
50
|
- lib/sysinfo.rb
|
40
51
|
- sysinfo.gemspec
|
41
52
|
has_rdoc: true
|