monitoring-client 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile.lock CHANGED
@@ -1,16 +1,26 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- monitoring (0.0.1)
4
+ monitoring-client (0.2.1)
5
5
  json
6
6
 
7
7
  GEM
8
8
  remote: http://rubygems.org/
9
9
  specs:
10
+ diff-lcs (1.1.2)
10
11
  json (1.5.1)
12
+ rspec (2.6.0)
13
+ rspec-core (~> 2.6.0)
14
+ rspec-expectations (~> 2.6.0)
15
+ rspec-mocks (~> 2.6.0)
16
+ rspec-core (2.6.0)
17
+ rspec-expectations (2.6.0)
18
+ diff-lcs (~> 1.1.2)
19
+ rspec-mocks (2.6.0)
11
20
 
12
21
  PLATFORMS
13
22
  ruby
14
23
 
15
24
  DEPENDENCIES
16
- monitoring!
25
+ monitoring-client!
26
+ rspec
data/Rakefile CHANGED
@@ -1,2 +1,8 @@
1
1
  require 'bundler'
2
2
  Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ desc "Run specs"
6
+ RSpec::Core::RakeTask.new do |t|
7
+ t.rspec_opts = %w(-fs --color)
8
+ end
data/bin/monitoringd CHANGED
@@ -110,7 +110,8 @@ module Monitoring
110
110
  post_samples(uri, samples_to_post) unless samples_to_post.empty?
111
111
  Kernel.sleep(@sleep_period)
112
112
  end
113
- #server.join()
113
+
114
+ server.join()
114
115
  end
115
116
 
116
117
  def daemonize()
@@ -146,7 +147,7 @@ module Monitoring
146
147
  JSON.parse(line).each do |sample|
147
148
  @samples.push(sample)
148
149
  end
149
- rescue Exception => e;
150
+ rescue StandardError => e
150
151
  STDOUT.puts("Could not parse " + line.inspect()) rescue nil
151
152
  STDERR.puts([[e.class.name, e.message].join(": "),
152
153
  e.backtrace.join("\n")].join("\n")) rescue nil
@@ -162,8 +163,8 @@ module Monitoring
162
163
  STDOUT.puts("Could not post sample (will requeue): " + samples.inspect()) rescue nil
163
164
  STDERR.puts([[e.class.name, e.message].join(": "),
164
165
  e.backtrace.join("\n")].join("\n")) rescue nil
165
- samples.each do |s| @samples.push(s) end
166
- rescue Exception => e
166
+ samples.each { |s| @samples.push(s) }
167
+ rescue StandardError => e
167
168
  STDOUT.puts("Could not post sample (will not requeue): " + samples.inspect()) rescue nil
168
169
  STDERR.puts([[e.class.name, e.message].join(": "),
169
170
  e.backtrace.join("\n")].join("\n")) rescue nil
@@ -0,0 +1,26 @@
1
+ module Monitoring
2
+ module Client
3
+ class Compiler
4
+
5
+ class << self
6
+
7
+ def compile(profiler, routine)
8
+ decorations = profiler.decorations.reject { |k, v| v.nil? }
9
+ now = Time.now()
10
+ routine.metrics.inject([]) do |compilation, metric_tuple|
11
+ metric_name, metric = metric_tuple
12
+ compilation.push({
13
+ "program_name" => profiler.program_name,
14
+ "routine_name" => routine.name,
15
+ "metric_name" => metric_name,
16
+ "metric_units" => metric.units,
17
+ "sample_value" => metric.value,
18
+ "sampled_at" => now,
19
+ "decorations" => decorations,
20
+ })
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,41 @@
1
+ module Monitoring
2
+ module Client
3
+ class Config
4
+
5
+ require "yaml"
6
+
7
+ class << self
8
+
9
+ def global
10
+ @@global ||= Config.new
11
+ end
12
+ end
13
+
14
+ DEFAULT_FILENAME = "/usr/local/etc/monitoring-client.yml"
15
+
16
+ attr_accessor :hostname, :hostgroup
17
+
18
+ def initialize
19
+ self.hostname = get_hostname
20
+ self.hostgroup = nil
21
+ load()
22
+ end
23
+
24
+
25
+ def load(filename = DEFAULT_FILENAME)
26
+ c = YAML.load_file(filename)
27
+ self.hostname = c[:hostname]
28
+ self.hostgroup = c[:hostgroup]
29
+ self
30
+ rescue Errno::ENOENT => e
31
+ # Squelch missing file if default
32
+ raise(e) unless filename == DEFAULT_FILENAME
33
+ end
34
+
35
+ def get_hostname
36
+ %x(hostname).strip
37
+ end
38
+
39
+ end
40
+ end
41
+ end
@@ -4,20 +4,42 @@ module Monitoring
4
4
 
5
5
  require "json"
6
6
  require "socket"
7
+ require "monitoring/client/config"
8
+ require "monitoring/client/compiler"
7
9
  require "monitoring/client/routine"
8
-
9
10
 
10
- def initialize(program_name)
11
- @program_name = program_name
11
+ attr_accessor :config, :program_name, :decorations
12
+
13
+ def initialize(program_name, decorations = {})
14
+ self.config = Config.global
15
+ self.program_name = program_name
16
+ self.decorations = default_decorations.merge(decorations)
12
17
  end
13
18
 
14
19
 
15
20
  def routine(routine_name)
16
21
  routine = Routine.new(routine_name)
17
22
  yield(routine)
18
- UDPSocket.new().send(routine.compile(@program_name).to_json + "\n",
23
+ compiled = compile(routine)
24
+ post_metrics(compiled)
25
+ routine
26
+ end
27
+
28
+ def compile(routine)
29
+ Compiler.compile(self, routine)
30
+ end
31
+
32
+ def post_metrics(compiled)
33
+ UDPSocket.new().send(compiled.to_json + "\n",
19
34
  0, "localhost", 57475)
20
35
  end
36
+
37
+ def default_decorations
38
+ {
39
+ :hostname => config.hostname,
40
+ :hostgroup => config.hostgroup,
41
+ }
42
+ end
21
43
 
22
44
  end
23
45
  end
@@ -4,9 +4,10 @@ module Monitoring
4
4
 
5
5
  require "monitoring/client/metric"
6
6
 
7
+ attr_reader :name, :metrics
7
8
 
8
- def initialize(routine_name)
9
- @routine_name = routine_name
9
+ def initialize(name)
10
+ @name = name
10
11
  @metrics = Hash.new do |h, k| h[k] = Metric.new(k) end
11
12
  end
12
13
 
@@ -18,25 +19,10 @@ module Monitoring
18
19
  @metrics[metric_name].units = "s"
19
20
  end
20
21
 
21
- def count(metric_name, increment_by, units = nil)
22
+ def count(metric_name, increment_by = 1, units = nil)
22
23
  @metrics[metric_name].value = (@metrics[metric_name].value || 0) + increment_by
23
24
  @metrics[metric_name].units = units
24
- end
25
-
26
- def compile(program_name)
27
- @metrics.inject([]) do |compilation, metric_tuple|
28
- metric_name, metric = metric_tuple
29
- compilation.push({
30
- "program_name" => program_name,
31
- "routine_name" => @routine_name,
32
- "metric_name" => metric_name,
33
- "metric_units" => metric.units,
34
- "sample_value" => metric.value,
35
- "sampled_at" => Time.now(),
36
- })
37
- end
38
- end
39
-
25
+ end
40
26
  end
41
27
  end
42
28
  end
@@ -1,5 +1,5 @@
1
1
  module Monitoring
2
2
  module Client
3
- VERSION = "0.2.1"
3
+ VERSION = "0.2.2"
4
4
  end
5
5
  end
@@ -20,4 +20,5 @@ Gem::Specification.new do |s|
20
20
  s.require_paths = ["lib"]
21
21
 
22
22
  s.add_runtime_dependency "json"
23
+ s.add_development_dependency "rspec"
23
24
  end
@@ -0,0 +1,70 @@
1
+ require "spec_helper"
2
+
3
+ require "monitoring/client/profiler"
4
+
5
+ module Monitoring::Client
6
+ describe Compiler do
7
+
8
+ def mk_metrics(n)
9
+ metrics = {}
10
+ n.times do |i|
11
+ name = "metric-#{i+1}"
12
+ metric = double(name)
13
+ metric.stub(:value).
14
+ and_return(i+1)
15
+ metric.stub(:units).
16
+ and_return("units-#{i+1}")
17
+ metrics[name] = metric
18
+ end
19
+ metrics
20
+ end
21
+
22
+ it "should compile properly" do
23
+ profiler = double("profiler")
24
+ profiler.stub(:name).
25
+ and_return("sample-profiler")
26
+ profiler.stub(:decorations).
27
+ and_return({:foo => :bar})
28
+ routine = double("routine")
29
+ routine.stub(:name).
30
+ and_return("sample-routine")
31
+ routine.stub(:metrics).
32
+ and_return(mk_metrics(3))
33
+
34
+ now = Time.now
35
+ Time.stub(:now).
36
+ and_return(now)
37
+
38
+ expected = [
39
+ {
40
+ "program_name" => "sample-profiler",
41
+ "routine_name" => "sample-routine",
42
+ "metric_name" => "metric-1",
43
+ "metric_units" => "units-1",
44
+ "sample_value" => 1,
45
+ "sampled_at" => now,
46
+ "decorations" => {:foo => :bar},
47
+ },
48
+ {
49
+ "program_name" => "sample-profiler",
50
+ "routine_name" => "sample-routine",
51
+ "metric_name" => "metric-2",
52
+ "metric_units" => "units-2",
53
+ "sample_value" => 2,
54
+ "sampled_at" => now,
55
+ "decorations" => {:foo => :bar},
56
+ },
57
+ {
58
+ "program_name" => "sample-profiler",
59
+ "routine_name" => "sample-routine",
60
+ "metric_name" => "metric-3",
61
+ "metric_units" => "units-3",
62
+ "sample_value" => 3,
63
+ "sampled_at" => now,
64
+ "decorations" => {:foo => :bar},
65
+ },
66
+ ]
67
+ Compiler.compile(profiler, routine).should eq(expected)
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,12 @@
1
+ require "spec_helper"
2
+
3
+ require "monitoring/client/config"
4
+
5
+ module Monitoring::Client
6
+ describe Config do
7
+
8
+ it "should load the hostname" do
9
+ Config.global.hostname.should == %x(hostname).strip
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,31 @@
1
+ require "spec_helper"
2
+
3
+ require "monitoring/client/profiler"
4
+
5
+ module Monitoring::Client
6
+ describe Profiler do
7
+
8
+ it "should post metrics" do
9
+ p = Profiler.new("program_name")
10
+ p.decorations.should be_a Hash
11
+ p.decorations.should include :hostname
12
+ routine = double("routine")
13
+ Routine.stub(:new).
14
+ and_return(routine)
15
+ compiled_routine = double("compiled_routine")
16
+ p.should_receive(:compile).
17
+ with(routine).
18
+ and_return(compiled_routine)
19
+ p.should_receive(:post_metrics).
20
+ with(compiled_routine).
21
+ and_return(true)
22
+ p.routine("routine_name") {}
23
+ end
24
+
25
+ it "should decorate with the hostname" do
26
+ p = Profiler.new("program_name")
27
+ p.decorations.should include(:hostname)
28
+ p.decorations[:hostname].should eq(Config.global.hostname)
29
+ end
30
+ end
31
+ end
File without changes
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: monitoring-client
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 1
10
- version: 0.2.1
9
+ - 2
10
+ version: 0.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Marc Bowes
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-13 00:00:00 +02:00
18
+ date: 2011-08-27 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -32,6 +32,20 @@ dependencies:
32
32
  version: "0"
33
33
  type: :runtime
34
34
  version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
35
49
  description: Ruby client which uses UDP to communicate with a daemon which uses HTTP POST to communicate with a Web server
36
50
  email:
37
51
  - marcbowes+monitoring+client@gmail.com
@@ -48,11 +62,17 @@ files:
48
62
  - Rakefile
49
63
  - bin/monitoringd
50
64
  - lib/monitoring.rb
65
+ - lib/monitoring/client/compiler.rb
66
+ - lib/monitoring/client/config.rb
51
67
  - lib/monitoring/client/metric.rb
52
68
  - lib/monitoring/client/profiler.rb
53
69
  - lib/monitoring/client/routine.rb
54
70
  - lib/monitoring/client/version.rb
55
71
  - monitoring-client.gemspec
72
+ - spec/compiler_spec.rb
73
+ - spec/config_spec.rb
74
+ - spec/profiler_spec.rb
75
+ - spec/spec_helper.rb
56
76
  has_rdoc: true
57
77
  homepage: http://rubygems.org/gems/monitoring-client
58
78
  licenses: []
@@ -83,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
103
  requirements: []
84
104
 
85
105
  rubyforge_project: monitoring-client
86
- rubygems_version: 1.5.0
106
+ rubygems_version: 1.6.2
87
107
  signing_key:
88
108
  specification_version: 3
89
109
  summary: Simple client & daemon to post metrics