server_metrics 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/test/test.rb ADDED
@@ -0,0 +1,14 @@
1
+ require File.expand_path("test_helper", File.dirname(__FILE__))
2
+ class TestWithFixtures < Test::Unit::TestCase
3
+
4
+ def test_system_info
5
+ assert ServerMetrics::SystemInfo.architecture
6
+ assert ServerMetrics::SystemInfo.os
7
+ assert ServerMetrics::SystemInfo.os_version
8
+ assert ServerMetrics::SystemInfo.num_processors
9
+ assert ServerMetrics::SystemInfo.hostname
10
+ assert ServerMetrics::SystemInfo.timezone
11
+ assert ServerMetrics::SystemInfo.timezone_offset
12
+ end
13
+
14
+ end
@@ -0,0 +1,153 @@
1
+ require File.expand_path("test_helper",File.dirname(__FILE__))
2
+
3
+ class TestWithFixtures < Test::Unit::TestCase
4
+
5
+ def test_colectors_defined
6
+ assert_nothing_raised do
7
+ ServerMetrics::Cpu
8
+ ServerMetrics::Disk
9
+ ServerMetrics::Memory
10
+ ServerMetrics::Network
11
+ end
12
+ end
13
+
14
+ def test_collector_to_hash
15
+ c=ServerMetrics::Collector.new(:port=>80)
16
+ h=c.to_hash
17
+ assert h[:options]
18
+ assert h[:data]
19
+ assert h[:memory]
20
+ end
21
+
22
+ def test_collector_from_hash
23
+ c=ServerMetrics::Collector.new(:port=>80)
24
+ c2=ServerMetrics::Collector.from_hash(c.to_hash)
25
+ assert_equal 80, c2.option(:port)
26
+ end
27
+
28
+ def test_collector
29
+ c=SomeCollector.new
30
+ c.run
31
+ assert_equal({:capacity=>9}, c.data)
32
+ end
33
+
34
+ def test_collector_with_memory
35
+ c=SomeCollectorWithMemory.new
36
+ c.run
37
+ assert_equal(0, c.data["val"])
38
+ c.run
39
+ assert_equal(1, c.data["val"])
40
+ end
41
+
42
+ def test_collector_with_counter
43
+ c=SomeCollectorWithCounter.new
44
+ c.run
45
+ assert_equal(nil, c.data["val"])
46
+ Timecop.travel(1) do
47
+ c.run
48
+ assert_includes(450..550, c.data["val"])
49
+ end
50
+ end
51
+
52
+
53
+ def test_multi_collector
54
+ c=SomeMultiCollector.new
55
+ c.run
56
+ assert_equal({:alpha=>{"capacity"=>9},:beta=>{"capacity"=>10}}, c.data)
57
+ end
58
+
59
+ def test_multi_collector_with_memory
60
+ c=SomeMultiCollectorWithMemory.new
61
+ c.run
62
+ assert_equal(0, c.data[:alpha]["val"])
63
+ assert_equal(100, c.data[:beta]["val"])
64
+ c.run
65
+ assert_equal(1, c.data[:alpha]["val"])
66
+ assert_equal(101, c.data[:beta]["val"])
67
+ end
68
+
69
+ def test_multi_collector_with_counter
70
+ c=SomeMultiCollectorWithCounter.new
71
+ c.run
72
+ assert_equal(nil, c.data[:alpha])
73
+ assert_equal(nil, c.data[:beta])
74
+ Timecop.travel(1) do
75
+ c.run
76
+ assert_include(450..550, c.data[:alpha]["val"])
77
+ assert_include(950..1050, c.data[:beta]["val"])
78
+ end
79
+ end
80
+
81
+ def test_processes_to_hash
82
+ p = ServerMetrics::Processes.new
83
+ last_run=Time.now-60
84
+ p.instance_variable_set '@last_run', last_run
85
+ p.instance_variable_set '@last_process_list', "bogus value"
86
+
87
+ assert_equal({:last_run=>last_run,:last_process_list=>"bogus value"}, p.to_hash)
88
+ end
89
+
90
+ def test_processes_from_hash
91
+ last_run=Time.now-60
92
+ p=ServerMetrics::Processes.from_hash(:last_run=>last_run,:last_process_list=>"bogus value")
93
+ assert_equal last_run, p.instance_variable_get("@last_run")
94
+ assert_equal "bogus value", p.instance_variable_get("@last_process_list")
95
+ end
96
+
97
+ # Helper Classes
98
+ class SomeCollector < ServerMetrics::Collector
99
+ def build_report
100
+ report(:capacity=>9)
101
+ end
102
+ end
103
+
104
+ class SomeMultiCollector < ServerMetrics::MultiCollector
105
+ def build_report
106
+ report(:alpha,"capacity"=>9)
107
+ report(:beta,"capacity"=>10)
108
+ end
109
+ end
110
+
111
+ class SomeCollectorWithMemory < ServerMetrics::Collector
112
+ def build_report
113
+ @val = memory(:val) || 0
114
+ report("val"=>@val)
115
+ remember :val=>@val+1
116
+ end
117
+ end
118
+
119
+ class SomeMultiCollectorWithMemory < ServerMetrics::MultiCollector
120
+ def build_report
121
+ @alpha_val = memory(:alpha,:val) || 0
122
+ @beta_val = memory(:beta,:val) || 100
123
+
124
+ report(:alpha, "val"=>@alpha_val)
125
+ report(:beta, "val"=>@beta_val)
126
+
127
+ remember(:alpha, :val => @alpha_val+1)
128
+ remember(:beta, :val => @beta_val+1)
129
+ end
130
+ end
131
+
132
+ class SomeCollectorWithCounter < ServerMetrics::Collector
133
+ def build_report
134
+ @val = memory(:val) || 0
135
+ counter "val", @val, :per=>:second
136
+ remember :val=>@val+500
137
+ end
138
+ end
139
+
140
+ class SomeMultiCollectorWithCounter < ServerMetrics::MultiCollector
141
+ def build_report
142
+ @alpha_val = memory(:alpha,:val) || 0
143
+ @beta_val = memory(:beta,:val) || 0
144
+
145
+ counter(:alpha, "val", @alpha_val, :per=>:second)
146
+ counter(:beta, "val", @beta_val, :per=>:second)
147
+
148
+ remember(:alpha, :val => @alpha_val+500)
149
+ remember(:beta, :val => @beta_val+1000)
150
+ end
151
+ end
152
+
153
+ end
@@ -0,0 +1,49 @@
1
+ $LOAD_PATH << File.expand_path( File.dirname(__FILE__) + '/..' )
2
+ require 'test/unit'
3
+ require 'lib/server_metrics'
4
+ require 'pry'
5
+ require 'awesome_print'
6
+ require 'timecop'
7
+ require 'mocha'
8
+ AwesomePrint.defaults = {
9
+ :indent => -2,
10
+ :sort_keys =>true
11
+ }
12
+
13
+
14
+ def fixtures(name)
15
+ FixtureFile.new(File.expand_path(File.dirname(__FILE__)+"/fixtures/#{name}.txt"))
16
+ end
17
+
18
+ # parses fixture files, intended for easily mocking system calls
19
+ # Fixture files are in the format:
20
+ #
21
+ # ### `some shell command` options
22
+ # the text output ...
23
+ #
24
+ # ### `another command` options
25
+ #
26
+ # ... with options being whatever text you'd like to be able to select this command with later
27
+ class FixtureFile
28
+ def initialize(file)
29
+ contents=File.read(file)
30
+ sections = contents.split(/### (.+)\n/)
31
+ sections = sections[1..sections.size-1]
32
+ @sections={}
33
+ (0..sections.size-1).each do |index|
34
+ @sections[sections[index]] = sections[index+1].chop if index.even?
35
+ end
36
+ end
37
+
38
+ def command(cmd, options=nil)
39
+ key = options ? @sections.keys.find{|k|k =~/`#{cmd}` #{options}/} : @sections.keys.find{|k|k =~/`#{cmd}`/}
40
+
41
+ if !key
42
+ puts "No fixture found for `#{cmd}` with options: #{options ? options : nil }. Fixtures available:\n"
43
+ puts @sections.keys.join("\n")
44
+ exit
45
+ else
46
+ @sections[key]
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,105 @@
1
+ require File.expand_path("test_helper",File.dirname(__FILE__))
2
+
3
+ class TestWithFixtures < Test::Unit::TestCase
4
+
5
+ def test_cpu
6
+ fixture = fixtures(:cpu)
7
+ c = ServerMetrics::Cpu.new()
8
+
9
+ ServerMetrics::Cpu::CpuStats.expects(:`).with("cat /proc/stat 2>&1").returns(fixture.command("cat /proc/stat 2>&1")).once
10
+ c.expects(:`).with("uptime").returns(fixture.command("uptime")).once
11
+
12
+ c.run
13
+ assert c.data["Last minute"]
14
+ assert c.data["Last five minutes"]
15
+ assert c.data["Last fifteen minutes"]
16
+ assert_equal 3, c.data.keys.size
17
+ c
18
+ end
19
+
20
+ def test_cpu_second_run
21
+ fixture = fixtures(:cpu)
22
+ c = test_cpu
23
+
24
+ ServerMetrics::Cpu::CpuStats.expects(:`).with("cat /proc/stat 2>&1").returns(fixture.command("cat /proc/stat 2>&1", "second run")).once
25
+ c.expects(:`).with("uptime").returns(fixture.command("uptime")).once
26
+
27
+ Timecop.travel(60) do
28
+ c.run
29
+ end
30
+
31
+ assert c.data["IO wait"]
32
+ assert c.data["Idle"]
33
+ end
34
+
35
+
36
+ # First run we get size info
37
+ def test_disk
38
+ c = ServerMetrics::Disk.new()
39
+ fixture = fixtures(:disk)
40
+ c.expects(:`).with("mount").returns(fixture.command("mount")).once
41
+ c.expects(:`).with("df -h").returns(fixture.command("df -h")).once
42
+ c.expects(:`).with("cat /proc/diskstats").returns(fixture.command("cat /proc/diskstats")).once
43
+ c.run
44
+
45
+ assert_equal ["/dev/xvda1"], c.data.keys
46
+ res = c.data["/dev/xvda1"]
47
+
48
+ assert res["Avail"]
49
+ assert res["Filesystem"]
50
+ assert res["Mounted on"]
51
+ assert res["Size"]
52
+ assert res["Use%"]
53
+ assert res["Used"]
54
+ assert_equal 6, res.keys.size
55
+ c
56
+ end
57
+
58
+ # Second run we also get counter data
59
+ def test_disk_second_run
60
+ c=test_disk
61
+ fixture = fixtures(:disk)
62
+ c.expects(:`).with("mount").returns(fixture.command("mount")).once
63
+ c.expects(:`).with("df -h").returns(fixture.command("df -h")).once
64
+ c.expects(:`).with("cat /proc/diskstats").returns(fixture.command("cat /proc/diskstats", "ubuntu second run")).once
65
+
66
+ Timecop.travel(60) do
67
+ c.run
68
+ end
69
+
70
+ assert_equal ["/dev/xvda1"], c.data.keys
71
+ res = c.data["/dev/xvda1"]
72
+ assert res["WPS"]
73
+ assert res["RPS"]
74
+ end
75
+
76
+ def test_memory
77
+ fixture = fixtures(:memory)
78
+ c = ServerMetrics::Memory.new()
79
+ c.expects(:`).with("uname").returns("Linux").times(2)
80
+ c.expects(:`).with("cat /proc/meminfo").returns(fixture.command("cat /proc/meminfo")).once
81
+ c.run
82
+ assert_equal 7, c.data.keys.size
83
+ assert c.data['Swap total']
84
+ end
85
+
86
+ def test_network
87
+ fixture = fixtures(:network)
88
+ c = ServerMetrics::Network.new()
89
+ c.expects(:`).with("cat /proc/net/dev").returns(fixture.command("cat /proc/net/dev")).once
90
+ c.run
91
+
92
+ c.expects(:`).with("cat /proc/net/dev").returns(fixture.command("cat /proc/net/dev", "second run")).once
93
+ Timecop.travel(60) do
94
+ c.run
95
+ end
96
+
97
+ assert c.data.keys.include?("eth0")
98
+ assert c.data.keys.include?("eth1")
99
+ assert_equal 2, c.data.keys.size
100
+
101
+ assert c.data["eth0"]["Bytes in"]
102
+ assert c.data["eth1"]["Bytes in"]
103
+ end
104
+ end
105
+
metadata ADDED
@@ -0,0 +1,193 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: server_metrics
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andre Lewis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sys-proctable
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: awesome_print
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: pry
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: timecop
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: mocha
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ description: Collect information about disks, memory, CPU, etc
127
+ email:
128
+ - andre@scoutapp.com
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - .gitignore
134
+ - Gemfile
135
+ - LICENSE.txt
136
+ - README.md
137
+ - Rakefile
138
+ - example_processes.rb
139
+ - example_usage.rb
140
+ - lib/server_metrics.rb
141
+ - lib/server_metrics/collector.rb
142
+ - lib/server_metrics/collectors/cpu.rb
143
+ - lib/server_metrics/collectors/disk.rb
144
+ - lib/server_metrics/collectors/memory.rb
145
+ - lib/server_metrics/collectors/network.rb
146
+ - lib/server_metrics/collectors/processes.rb
147
+ - lib/server_metrics/multi_collector.rb
148
+ - lib/server_metrics/system_info.rb
149
+ - lib/server_metrics/version.rb
150
+ - server_metrics.gemspec
151
+ - test/fixtures/cpu.txt
152
+ - test/fixtures/disk.txt
153
+ - test/fixtures/memory.txt
154
+ - test/fixtures/network.txt
155
+ - test/test.rb
156
+ - test/test_basics.rb
157
+ - test/test_helper.rb
158
+ - test/test_with_fixtures.rb
159
+ homepage: http://scoutapp.com
160
+ licenses:
161
+ - MIT
162
+ post_install_message:
163
+ rdoc_options: []
164
+ require_paths:
165
+ - lib
166
+ required_ruby_version: !ruby/object:Gem::Requirement
167
+ none: false
168
+ requirements:
169
+ - - ! '>='
170
+ - !ruby/object:Gem::Version
171
+ version: '0'
172
+ required_rubygems_version: !ruby/object:Gem::Requirement
173
+ none: false
174
+ requirements:
175
+ - - ! '>='
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ requirements: []
179
+ rubyforge_project:
180
+ rubygems_version: 1.8.25
181
+ signing_key:
182
+ specification_version: 3
183
+ summary: For use with the Scout agent
184
+ test_files:
185
+ - test/fixtures/cpu.txt
186
+ - test/fixtures/disk.txt
187
+ - test/fixtures/memory.txt
188
+ - test/fixtures/network.txt
189
+ - test/test.rb
190
+ - test/test_basics.rb
191
+ - test/test_helper.rb
192
+ - test/test_with_fixtures.rb
193
+ has_rdoc: