systeminformation 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Stefan Maier
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,24 @@
1
+ = SystemInformation
2
+
3
+ This gem provides information on the system it is running on.
4
+
5
+
6
+ == Usage
7
+ See SystemInformation.
8
+
9
+ == Limitations
10
+ At the moment only implementations for Linux exits. Implementations for other operating system are appreciated.
11
+
12
+ == Note on Patches/Pull Requests
13
+
14
+ * Fork the project.
15
+ * Make your feature addition or bug fix.
16
+ * Add tests for it. This is important so I don't break it in a
17
+ future version unintentionally.
18
+ * Commit, do not mess with rakefile, version, or history.
19
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
20
+ * Send me a pull request. Bonus points for topic branches.
21
+
22
+ == Copyright
23
+
24
+ Copyright (c) 2010 Stefan Maier. See LICENSE for details.
@@ -0,0 +1,52 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "systeminformation"
8
+ gem.summary = %Q{Provides information about the system}
9
+ gem.description = %Q{Provides information about cpu, memory, network throughput, current load and usercount}
10
+ gem.email = "stefanmaier@gmail.com"
11
+ gem.homepage = "http://github.com/tliff/systeminformation"
12
+ gem.authors = ["Stefan Maier"]
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/test_*.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
47
+
48
+ rdoc.rdoc_dir = 'rdoc'
49
+ rdoc.title = "systeminformation #{version}"
50
+ rdoc.rdoc_files.include('README*')
51
+ rdoc.rdoc_files.include('lib/**/*.rb')
52
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,108 @@
1
+ require 'lib/systeminformation/os.rb'
2
+
3
+ module SystemInformation
4
+ #Returns what operating system is running.
5
+ #Valid return values are :linux, :solaris, :bsd, :darwin, :windows and :unknown
6
+ def self.os
7
+ OS::os
8
+ end
9
+
10
+ # Returns a hash of
11
+ # {
12
+ # "cpuid" => {
13
+ # :idle => 1.0,
14
+ # :user => 0.0,
15
+ # ...
16
+ # },
17
+ # ...
18
+ # }
19
+ #
20
+ # The numerical values for each cpu sum up to 1.
21
+ #
22
+ # _Note:_ Since cpu utilisation is only defined for a time interval, the
23
+ # first call to this methods does not return meaningful data. It should be
24
+ # discarded and after a short time reexecuted. Subsequent calls will use
25
+ # the previous call as reference.
26
+ def self.cpu
27
+ if OS::linux?
28
+ @@cpu ||= Linux::CPU.new
29
+ @@cpu.utilization
30
+ end
31
+ end
32
+
33
+
34
+ # Returns a hash of
35
+ # {
36
+ # :load_1 =>0.0,
37
+ # :load_5 =>0.0,
38
+ # :load_15 =>0.0,
39
+ # :procs_running =>0.0,
40
+ # :procs_total =>0.0
41
+ # }
42
+ #
43
+ def self.load
44
+ if OS::linux?
45
+ Linux::Load.load
46
+ end
47
+ end
48
+
49
+ # Returns a hash of
50
+ # {
51
+ # "eth0" => {
52
+ # :rbytes => 0.0,
53
+ # :rpackets=>0.0,
54
+ # :sbytes=>0.0,
55
+ # :spackets=>0.0
56
+ # },
57
+ # ...
58
+ # }
59
+ #
60
+ # values are bytes/second or packets/second respectively
61
+ #
62
+ # _Note:_ Since network throughput is only defined for a time interval, the
63
+ # first call to this methods does not return meaningful data. It should be
64
+ # discarded and after a short time reexecuted. Subsequent calls will use
65
+ # the previous call as reference.
66
+ def self.net
67
+ if OS::linux?
68
+ @@net ||= Linux::Net.new
69
+ @@net.throughput
70
+ end
71
+ end
72
+ # Returns a hash of
73
+ # items = {
74
+ # :free=>23340,
75
+ # :cached=>106052,
76
+ # :buffers=>10864,
77
+ # :total=>505492,
78
+ # :swaptotal=>916472,
79
+ # :swapfree=>782652,
80
+ # :used=>365236,
81
+ # :swapused=>133820
82
+ # }
83
+ #
84
+ # The unit is kilobyte blocks.
85
+ def self.memory
86
+ if OS::linux?
87
+ Linux::Memory.usage
88
+ end
89
+ end
90
+
91
+ # Returns the number of currently logged in users as an integer
92
+ def self.users
93
+ if OS::unix?
94
+ Unix::Users.loggedin
95
+ end
96
+ end
97
+
98
+ end
99
+
100
+ os = [:unix, :linux]
101
+ os.each do |o|
102
+ if SystemInformation::OS::os == o
103
+ Dir.glob(File.dirname(__FILE__) + "/systeminformation/#{o.to_s}/*.rb") do |i|
104
+ require i
105
+ end
106
+ end
107
+ end
108
+
@@ -0,0 +1,47 @@
1
+ module SystemInformation
2
+ module Linux
3
+ class CPU
4
+ def initialize
5
+ @prev_data = read_data
6
+ end
7
+
8
+ def utilization
9
+ new_data = read_data
10
+ returnhash = {}
11
+ new_data.keys.each do |key|
12
+ difference = @prev_data[key].zip(new_data[key]).map{|i| i[1].to_f - i[0].to_f}
13
+ difference
14
+ sum = difference.inject{|a,b| a+b}
15
+
16
+ difference.map!{|i| i/sum}.map!{|i| i.nan? ? 0 : i}
17
+ returnhash[key] = {
18
+ :user => difference[0],
19
+ :nice => difference[1],
20
+ :system => difference[2],
21
+ :idle => difference[3],
22
+ :running => difference[4],
23
+ :iowait => difference[5],
24
+ :irq => difference[6],
25
+ :softirq => difference[7]
26
+ }
27
+ end
28
+ @prev_data = new_data
29
+ return returnhash
30
+ end
31
+
32
+ private
33
+ def read_data
34
+ data = {}
35
+ File.open('/proc/stat','r') do |f|
36
+ f.readlines.select{|l| l =~ /^(cpu\d*) /}.each do |l|
37
+ cpu, *rest = l.split
38
+ data[cpu] = rest
39
+ end
40
+ end
41
+ data
42
+ end
43
+
44
+
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,15 @@
1
+ module SystemInformation
2
+ module Linux
3
+ module Load
4
+ def self.load
5
+ data = {}
6
+ File.open('/proc/loadavg','r') do |f|
7
+ line = f.readline
8
+ data[:load_1], data[:load_5], data[:load_15], processes = line.split
9
+ data[:procs_runnning], data[:procs_total] = processes.split('/')
10
+ end
11
+ return data
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,29 @@
1
+ module SystemInformation
2
+ module Linux
3
+ module Memory
4
+ def self.usage
5
+ items = {
6
+ :free => 'MemFree',
7
+ :cached => 'Cached',
8
+ :buffers => 'Buffers',
9
+ :total => 'MemTotal',
10
+ :swaptotal => 'SwapTotal',
11
+ :swapfree => 'SwapFree'
12
+ }
13
+
14
+ File.open('/proc/meminfo','r') do |f|
15
+ f.readlines.each do |l|
16
+ items.each_pair do |i, k|
17
+ if matches = Regexp.new("#{k}: +([0-9]+)").match(l)
18
+ items[i] = matches[1].to_i
19
+ end
20
+ end
21
+ end
22
+ items[:used] = items[:total] - items[:free] - items[:cached] - items[:buffers]
23
+ items[:swapused] = items[:swaptotal] - items[:swapfree]
24
+ end
25
+ return items
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,41 @@
1
+ module SystemInformation
2
+ module Linux
3
+ class Net
4
+ def initialize
5
+ @prev_data = get_data
6
+ @prev_time = Time.now
7
+ end
8
+
9
+ def throughput
10
+ new_data = get_data
11
+ difference ={}
12
+ delta = Time.now - @prev_time
13
+ @prev_data.keys.each{|k|
14
+ difference[k] = @prev_data[k].zip(new_data[k]).map{|i| (i[1].to_f - i[0].to_f)/delta}
15
+ }
16
+ @prev_data = new_data
17
+ @prev_time = Time.now
18
+ result={}
19
+ difference.each{|k,body|
20
+ result[k] = {:rbytes => body[0], :rpackets => body[1], :sbytes => body[2], :spackets => body[3]}
21
+ }
22
+ result
23
+ end
24
+
25
+ private
26
+ def get_data
27
+ delta = nil
28
+ data = {}
29
+ File.open('/proc/net/dev','r') do |f|
30
+ f.readlines.select{|l| l =~ /:/}.each do |l|
31
+ l.gsub!(/ +/,' ').strip!
32
+ id, body = l.split(':')
33
+ body = body.strip.split
34
+ data[id] = [body[0],body[1],body[8],body[9]]
35
+ end
36
+ end
37
+ data
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,37 @@
1
+ if /java/.match(RUBY_PLATFORM)
2
+ require 'java'
3
+ import 'java.lang.System'
4
+ end
5
+
6
+
7
+ module SystemInformation
8
+ module OS
9
+ def self.os
10
+ return @os if @os
11
+ name = RUBY_PLATFORM
12
+ @os = case name
13
+ when /linux/
14
+ :linux
15
+ when /solaris/
16
+ :solaris
17
+ when /bsd/
18
+ :bsd
19
+ when /darwin/
20
+ :darwin
21
+ when /win/
22
+ :windows
23
+ else
24
+ :unknown
25
+ end
26
+ end
27
+
28
+ def self.unix?
29
+ [:linux, :bsd, :darwin, :solaris].member?(os)
30
+ end
31
+
32
+ def self.linux?
33
+ os == :linux
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,13 @@
1
+ module SystemInformation
2
+ module Linux
3
+ module Users
4
+ def self.loggedin
5
+ count = 0
6
+ IO.popen('who') do |p|
7
+ count = p.readlines.size
8
+ end
9
+ return count
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,51 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{systeminformation}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Stefan Maier"]
12
+ s.date = %q{2010-06-07}
13
+ s.description = %q{Provides information about cpu, memory, network throughput, current load and usercount}
14
+ s.email = %q{stefanmaier@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/systeminformation.rb",
27
+ "lib/systeminformation/linux/cpu.rb",
28
+ "lib/systeminformation/linux/load.rb",
29
+ "lib/systeminformation/linux/memory.rb",
30
+ "lib/systeminformation/linux/net.rb",
31
+ "lib/systeminformation/os.rb",
32
+ "lib/systeminformation/unix/users.rb",
33
+ "systeminformation.gemspec"
34
+ ]
35
+ s.homepage = %q{http://github.com/tliff/systeminformation}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.7}
39
+ s.summary = %q{Provides information about the system}
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
46
+ else
47
+ end
48
+ else
49
+ end
50
+ end
51
+
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: systeminformation
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Stefan Maier
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-06-07 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Provides information about cpu, memory, network throughput, current load and usercount
23
+ email: stefanmaier@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - LICENSE
30
+ - README.rdoc
31
+ files:
32
+ - .document
33
+ - .gitignore
34
+ - LICENSE
35
+ - README.rdoc
36
+ - Rakefile
37
+ - VERSION
38
+ - lib/systeminformation.rb
39
+ - lib/systeminformation/linux/cpu.rb
40
+ - lib/systeminformation/linux/load.rb
41
+ - lib/systeminformation/linux/memory.rb
42
+ - lib/systeminformation/linux/net.rb
43
+ - lib/systeminformation/os.rb
44
+ - lib/systeminformation/unix/users.rb
45
+ - systeminformation.gemspec
46
+ has_rdoc: true
47
+ homepage: http://github.com/tliff/systeminformation
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options:
52
+ - --charset=UTF-8
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ hash: 3
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ requirements: []
74
+
75
+ rubyforge_project:
76
+ rubygems_version: 1.3.7
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: Provides information about the system
80
+ test_files: []
81
+