sysinfo 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. data/CHANGES.txt +8 -0
  2. data/LICENSE.txt +19 -0
  3. data/README.rdoc +23 -0
  4. data/Rakefile +104 -0
  5. data/lib/sysinfo.rb +307 -0
  6. data/sysinfo.gemspec +56 -0
  7. metadata +74 -0
@@ -0,0 +1,8 @@
1
+ SYSINFO, CHANGES
2
+
3
+
4
+ #### 0.5 (2009-05-07) ###############################
5
+
6
+ * First public release. See commit history for solutious-stella, solutious-rudy,
7
+ and delano-rye for complete history of SysInfo (was SystemInfo).
8
+
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2009 Delano Mandelbaum, Solutious Inc
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,23 @@
1
+ = SysInfo - v0.5
2
+
3
+ All your system-independent infoz in one handy class.
4
+
5
+ == Installation
6
+
7
+ Via Rubygems, one of:
8
+
9
+ $ sudo gem install sysinfo
10
+ $ sudo gem install delano-sysinfo --source http://gems.github.com/
11
+
12
+ or via download:
13
+ * sysinfo-latest.tar.gz[http://github.com/delano/sysinfo/tarball/latest]
14
+ * sysinfo-latest.zip[http://github.com/delano/sysinfo/zipball/latest]
15
+
16
+
17
+ == Credits
18
+
19
+ * Delano Mandelbaum (delano@solutious.com)
20
+
21
+ == License
22
+
23
+ See: LICENSE.txt
@@ -0,0 +1,104 @@
1
+ require 'rubygems'
2
+ require 'rake/clean'
3
+ require 'rake/gempackagetask'
4
+ require 'hanna/rdoctask'
5
+ require 'fileutils'
6
+ include FileUtils
7
+
8
+ task :default => :package
9
+
10
+ # CONFIG =============================================================
11
+
12
+ # Change the following according to your needs
13
+ README = "README.rdoc"
14
+ CHANGES = "CHANGES.txt"
15
+ LICENSE = "LICENSE.txt"
16
+
17
+ # Files and directories to be deleted when you run "rake clean"
18
+ CLEAN.include [ 'pkg', '*.gem', '.config']
19
+
20
+ # Virginia assumes your project and gemspec have the same name
21
+ name = (Dir.glob('*.gemspec') || ['virginia']).first.split('.').first
22
+ load "#{name}.gemspec"
23
+ version = @spec.version
24
+
25
+ # That's it! The following defaults should allow you to get started
26
+ # on other things.
27
+
28
+
29
+ # TESTS/SPECS =========================================================
30
+
31
+
32
+
33
+ # INSTALL =============================================================
34
+
35
+ Rake::GemPackageTask.new(@spec) do |p|
36
+ p.need_tar = true if RUBY_PLATFORM !~ /mswin/
37
+ end
38
+
39
+ task :release => [ :rdoc, :package ]
40
+ task :install => [ :rdoc, :package ] do
41
+ sh %{sudo gem install pkg/#{name}-#{version}.gem}
42
+ end
43
+ task :uninstall => [ :clean ] do
44
+ sh %{sudo gem uninstall #{name}}
45
+ end
46
+
47
+
48
+ # RUBYFORGE RELEASE / PUBLISH TASKS ==================================
49
+
50
+ if @spec.rubyforge_project
51
+ desc 'Publish website to rubyforge'
52
+ task 'publish:rdoc' => 'doc/index.html' do
53
+ sh "scp -rp doc/* rubyforge.org:/var/www/gforge-projects/#{name}/"
54
+ end
55
+
56
+ desc 'Public release to rubyforge'
57
+ task 'publish:gem' => [:package] do |t|
58
+ sh <<-end
59
+ rubyforge add_release -o Any -a #{CHANGES} -f -n #{README} #{name} #{name} #{@spec.version} pkg/#{name}-#{@spec.version}.gem &&
60
+ rubyforge add_file -o Any -a #{CHANGES} -f -n #{README} #{name} #{name} #{@spec.version} pkg/#{name}-#{@spec.version}.tgz
61
+ end
62
+ end
63
+ end
64
+
65
+
66
+
67
+ # RUBY DOCS TASK ==================================
68
+
69
+ Rake::RDocTask.new do |t|
70
+ t.rdoc_dir = 'doc'
71
+ t.title = @spec.summary
72
+ t.options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object'
73
+ t.options << '--charset' << 'utf-8'
74
+ t.rdoc_files.include(LICENSE)
75
+ t.rdoc_files.include(README)
76
+ t.rdoc_files.include(CHANGES)
77
+ #t.rdoc_files.include('bin/*')
78
+ t.rdoc_files.include('lib/**/*.rb')
79
+ end
80
+
81
+
82
+
83
+
84
+ #Hoe.new('rspec', Spec::VERSION::STRING) do |p|
85
+ # p.summary = Spec::VERSION::SUMMARY
86
+ # p.description = "Behaviour Driven Development for Ruby."
87
+ # p.rubyforge_name = 'rspec'
88
+ # p.developer('RSpec Development Team', 'rspec-devel@rubyforge.org')
89
+ # p.extra_dev_deps = [["cucumber",">= 0.1.13"]]
90
+ # p.remote_rdoc_dir = "rspec/#{Spec::VERSION::STRING}"
91
+ # p.rspec_options = ['--options', 'spec/spec.opts']
92
+ # p.history_file = 'History.rdoc'
93
+ # p.readme_file = 'README.rdoc'
94
+ # p.post_install_message = <<-POST_INSTALL_MESSAGE
95
+ ##{'*'*50}
96
+ #
97
+ # Thank you for installing rspec-#{Spec::VERSION::STRING}
98
+ #
99
+ # Please be sure to read History.rdoc and Upgrade.rdoc
100
+ # for useful information about this release.
101
+ #
102
+ #{'*'*50}
103
+ #POST_INSTALL_MESSAGE
104
+ #end
@@ -0,0 +1,307 @@
1
+ require 'socket'
2
+
3
+ # = SysInfo
4
+ #
5
+ # A container for the platform specific system information.
6
+ # Portions of this code were originally from Amazon's EC2 AMI tools,
7
+ # specifically lib/platform.rb.
8
+ class SysInfo < Storable
9
+ unless defined?(IMPLEMENTATIONS)
10
+ VERSION = 5.freeze
11
+ IMPLEMENTATIONS = [
12
+
13
+ # These are for JRuby, System.getproperty('os.name').
14
+ # For a list of all values, see: http://lopica.sourceforge.net/os.html
15
+ #regexp matcher os implementation
16
+ [/mac\s*os\s*x/i, :unix, :osx ],
17
+ [/sunos/i, :unix, :solaris ],
18
+ [/windows\s*ce/i, :win32, :windows ],
19
+ [/windows/i, :win32, :windows ],
20
+ [/osx/i, :unix, :osx ],
21
+
22
+ # TODO: implement other windows matches: # /djgpp|(cyg|ms|bcc)win|mingw/ (from mongrel)
23
+
24
+ # These are for RUBY_PLATFORM and JRuby
25
+ [/java/i, :java, :java ],
26
+ [/darwin/i, :unix, :osx ],
27
+ [/linux/i, :unix, :linux ],
28
+ [/freebsd/i, :unix, :freebsd ],
29
+ [/netbsd/i, :unix, :netbsd ],
30
+ [/solaris/i, :unix, :solaris ],
31
+ [/irix/i, :unix, :irix ],
32
+ [/cygwin/i, :unix, :cygwin ],
33
+ [/mswin/i, :win32, :windows ],
34
+ [/mingw/i, :win32, :mingw ],
35
+ [/bccwin/i, :win32, :bccwin ],
36
+ [/wince/i, :win32, :wince ],
37
+ [/vms/i, :vms, :vms ],
38
+ [/os2/i, :os2, :os2 ],
39
+ [nil, :unknown, :unknown ],
40
+ ].freeze
41
+
42
+ ARCHITECTURES = [
43
+ [/(i\d86)/i, :i386 ],
44
+ [/x86_64/i, :x86_64 ],
45
+ [/x86/i, :i386 ], # JRuby
46
+ [/ia64/i, :ia64 ],
47
+ [/alpha/i, :alpha ],
48
+ [/sparc/i, :sparc ],
49
+ [/mips/i, :mips ],
50
+ [/powerpc/i, :powerpc ],
51
+ [/universal/i,:universal ],
52
+ [nil, :unknown ],
53
+ ].freeze
54
+ end
55
+
56
+
57
+ field :os => String
58
+ field :implementation => String
59
+ field :architecture => String
60
+ field :hostname => String
61
+ field :ipaddress => String
62
+ field :ipaddress_external => String
63
+ field :uptime => Float
64
+
65
+
66
+ alias :impl :implementation
67
+ alias :arch :architecture
68
+
69
+
70
+ def initialize
71
+ @os, @implementation, @architecture = guess
72
+ @hostname, @ipaddress, @uptime = get_info
73
+ end
74
+
75
+
76
+ # This is called at require-time. It guesses the
77
+ # current operating system, implementation, architecture.
78
+ # Returns [os, impl, arch]
79
+ def guess
80
+ os = :unknown
81
+ impl = :unknown
82
+ arch = :unknown
83
+ IMPLEMENTATIONS.each do |r, o, i|
84
+ if r and RUBY_PLATFORM =~ r
85
+ os, impl = [o, i]
86
+ break
87
+ end
88
+ end
89
+ ARCHITECTURES.each do |r, a|
90
+ if r and RUBY_PLATFORM =~ r
91
+ arch = a
92
+ break
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
+
123
+ end
124
+
125
+ [os, impl, arch]
126
+ end
127
+
128
+ # Returns [hostname, ipaddr, uptime] for the local machine
129
+ def get_info
130
+ hostname = :unknown
131
+ ipaddr = :unknown
132
+ uptime = :unknown
133
+
134
+ begin
135
+ hostname = local_hostname
136
+ ipaddr = local_ip_address
137
+ uptime = local_uptime
138
+ rescue => ex
139
+ # Be silent!
140
+ end
141
+
142
+ [hostname, ipaddr, uptime]
143
+ end
144
+
145
+ # Return the hostname for the local machine
146
+ def local_hostname
147
+ Socket.gethostname
148
+ end
149
+
150
+
151
+ # Returns the local uptime in hours. Use Win32API in Windows,
152
+ # 'sysctl -b kern.boottime' os osx, and 'who -b' on unix.
153
+ # Based on Ruby Quiz solutions by: Matthias Reitinger
154
+ # On Windows, see also: net statistics server
155
+ def local_uptime
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
+
187
+ hours = 0
188
+
189
+ begin
190
+ key = platform
191
+ method = (methods.has_key? key) ? methods[key] : methods[:unix]
192
+ hours = (method.call) / 3600 # seconds to hours
193
+ rescue => ex
194
+ end
195
+ hours
196
+ end
197
+
198
+
199
+ #
200
+ # Return the local IP address which receives external traffic
201
+ # from: http://coderrr.wordpress.com/2008/05/28/get-your-local-ip-address/
202
+ # NOTE: This <em>does not</em> open a connection to the IP address.
203
+ def local_ip_address
204
+ # turn off reverse DNS resolution temporarily
205
+ orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true
206
+ UDPSocket.open {|s| s.connect('75.101.137.7', 1); s.addr.last } # Solutious IP
207
+ ensure
208
+ Socket.do_not_reverse_lookup = orig
209
+ end
210
+
211
+ #
212
+ # Returns the local IP address based on the hostname.
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.
229
+ def platform
230
+ "#{@os}_#{@implementation}".to_sym
231
+ end
232
+
233
+ # Returns Ruby version as an array
234
+ def ruby
235
+ RUBY_VERSION.split('.').map { |v| v.to_i }
236
+ end
237
+
238
+ # Returns the environment PATH as an Array
239
+ def paths
240
+ if @os == :unix
241
+ (ENV['PATH'] || '').split(':')
242
+ elsif @os == :win32
243
+ (ENV['PATH'] || '').split(';') # Not tested!
244
+ elsif @os == :java
245
+ delim = @impl == :windows ? ';' : ':'
246
+ (ENV['PATH'] || '').split(delim)
247
+ else
248
+ raise "paths not implemented for: #{@os}"
249
+ end
250
+ end
251
+
252
+ def user
253
+ ENV['USER']
254
+ end
255
+
256
+ def home
257
+ if @os == :unix
258
+ File.expand_path(ENV['HOME'])
259
+ elsif @os == :win32
260
+ File.expand_path(ENV['USERPROFILE'])
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
270
+ end
271
+
272
+ def shell
273
+ if @os == :unix
274
+ (ENV['SHELL'] || 'bash').to_sym
275
+ elsif @os == :win32
276
+ :dos
277
+ else
278
+ raise "paths not implemented for: #{@os}"
279
+ end
280
+ end
281
+
282
+ def tmpdir
283
+ if @os == :unix
284
+ (ENV['TMPDIR'] || '/tmp')
285
+ elsif @os == :win32
286
+ (ENV['TMPDIR'] || 'C:\\temp')
287
+ elsif @os == :java
288
+ default = @impl == :windows ? 'C:\\temp' : '/tmp'
289
+ (ENV['TMPDIR'] || default)
290
+ else
291
+ raise "paths not implemented for: #{@os}"
292
+ end
293
+ end
294
+
295
+ # Print friendly system information.
296
+ ##def to_s
297
+ ## sprintf("Hostname: %s#{$/}IP Address: %s#{$/}System: %s#{$/}Uptime: %.2f (hours)#{$/}Ruby: #{ruby.join('.')}",
298
+ ## @hostname, @ipaddress, "#{@os}-#{@implementation}-#{@architecture}", @uptime)
299
+ ##end
300
+
301
+
302
+ end
303
+
304
+
305
+ if $0 == __FILE__
306
+ puts SysInfo.new.to_yaml
307
+ end
@@ -0,0 +1,56 @@
1
+ @spec = Gem::Specification.new do |s|
2
+ s.name = "sysinfo"
3
+ s.rubyforge_project = "sysinfo"
4
+ s.version = "0.5.0"
5
+ s.summary = "SysInfo: All your system-independent infoz in one handy class. "
6
+ s.description = s.summary
7
+ s.author = "Delano Mandelbaum"
8
+ s.email = "delano@solutious.com"
9
+ s.homepage = "http://solutious.com/"
10
+
11
+
12
+ # = EXECUTABLES =
13
+ # The list of executables in your project (if any). Don't include the path,
14
+ # just the base filename.
15
+ s.executables = %w[]
16
+
17
+ # = DEPENDENCIES =
18
+ # Add all gem dependencies
19
+ #s.add_dependency ''
20
+ #s.add_dependency '', '>= 0.0'
21
+
22
+ # = MANIFEST =
23
+ # The complete list of files to be included in the release. When GitHub packages your gem,
24
+ # it doesn't allow you to run any command that accesses the filesystem. You will get an
25
+ # error. You can ask your VCS for the list of versioned files:
26
+ # git ls-files
27
+ # svn list -R
28
+ s.files = %w(
29
+ CHANGES.txt
30
+ LICENSE.txt
31
+ README.rdoc
32
+ Rakefile
33
+ lib/sysinfo.rb
34
+ sysinfo.gemspec
35
+ )
36
+
37
+ s.extra_rdoc_files = %w[README.rdoc LICENSE.txt]
38
+ s.has_rdoc = true
39
+ s.rdoc_options = ["--line-numbers", "--title", s.summary, "--main", "README.rdoc"]
40
+ s.require_paths = %w[lib]
41
+ s.rubygems_version = '1.3.0'
42
+
43
+ if s.respond_to? :specification_version then
44
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
45
+ s.specification_version = 2
46
+
47
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
48
+ s.add_runtime_dependency(%q<RedCloth>, [">= 4.0.4"])
49
+ else
50
+ s.add_dependency(%q<RedCloth>, [">= 4.0.4"])
51
+ end
52
+ else
53
+ s.add_dependency(%q<RedCloth>, [">= 4.0.4"])
54
+ end
55
+
56
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sysinfo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Delano Mandelbaum
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-07 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: RedCloth
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 4.0.4
24
+ version:
25
+ description: "SysInfo: All your system-independent infoz in one handy class."
26
+ email: delano@solutious.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.rdoc
33
+ - LICENSE.txt
34
+ files:
35
+ - CHANGES.txt
36
+ - LICENSE.txt
37
+ - README.rdoc
38
+ - Rakefile
39
+ - lib/sysinfo.rb
40
+ - sysinfo.gemspec
41
+ has_rdoc: true
42
+ homepage: http://solutious.com/
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --line-numbers
48
+ - --title
49
+ - "SysInfo: All your system-independent infoz in one handy class."
50
+ - --main
51
+ - README.rdoc
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project: sysinfo
69
+ rubygems_version: 1.3.2
70
+ signing_key:
71
+ specification_version: 2
72
+ summary: "SysInfo: All your system-independent infoz in one handy class."
73
+ test_files: []
74
+