vidibus-sysinfo 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ rdoc
5
+ coverage
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :gemcutter
2
+
3
+ # Specify your gem's dependencies in vidibus-sysinfo.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,33 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ vidibus-sysinfo (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.1.2)
10
+ rake (0.8.7)
11
+ relevance-rcov (0.9.2.1)
12
+ rr (1.0.2)
13
+ rspec (2.0.1)
14
+ rspec-core (~> 2.0.1)
15
+ rspec-expectations (~> 2.0.1)
16
+ rspec-mocks (~> 2.0.1)
17
+ rspec-core (2.0.1)
18
+ rspec-expectations (2.0.1)
19
+ diff-lcs (>= 1.1.2)
20
+ rspec-mocks (2.0.1)
21
+ rspec-core (~> 2.0.1)
22
+ rspec-expectations (~> 2.0.1)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ bundler (>= 1.0.0)
29
+ rake
30
+ relevance-rcov
31
+ rr
32
+ rspec (~> 2.0.0.beta.20)
33
+ vidibus-sysinfo!
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Andre Pankratz
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.
data/README.rdoc ADDED
@@ -0,0 +1,106 @@
1
+ = Vidibus::Sysinfo
2
+
3
+ Allows reading various system information.
4
+
5
+
6
+ == Installation
7
+
8
+ Add the dependency to the Gemfile of your application:
9
+
10
+ gem "vidibus-sysinfo"
11
+
12
+ Then call bundle install on your console.
13
+
14
+
15
+ == System requirements
16
+
17
+ This gem uses various tools to obtain system data. On Debian Lenny, installing
18
+ those tools should be simple:
19
+
20
+ apt-get install sysstat vnstat
21
+ vnstat -u -i eth0
22
+
23
+
24
+ == Usage
25
+
26
+ === CPU cores
27
+
28
+ To get the number of CPU cores, call
29
+
30
+ Vidibus::Sysinfo.core
31
+ # => 8
32
+
33
+
34
+ === CPU utilization
35
+
36
+ To get CPU utilization in percent, call
37
+
38
+ Vidibus::Sysinfo.cpu
39
+ # => 18.4 # percent
40
+
41
+
42
+ === System load
43
+
44
+ The system load value is divided by the number of CPU cores.
45
+ To get the system load, call
46
+
47
+ Vidibus::Sysinfo.load
48
+ # => 0.39
49
+
50
+
51
+ === Monthly traffic
52
+
53
+ To get the total traffic of this month in gigabytes, call
54
+
55
+ Vidibus::Sysinfo.traffic
56
+ # => 7992.15 # gigabytes
57
+
58
+
59
+ === Currently used bandwidth
60
+
61
+ To get the currently used bandwidth in MBit/s, call
62
+
63
+ Vidibus::Sysinfo.bandwidth
64
+ # => 38.71 # MBit/s
65
+
66
+ Bandwidth detection is performed by analyzing the output of /proc/net/dev.
67
+ To get the amount of traffic in a certain timespan, the analyzer gets
68
+ called twice and the delta of the two results is the traffic.
69
+
70
+ By default, bandwidth detection only waits one second, thus the results
71
+ will be quite volatile. To get a more accurate result, you can provide
72
+ an optional argument:
73
+
74
+ Vidibus::Sysinfo.bandwidth(10)
75
+ # => 33.28 # MBit/s
76
+
77
+
78
+ === Consumed storage
79
+
80
+ To get the consumed storage of the main device in gigabytes, call
81
+
82
+ Vidibus::Sysinfo.storage
83
+ # => 647.89 # gigabytes
84
+
85
+
86
+ === Used memory
87
+
88
+ To get the currently used memory in megabytes, call
89
+
90
+ Vidibus::Sysinfo.memory
91
+ # => 3281.38 # megabytes
92
+
93
+ This will ignore memory used for system caching.
94
+
95
+
96
+ === Used swap
97
+
98
+ To get the currently used swap in megabytes, call
99
+
100
+ Vidibus::Sysinfo.swap
101
+ # => 0.0 # megabytes
102
+
103
+
104
+ == Copyright
105
+
106
+ Copyright (c) 2011 Andre Pankratz. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ require "bundler"
2
+ require "rake/rdoctask"
3
+ require "rspec"
4
+ require "rspec/core/rake_task"
5
+ Bundler::GemHelper.install_tasks
6
+
7
+ Rspec::Core::RakeTask.new(:rcov) do |t|
8
+ t.pattern = "spec/**/*_spec.rb"
9
+ t.rcov = true
10
+ t.rcov_opts = ["--exclude", "^spec,/gems/"]
11
+ end
12
+
13
+ Rake::RDocTask.new do |rdoc|
14
+ require File.expand_path("../lib/vidibus/sysinfo/version.rb", __FILE__)
15
+ rdoc.rdoc_dir = "rdoc"
16
+ rdoc.title = "vidibus-sysinfo #{Vidibus::Sysinfo::VERSION}"
17
+ rdoc.rdoc_files.include("README*")
18
+ rdoc.rdoc_files.include("lib/**/*.rb")
19
+ rdoc.options << "--charset=utf-8"
20
+ end
@@ -0,0 +1,48 @@
1
+ module Vidibus
2
+ module Sysinfo
3
+
4
+ # Returns currently used bandwith in MBit/s.
5
+ #
6
+ # Analyzes /proc/net/dev
7
+ #
8
+ module Bandwidth
9
+ extend Base
10
+
11
+ class << self
12
+ def command
13
+ "cat /proc/net/dev | grep eth0:"
14
+ end
15
+
16
+ # Provide seconds to sleep between first and second call.
17
+ # The higher the seconds, the more accurate are the results.
18
+ def call(seconds = 1)
19
+ values = []
20
+ 2.times do
21
+ output, error = perform(command)
22
+ values << respond(output, error)
23
+ sleep(seconds) unless values.size > 1
24
+ end
25
+ megs = values[1] - values[0]
26
+ mbits = (megs*8)/seconds
27
+ round(mbits)
28
+ end
29
+
30
+ # Returns sum of transmitted and received bytes in megabytes.
31
+ def parse(output)
32
+ if output.match(/eth0\:([\d\s]+)/)
33
+ numbers = $1.split(/\s+/)
34
+ received = numbers[0].to_i
35
+ transmitted = numbers[8].to_i
36
+ megabytes(received + transmitted)
37
+ end
38
+ end
39
+
40
+ def explain(error)
41
+ if error.match("No such file or directory")
42
+ return "This system does not provide /proc/net/dev"
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,72 @@
1
+ module Vidibus
2
+ module Sysinfo
3
+
4
+ class CallError < StandardError; end
5
+ class OutputError < StandardError; end
6
+ class ParseError < StandardError; end
7
+
8
+ # Provides common methods.
9
+ module Base
10
+
11
+ def call
12
+ output, error = perform(command)
13
+ respond(output, error)
14
+ end
15
+
16
+ def explain(error); end
17
+
18
+ private
19
+
20
+ def perform(cmd)
21
+ response = nil
22
+ Open3.popen3(cmd) do |stdin, stdout, stderr|
23
+ response = [stdout.read, stderr.read]
24
+ end
25
+ response
26
+ end
27
+
28
+ def respond(output, error)
29
+ if error and error != ""
30
+ reason = explain(error) || open3_reason(error) || error
31
+ raise CallError.new("Failed to call #{self}:\n- #{reason}")
32
+ end
33
+
34
+ unless output
35
+ raise OutputError.new("Result from call of #{self} is empty")
36
+ end
37
+
38
+ parse(output) or
39
+ raise ParseError.new("Result from call of #{self} could not be parsed: #{output.strip}")
40
+ end
41
+
42
+ def open3_reason(error)
43
+ if error.match("in `popen3'")
44
+ "the command '#{command}' is not available or this program is not allowed to call it"
45
+ end
46
+ end
47
+
48
+ # Rounds float with given precision.
49
+ def round(float, precision = 2)
50
+ m = 10**precision
51
+ (float.to_f*m).round.to_f/m
52
+ end
53
+
54
+ # Converts given amount from unit to megabytes.
55
+ def megabytes(value, unit = "B")
56
+ value = value.to_f
57
+ case unit
58
+ when "B" : value /= 1048576 # bytes
59
+ when "K", "KB": value /= 1024 # kiloytes
60
+ when "G", "GB": value *= 1024 # gigabytes
61
+ when "T", "TB": value *= 1048576 # terabytes
62
+ end
63
+ round(value)
64
+ end
65
+
66
+ # Converts given amount from unit to gigabytes.
67
+ def gigabytes(value, unit = "B")
68
+ round(megabytes(value, unit) / 1024)
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,30 @@
1
+ module Vidibus
2
+ module Sysinfo
3
+
4
+ # Returns number of cpu cores on this system.
5
+ #
6
+ # Analyzes /proc/cpuinfo
7
+ #
8
+ module Core
9
+ extend Base
10
+
11
+ class << self
12
+ def command
13
+ "cat /proc/cpuinfo | grep processor | wc -l"
14
+ end
15
+
16
+ def parse(output)
17
+ if output.match(/\d+/)
18
+ output.to_i
19
+ end
20
+ end
21
+
22
+ def explain(error)
23
+ if error.match("No such file or directory")
24
+ return "This system does not provide /proc/cpuinfo"
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,34 @@
1
+ module Vidibus
2
+ module Sysinfo
3
+
4
+ # Returns CPU utilization in percent.
5
+ #
6
+ # Calls `mpstat`
7
+ #
8
+ # mpstat is part of the "systat" tools for Linux.
9
+ # Installation on Debian:
10
+ # apt-get install sysstat
11
+ #
12
+ module Cpu
13
+ extend Base
14
+
15
+ class << self
16
+ def command
17
+ "mpstat 5 1 | grep Average:"
18
+ end
19
+
20
+ def parse(output)
21
+ if output.match(/([\d\.]+)\s+[^\s]+$/)
22
+ round(100.0 - $1.to_f)
23
+ end
24
+ end
25
+
26
+ def explain(error)
27
+ if error.match("mpstat: command not found")
28
+ return "mpstat is not installed. On Debian, you can install it with 'apt-get install sysstat'"
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,26 @@
1
+ module Vidibus
2
+ module Sysinfo
3
+
4
+ # Returns system load, divided by number of CPU cores.
5
+ #
6
+ # Calls `uptime`
7
+ #
8
+ module Load
9
+ extend Base
10
+
11
+ class << self
12
+ def command
13
+ "uptime"
14
+ end
15
+
16
+ def parse(output)
17
+ if output.match(/load average:\s+(\d+(?:\.\d+)?)/)
18
+ value = $1.to_f
19
+ cores = Core.call
20
+ round(value/cores)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,27 @@
1
+ module Vidibus
2
+ module Sysinfo
3
+
4
+ # Returns used memory in megabytes.
5
+ #
6
+ # Calls `free`
7
+ #
8
+ module Memory
9
+ extend Base
10
+
11
+ class << self
12
+ def command
13
+ "free -m | grep Mem:"
14
+ end
15
+
16
+ def parse(output)
17
+ if output.match(/^Mem:\s+([\d\s]+)$/)
18
+ numbers = $1.split(/\s+/)
19
+ used = numbers[1].to_i
20
+ cached = numbers[5].to_i
21
+ used - cached
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,28 @@
1
+ module Vidibus
2
+ module Sysinfo
3
+
4
+ # Returns consumed storage in gigabytes.
5
+ #
6
+ # Calls `df`
7
+ #
8
+ module Storage
9
+ extend Base
10
+
11
+ class << self
12
+ def command
13
+ "df -h"
14
+ end
15
+
16
+ def parse(output)
17
+ device = /(?:[\/a-z0-9]+)/
18
+ size = /\s+([\d\.]+)(K|M|G|T)\i?/
19
+ if output.match(/#{device}#{size}#{size}/m)
20
+ amount = $3.to_f
21
+ unit = $4
22
+ gigabytes(amount, unit)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,25 @@
1
+ module Vidibus
2
+ module Sysinfo
3
+
4
+ # Returns used swap in megabytes.
5
+ #
6
+ # Calls `free`
7
+ #
8
+ module Swap
9
+ extend Base
10
+
11
+ class << self
12
+ def command
13
+ "free -m | grep Swap:"
14
+ end
15
+
16
+ def parse(output)
17
+ if output.match(/^Swap:\s+([\d\s]+)$/)
18
+ numbers = $1.split(/\s+/)
19
+ numbers[1].to_i
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,41 @@
1
+ module Vidibus
2
+ module Sysinfo
3
+
4
+ # Returns total traffic of this month in gigabytes.
5
+ #
6
+ # Calls `vnstat`
7
+ #
8
+ # Installation on Debian:
9
+ # apt-get install vnstat
10
+ # vnstat -u -i eth0
11
+ #
12
+ module Traffic
13
+ extend Base
14
+
15
+ class << self
16
+ def command
17
+ "vnstat -m"
18
+ end
19
+
20
+ def parse(output)
21
+ month = /(\s*\w{3} \'\d{2})/
22
+ traffic = /\s*(\d+(?:\.\d+)?) (kB|MB|GB|TB)\s*/
23
+ last_month = output.split(/\r?\n/)[-3]
24
+ if last_month and last_month.match(/#{month}#{traffic}\|#{traffic}\|#{traffic}+/m)
25
+ amount = $6.to_f
26
+ unit = $7
27
+ gigabytes(amount, unit)
28
+ elsif output.match("Not enough data available yet")
29
+ 0.0
30
+ end
31
+ end
32
+
33
+ def explain(error)
34
+ if error.match("vnstat: command not found")
35
+ return "vnstat is not installed. On Debian, you can install it with 'apt-get install vnstat' and 'vnstat -u -i eth0'"
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,5 @@
1
+ module Vidibus
2
+ module Sysinfo
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,58 @@
1
+ require "sysinfo/base"
2
+ require "sysinfo/core"
3
+ require "sysinfo/cpu"
4
+ require "sysinfo/load"
5
+ require "sysinfo/traffic"
6
+ require "sysinfo/bandwidth"
7
+ require "sysinfo/storage"
8
+ require "sysinfo/memory"
9
+ require "sysinfo/swap"
10
+
11
+ module Vidibus
12
+ module Sysinfo
13
+ class << self
14
+
15
+ # Returns number of cpu cores.
16
+ def core
17
+ Core.call
18
+ end
19
+
20
+ # Returns CPU utilization in percent.
21
+ def cpu
22
+ Cpu.call
23
+ end
24
+
25
+ # Returns system load, divided by number of CPU cores.
26
+ def load
27
+ Load.call
28
+ end
29
+
30
+ # Returns total traffic of this month in gigabytes.
31
+ def traffic
32
+ Traffic.call
33
+ end
34
+
35
+ # Returns currently used bandwith in MBit/s.
36
+ # Provide seconds to improve measurement.
37
+ # The higher the seconds, the more accurate are the results.
38
+ def bandwidth(seconds = 1)
39
+ Bandwidth.call(seconds)
40
+ end
41
+
42
+ # Returns consumed storage in gigabytes.
43
+ def storage
44
+ Storage.call
45
+ end
46
+
47
+ # Returns used memory in megabytes.
48
+ def memory
49
+ Memory.call
50
+ end
51
+
52
+ # Returns used swap in megabytes.
53
+ def swap
54
+ Swap.call
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,2 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), "vidibus"))
2
+ require "sysinfo"
@@ -0,0 +1,11 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
3
+
4
+ require "rubygems"
5
+ require "rspec"
6
+ require "rr"
7
+ require "vidibus-sysinfo"
8
+
9
+ RSpec.configure do |config|
10
+ config.mock_with :rr
11
+ end
@@ -0,0 +1,59 @@
1
+ require "spec_helper"
2
+
3
+ describe "Vidibus::Sysinfo::Bandwidth" do
4
+ let(:this) {Vidibus::Sysinfo::Bandwidth}
5
+ let(:first_output) do
6
+ " eth0:11987621067 183430993 0 0 0 0 0 0 639006876738 427999432 0 0 0 0 0 0"
7
+ end
8
+ let(:second_output) do
9
+ " eth0:11987897466 183435418 0 0 0 0 0 0 639026477572 428012510 0 0 0 0 0 0"
10
+ end
11
+
12
+ describe ".command" do
13
+ it "should return 'cat /proc/net/dev | grep eth0:'" do
14
+ this.command.should eql("cat /proc/net/dev | grep eth0:")
15
+ end
16
+ end
17
+
18
+ describe ".parse" do
19
+ it "should return a float of megabytes from valid output" do
20
+ this.parse(first_output).should eql(620836.73)
21
+ end
22
+
23
+ it "should return nil from invalid output" do
24
+ this.parse("something").should be_nil
25
+ end
26
+ end
27
+
28
+ describe ".call" do
29
+ let(:calls) do
30
+ [
31
+ [first_output, ""],
32
+ [second_output, ""]
33
+ ]
34
+ end
35
+
36
+ before do
37
+ stub(this).perform(this.command) {calls.shift}
38
+ end
39
+
40
+ it "should return the currently used bandwidth in MBit/s" do
41
+ this.call.should eql(151.68)
42
+ end
43
+
44
+ it "should accept a seconds argument as interval" do
45
+ mock(this).sleep(2)
46
+ this.call(2)
47
+ end
48
+
49
+ it "should divide the result by given seconds" do
50
+ stub(this).sleep(3)
51
+ this.call(3).should eql(151.68/3)
52
+ end
53
+
54
+ it "should yield an error if /proc/net/dev is not available" do
55
+ stub(this).perform(this.command) {["", "cat: /proc/net/dev: No such file or directory\n"]}
56
+ expect {this.call}.to raise_error(Vidibus::Sysinfo::CallError)
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,34 @@
1
+ require "spec_helper"
2
+
3
+ describe "Vidibus::Sysinfo::Core" do
4
+ let(:this) {Vidibus::Sysinfo::Core}
5
+ let(:output) {"8"}
6
+
7
+ describe ".command" do
8
+ it "should return 'cat /proc/cpuinfo | grep processor | wc -l'" do
9
+ this.command.should eql("cat /proc/cpuinfo | grep processor | wc -l")
10
+ end
11
+ end
12
+
13
+ describe ".parse" do
14
+ it "should return a number from valid output" do
15
+ this.parse(output).should eql(8)
16
+ end
17
+
18
+ it "should return nil from invalid output" do
19
+ this.parse("something").should be_nil
20
+ end
21
+ end
22
+
23
+ describe ".call" do
24
+ it "should return the number of CPU cores" do
25
+ stub(this).perform(this.command) {[output, ""]}
26
+ this.call.should eql(8)
27
+ end
28
+
29
+ it "should yield an error if /proc/cpuinfo is not available" do
30
+ stub(this).perform(this.command) {[" 0\n", "cat: /proc/cpuinfo: No such file or directory\n"]}
31
+ expect {this.call}.to raise_error(Vidibus::Sysinfo::CallError)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,34 @@
1
+ require "spec_helper"
2
+
3
+ describe "Vidibus::Sysinfo::Cpu" do
4
+ let(:this) {Vidibus::Sysinfo::Cpu}
5
+ let(:output) {"Average: all 0.72 0.00 0.21 0.41 0.05 0.26 0.00 98.35 2950.20"}
6
+
7
+ describe ".command" do
8
+ it "should return 'mpstat 5 1 | grep Average:'" do
9
+ this.command.should eql("mpstat 5 1 | grep Average:")
10
+ end
11
+ end
12
+
13
+ describe ".parse" do
14
+ it "should return a decimal from valid output" do
15
+ this.parse(output).should eql(1.65)
16
+ end
17
+
18
+ it "should return nil from invalid output" do
19
+ this.parse("something").should be_nil
20
+ end
21
+ end
22
+
23
+ describe ".call" do
24
+ it "should return the CPU load in percent" do
25
+ stub(this).perform(this.command) {[output, ""]}
26
+ this.call.should eql(1.65)
27
+ end
28
+
29
+ it "should yield an error if mpstat is not installed" do
30
+ stub(this).perform(this.command) {["", "sh: mpstat: command not found\n"]}
31
+ expect {this.call}.to raise_error(Vidibus::Sysinfo::CallError)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,36 @@
1
+ require "spec_helper"
2
+
3
+ describe "Vidibus::Sysinfo::Load" do
4
+ let(:this) {Vidibus::Sysinfo::Load}
5
+ let(:output) {" 19:52:41 up 2 days, 9:39, 4 users, load average: 0.46, 0.53, 0.56"}
6
+
7
+ before {stub(Vidibus::Sysinfo::Core).call {2}}
8
+
9
+ describe ".command" do
10
+ it "should return 'uptime'" do
11
+ this.command.should eql("uptime")
12
+ end
13
+ end
14
+
15
+ describe ".parse" do
16
+ it "should return a decimal from valid output" do
17
+ this.parse(output).should eql(0.23)
18
+ end
19
+
20
+ it "should return nil from invalid output" do
21
+ this.parse("something").should be_nil
22
+ end
23
+ end
24
+
25
+ describe ".call" do
26
+ it "should return the system load as decimal" do
27
+ stub(this).perform(this.command) {[output, ""]}
28
+ this.call.should eql(0.23)
29
+ end
30
+
31
+ it "should yield a ParseError if output of uptime could not be parsed" do
32
+ stub(this).perform(this.command) {["20:14 up 6 days, 22:03, 20 users, load averages: 0,41 0,26 0,29\n",""]}
33
+ expect {this.call}.to raise_error(Vidibus::Sysinfo::ParseError)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,36 @@
1
+ require "spec_helper"
2
+
3
+ describe "Vidibus::Sysinfo::Memory" do
4
+ let(:this) {Vidibus::Sysinfo::Memory}
5
+ let(:output) do
6
+ "Mem: 12041 11873 167 0 75 10511"
7
+ end
8
+
9
+ describe ".command" do
10
+ it "should return 'free -m | grep Mem:'" do
11
+ this.command.should eql("free -m | grep Mem:")
12
+ end
13
+ end
14
+
15
+ describe ".parse" do
16
+ it "should return a number from valid output" do
17
+ this.parse(output).should eql(1362)
18
+ end
19
+
20
+ it "should return nil from invalid output" do
21
+ this.parse("something").should be_nil
22
+ end
23
+ end
24
+
25
+ describe ".call" do
26
+ it "should return the number of CPU cores" do
27
+ stub(this).perform(this.command) {[output, ""]}
28
+ this.call.should eql(1362)
29
+ end
30
+
31
+ it "should yield an error if /proc/cpuinfo is not available" do
32
+ stub(this).perform(this.command) {["", "sh: free: command not found\n"]}
33
+ expect {this.call}.to raise_error(Vidibus::Sysinfo::CallError)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,36 @@
1
+ require "spec_helper"
2
+
3
+ describe "Vidibus::Sysinfo::Storage" do
4
+ let(:this) {Vidibus::Sysinfo::Storage}
5
+ let(:output) do
6
+ "Filesystem Size Used Avail Use% Mounted on
7
+ /dev/md2 1.4T 693G 622G 53% /
8
+ tmpfs 5.9G 0 5.9G 0% /lib/init/rw
9
+ udev 10M 764K 9.3M 8% /dev
10
+ tmpfs 5.9G 0 5.9G 0% /dev/shm
11
+ /dev/md1 251M 22M 217M 10% /boot"
12
+ end
13
+
14
+ describe ".command" do
15
+ it "should return 'df -h'" do
16
+ this.command.should eql("df -h")
17
+ end
18
+ end
19
+
20
+ describe ".parse" do
21
+ it "should return a number from valid output" do
22
+ this.parse(output).should eql(693.0)
23
+ end
24
+
25
+ it "should return nil from invalid output" do
26
+ this.parse("something").should be_nil
27
+ end
28
+ end
29
+
30
+ describe ".call" do
31
+ it "should return the used storage in gigabytes" do
32
+ stub(this).perform(this.command) {[output, ""]}
33
+ this.call.should eql(693.0)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,36 @@
1
+ require "spec_helper"
2
+
3
+ describe "Vidibus::Sysinfo::Swap" do
4
+ let(:this) {Vidibus::Sysinfo::Swap}
5
+ let(:output) do
6
+ "Swap: 2053 0 2052"
7
+ end
8
+
9
+ describe ".command" do
10
+ it "should return 'free -m | grep Swap:'" do
11
+ this.command.should eql("free -m | grep Swap:")
12
+ end
13
+ end
14
+
15
+ describe ".parse" do
16
+ it "should return a number from valid output" do
17
+ this.parse(output).should eql(0)
18
+ end
19
+
20
+ it "should return nil from invalid output" do
21
+ this.parse("something").should be_nil
22
+ end
23
+ end
24
+
25
+ describe ".call" do
26
+ it "should return the number of CPU cores" do
27
+ stub(this).perform(this.command) {[output, ""]}
28
+ this.call.should eql(0)
29
+ end
30
+
31
+ it "should yield an error if /proc/cpuinfo is not available" do
32
+ stub(this).perform(this.command) {["", "sh: free: command not found\n"]}
33
+ expect {this.call}.to raise_error(Vidibus::Sysinfo::CallError)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,46 @@
1
+ require "spec_helper"
2
+
3
+ describe "Vidibus::Sysinfo::Traffic" do
4
+ let(:this) {Vidibus::Sysinfo::Traffic}
5
+ let(:output) do
6
+ " eth0 / monthly
7
+
8
+ month rx | tx | total
9
+ -------------------------+--------------+--------------------------------------
10
+ Feb '11 31.62 GB | 1.93 TB | 1.96 TB ::::::::::::::::::::::
11
+ -------------------------+--------------+--------------------------------------
12
+ estimated 88 GB | 5.50 TB | 5.58 TB"
13
+ end
14
+
15
+ describe ".command" do
16
+ it "should return 'vnstat -m'" do
17
+ this.command.should eql("vnstat -m")
18
+ end
19
+ end
20
+
21
+ describe ".parse" do
22
+ it "should return a number from valid output" do
23
+ this.parse(output).should eql(2007.04)
24
+ end
25
+
26
+ it "should return 0.0 if not enough data is available yet" do
27
+ this.parse(" eth0: Not enough data available yet.\n").should eql(0.0)
28
+ end
29
+
30
+ it "should return nil from invalid output" do
31
+ this.parse("something").should be_nil
32
+ end
33
+ end
34
+
35
+ describe ".call" do
36
+ it "should return the total traffic in gigabytes" do
37
+ stub(this).perform(this.command) {[output, ""]}
38
+ this.call.should eql(2007.04)
39
+ end
40
+
41
+ it "should yield an error if the command is not available" do
42
+ stub(this).perform(this.command) {["", "/Users/punkrats/.rvm/rubies/ruby-1.8.7-p249/lib/ruby/1.8/open3.rb:73:in `exec': Permission denied - vnstat -m (Errno::EACCES)\n\tfrom /Users/punkrats/.rvm/rubies/ruby-1.8.7-p249/lib/ruby/1.8/open3.rb:73:in `popen3'\n\tfrom..."]}
43
+ expect {this.call}.to raise_error(Vidibus::Sysinfo::CallError)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,66 @@
1
+ require "spec_helper"
2
+
3
+ describe "Vidibus::Sysinfo" do
4
+ let(:this) {Vidibus::Sysinfo}
5
+
6
+ describe ".core" do
7
+ it "should call Vidibus::Sysinfo::Core" do
8
+ mock(this::Core).call
9
+ this.core
10
+ end
11
+ end
12
+
13
+ describe ".cpu" do
14
+ it "should call Vidibus::Sysinfo::Cpu" do
15
+ mock(this::Cpu).call
16
+ this.cpu
17
+ end
18
+ end
19
+
20
+ describe ".load" do
21
+ it "should call Vidibus::Sysinfo::Load" do
22
+ mock(this::Load).call
23
+ this.load
24
+ end
25
+ end
26
+
27
+ describe ".traffic" do
28
+ it "should call Vidibus::Sysinfo::Traffic" do
29
+ mock(this::Traffic).call
30
+ this.traffic
31
+ end
32
+ end
33
+
34
+ describe ".bandwidth" do
35
+ it "should call Vidibus::Sysinfo::Bandwidth" do
36
+ mock(this::Bandwidth).call(1)
37
+ this.bandwidth
38
+ end
39
+
40
+ it "should accept a seconds argument" do
41
+ mock(this::Bandwidth).call(2)
42
+ this.bandwidth(2)
43
+ end
44
+ end
45
+
46
+ describe ".storage" do
47
+ it "should call Vidibus::Sysinfo::Storage" do
48
+ mock(this::Storage).call
49
+ this.storage
50
+ end
51
+ end
52
+
53
+ describe ".memory" do
54
+ it "should call Vidibus::Sysinfo::Memory" do
55
+ mock(this::Memory).call
56
+ this.memory
57
+ end
58
+ end
59
+
60
+ describe ".swap" do
61
+ it "should call Vidibus::Sysinfo::Swap" do
62
+ mock(this::Swap).call
63
+ this.swap
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/vidibus/sysinfo/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "vidibus-sysinfo"
6
+ s.version = Vidibus::Sysinfo::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = "Andre Pankratz"
9
+ s.email = "andre@vidibus.com"
10
+ s.homepage = "https://github.com/vidibus/vidibus-sysinfo"
11
+ s.summary = "Provides tools for obtaining information about the system"
12
+ s.description = "Gets CPU usage, current load, consumed memory and some other figures."
13
+
14
+ s.required_rubygems_version = ">= 1.3.6"
15
+ s.rubyforge_project = "vidibus-sysinfo"
16
+
17
+ s.add_development_dependency "bundler", ">= 1.0.0"
18
+ s.add_development_dependency "rake"
19
+ s.add_development_dependency "rspec", "~> 2.0.0.beta.20"
20
+ s.add_development_dependency "rr"
21
+ s.add_development_dependency "relevance-rcov"
22
+
23
+ s.files = `git ls-files`.split("\n")
24
+ s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
25
+ s.require_path = 'lib'
26
+ end
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vidibus-sysinfo
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Andre Pankratz
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-11 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: bundler
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 23
30
+ segments:
31
+ - 1
32
+ - 0
33
+ - 0
34
+ version: 1.0.0
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rake
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :development
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: rspec
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ hash: 62196427
60
+ segments:
61
+ - 2
62
+ - 0
63
+ - 0
64
+ - beta
65
+ - 20
66
+ version: 2.0.0.beta.20
67
+ type: :development
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: rr
71
+ prerelease: false
72
+ requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ type: :development
82
+ version_requirements: *id004
83
+ - !ruby/object:Gem::Dependency
84
+ name: relevance-rcov
85
+ prerelease: false
86
+ requirement: &id005 !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ hash: 3
92
+ segments:
93
+ - 0
94
+ version: "0"
95
+ type: :development
96
+ version_requirements: *id005
97
+ description: Gets CPU usage, current load, consumed memory and some other figures.
98
+ email: andre@vidibus.com
99
+ executables: []
100
+
101
+ extensions: []
102
+
103
+ extra_rdoc_files: []
104
+
105
+ files:
106
+ - .gitignore
107
+ - Gemfile
108
+ - Gemfile.lock
109
+ - LICENSE
110
+ - README.rdoc
111
+ - Rakefile
112
+ - lib/vidibus-sysinfo.rb
113
+ - lib/vidibus/sysinfo.rb
114
+ - lib/vidibus/sysinfo/bandwidth.rb
115
+ - lib/vidibus/sysinfo/base.rb
116
+ - lib/vidibus/sysinfo/core.rb
117
+ - lib/vidibus/sysinfo/cpu.rb
118
+ - lib/vidibus/sysinfo/load.rb
119
+ - lib/vidibus/sysinfo/memory.rb
120
+ - lib/vidibus/sysinfo/storage.rb
121
+ - lib/vidibus/sysinfo/swap.rb
122
+ - lib/vidibus/sysinfo/traffic.rb
123
+ - lib/vidibus/sysinfo/version.rb
124
+ - spec/spec_helper.rb
125
+ - spec/vidibus/sysinfo/bandwidth_spec.rb
126
+ - spec/vidibus/sysinfo/core_spec.rb
127
+ - spec/vidibus/sysinfo/cpu_spec.rb
128
+ - spec/vidibus/sysinfo/load_spec.rb
129
+ - spec/vidibus/sysinfo/memory_spec.rb
130
+ - spec/vidibus/sysinfo/storage_spec.rb
131
+ - spec/vidibus/sysinfo/swap_spec.rb
132
+ - spec/vidibus/sysinfo/traffic_spec.rb
133
+ - spec/vidibus/sysinfo_spec.rb
134
+ - vidibus-sysinfo.gemspec
135
+ has_rdoc: true
136
+ homepage: https://github.com/vidibus/vidibus-sysinfo
137
+ licenses: []
138
+
139
+ post_install_message:
140
+ rdoc_options: []
141
+
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ hash: 3
150
+ segments:
151
+ - 0
152
+ version: "0"
153
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
+ none: false
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ hash: 23
159
+ segments:
160
+ - 1
161
+ - 3
162
+ - 6
163
+ version: 1.3.6
164
+ requirements: []
165
+
166
+ rubyforge_project: vidibus-sysinfo
167
+ rubygems_version: 1.3.7
168
+ signing_key:
169
+ specification_version: 3
170
+ summary: Provides tools for obtaining information about the system
171
+ test_files: []
172
+