server_metrics 1.2.3 → 1.2.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -6,6 +6,7 @@
6
6
  Gemfile.lock
7
7
  InstalledFiles
8
8
  _yardoc
9
+ bin
9
10
  coverage
10
11
  doc/
11
12
  lib/bundler/man
@@ -15,5 +16,6 @@ spec/reports
15
16
  test/tmp
16
17
  test/version_tmp
17
18
  tmp
19
+ vendor
18
20
  .idea
19
21
  .ruby-version
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 1.2.4
2
+
3
+ * Collecting OSX disk capacity metrics
4
+
1
5
  ## 1.2.3
2
6
 
3
7
  * Skipping mapped devices in disk collector
@@ -136,12 +136,18 @@ module ServerMetrics
136
136
  end
137
137
 
138
138
  def linux?
139
- RbConfig::CONFIG['target_os'].start_with?('linux')
139
+ ruby_config['target_os'].start_with?('linux')
140
140
  end
141
141
 
142
142
  def osx?
143
- RbConfig::CONFIG['target_os'] == 'darwin'
143
+ ruby_config['target_os'].start_with?('darwin')
144
144
  end
145
145
 
146
+ private
147
+
148
+ # Returns hash of Ruby config parameters.
149
+ def ruby_config
150
+ RbConfig::CONFIG
151
+ end
146
152
  end
147
153
  end
@@ -10,8 +10,6 @@ class ServerMetrics::Disk < ServerMetrics::MultiCollector
10
10
  ENV['LC_ALL'] = 'C'
11
11
  ENV['LANG'] = 'C'
12
12
 
13
- @disk_stats = File.read("/proc/diskstats").lines.to_a
14
-
15
13
  devices.each do |device|
16
14
  get_sizes(device) # does its own reporting
17
15
  get_io_stats(device) if linux? # does its own reporting
@@ -103,15 +101,15 @@ class ServerMetrics::Disk < ServerMetrics::MultiCollector
103
101
  # match /proc/diskstats output.
104
102
  def iostat(dev)
105
103
 
106
- # if this is a mapped device, translate it into the mapped name for lookup in @disk_stats
104
+ # if this is a mapped device, translate it into the mapped name for lookup in disk_stats
107
105
  if dev =~ %r(^/dev/mapper/)
108
106
  name_to_find = File.readlink(dev).split("/").last rescue dev
109
107
  else
110
108
  name_to_find = dev
111
109
  end
112
110
 
113
- # narrow our @disk_stats down to a list of possible devices
114
- possible_devices = @disk_stats.map { |line|
111
+ # narrow our disk_stats down to a list of possible devices
112
+ possible_devices = disk_stats.map { |line|
115
113
  Hash[*COLUMNS.zip(line.strip.split(/\s+/).collect { |v| Integer(v) rescue v }).flatten]
116
114
  }.select{|entry| name_to_find.include?(entry['name']) }
117
115
 
@@ -119,6 +117,13 @@ class ServerMetrics::Disk < ServerMetrics::MultiCollector
119
117
  return possible_devices.find { |entry| name_to_find == entry['name'] } || possible_devices.first
120
118
  end
121
119
 
120
+ # Returns /proc/diskstats as array.
121
+ def disk_stats
122
+ File.readlines("/proc/diskstats")
123
+ rescue Errno::ENOENT # Handle missing /proc/diskstats, i.e. on Mac OS X.
124
+ []
125
+ end
126
+
122
127
  def normalize_key(key)
123
128
  key = "Used Percent" if /capacity|use.*%|%.*use/i === key
124
129
  key.downcase.gsub(" ", "_").to_sym
@@ -3,7 +3,6 @@ require "server_metrics/system_info"
3
3
  class ServerMetrics::Network < ServerMetrics::MultiCollector
4
4
 
5
5
  def build_report
6
-
7
6
  if linux?
8
7
  lines = File.read("/proc/net/dev").lines.to_a[2..-1]
9
8
  interfaces = []
@@ -1,3 +1,3 @@
1
1
  module ServerMetrics
2
- VERSION = "1.2.3"
2
+ VERSION = '1.2.4'
3
3
  end
@@ -24,4 +24,5 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "pry"
25
25
  spec.add_development_dependency "timecop"
26
26
  spec.add_development_dependency "mocha"
27
+ spec.add_development_dependency "rspec", "~> 2.14"
27
28
  end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe ServerMetrics::Collector do
4
+ let(:collector) { ServerMetrics::Collector.new }
5
+
6
+ describe '#linux?' do
7
+ it 'is true when target OS starts with linux' do
8
+ collector.should_receive(:ruby_config).
9
+ and_return( {'target_os' => 'linux42.0'} )
10
+ expect(collector.linux?).to be true
11
+ end
12
+
13
+ it 'is false when target OS does not start with linux' do
14
+ collector.should_receive(:ruby_config).
15
+ and_return( {'target_os' => 'chunkybacon'} )
16
+ expect(collector.linux?).to be false
17
+ end
18
+ end
19
+
20
+ describe '#osx?' do
21
+ it 'is true when target OS starts with darwin' do
22
+ collector.should_receive(:ruby_config).
23
+ and_return( {'target_os' => 'darwin42.0'} )
24
+ expect(collector.osx?).to be true
25
+ end
26
+
27
+ it 'is false when target OS does not start with darwin' do
28
+ collector.should_receive(:ruby_config).
29
+ and_return( {'target_os' => 'chunkybacon'} )
30
+ expect(collector.osx?).to be false
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe ServerMetrics::Disk do
4
+ let(:disk) { ServerMetrics::Disk.new }
5
+
6
+ describe '#iostat' do
7
+ it 'uses Disk#diskstats instead @disk_stats' do
8
+ disk.should_receive(:disk_stats).and_return( [] )
9
+ expect {
10
+ disk.send(:iostat, '/dev/chunky')
11
+ }.to_not raise_error
12
+ end
13
+ end
14
+
15
+ describe '#disk_stats' do
16
+ it 'returns lines from /proc/diskstats as array' do
17
+ File.should_receive(:readlines).with('/proc/diskstats').
18
+ and_return( %w(chunky bacon) )
19
+ expect(disk.send(:disk_stats)).to eq %w(chunky bacon)
20
+ end
21
+
22
+ it 'returns empty array when /proc/diskstats is missing' do
23
+ File.stub(:readlines).and_raise(Errno::ENOENT)
24
+ expect(disk.send(:disk_stats)).to eq []
25
+ end
26
+ end
27
+ end
@@ -0,0 +1 @@
1
+ require 'server_metrics'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: server_metrics
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 1.2.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2014-03-14 00:00:00.000000000 Z
14
+ date: 2014-04-03 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: bundler
@@ -109,6 +109,22 @@ dependencies:
109
109
  - - ! '>='
110
110
  - !ruby/object:Gem::Version
111
111
  version: '0'
112
+ - !ruby/object:Gem::Dependency
113
+ name: rspec
114
+ requirement: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ~>
118
+ - !ruby/object:Gem::Version
119
+ version: '2.14'
120
+ type: :development
121
+ prerelease: false
122
+ version_requirements: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ~>
126
+ - !ruby/object:Gem::Version
127
+ version: '2.14'
112
128
  description: Collect information about disks, memory, CPU, networks, and processes
113
129
  email:
114
130
  - support@scoutapp.com
@@ -136,6 +152,9 @@ files:
136
152
  - lib/server_metrics/system_info.rb
137
153
  - lib/server_metrics/version.rb
138
154
  - server_metrics.gemspec
155
+ - spec/server_metrics/collector_spec.rb
156
+ - spec/server_metrics/collectors/disk_spec.rb
157
+ - spec/spec_helper.rb
139
158
  - test/fixtures/cpu.txt
140
159
  - test/fixtures/disk.txt
141
160
  - test/fixtures/memory.txt
@@ -171,6 +190,9 @@ signing_key:
171
190
  specification_version: 3
172
191
  summary: Used by the Scout agent, but also available as a stand-lone gem
173
192
  test_files:
193
+ - spec/server_metrics/collector_spec.rb
194
+ - spec/server_metrics/collectors/disk_spec.rb
195
+ - spec/spec_helper.rb
174
196
  - test/fixtures/cpu.txt
175
197
  - test/fixtures/disk.txt
176
198
  - test/fixtures/memory.txt