mongodb-graphite-agent 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,19 +1,20 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
- Gemfile.lock
7
- InstalledFiles
8
- _yardoc
9
- coverage
10
- doc/
11
- lib/bundler/man
12
- pkg
13
- rdoc
14
- spec/reports
15
- test/tmp
16
- test/version_tmp
17
- tmp
18
- .idea/*
19
- lastsample
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea/*
19
+ lastsample
20
+ *.iml
data/Gemfile CHANGED
@@ -1,3 +1,3 @@
1
- source 'https://rubygems.org'
2
-
1
+ source 'https://rubygems.org'
2
+
3
3
  gemspec
@@ -1,27 +1,29 @@
1
- #!/usr/bin/env ruby
2
-
3
- $:.unshift File.expand_path("#{File.dirname(__FILE__)}/../lib")
4
-
5
- require 'trollop'
6
- require 'mongodb/graphite/agent'
7
- require 'mongodb/graphite/agent/graphite_writer'
8
-
9
- opts = Trollop::options do
10
- opt :mongodb_username, "MongoDB username", :type => :string
11
- opt :mongodb_host, "MongoDB host", :type => :string, :default => "localhost"
12
- opt :mongodb_post, "MongoDB host", :type => :int, :default => 27017
13
- opt :mongodb_password, "MongoDB password", :type => :string
14
- opt :graphite_host, "Graphite host", :type => :string
15
- opt :graphite_port, "Graphite port", :type => :string
16
- opt :dry_run, "Dry run", :type => :boolean, :default => false
17
- opt :verbose, "Verbose", :type => :boolean, :default => false
18
- end
19
-
20
- if opts[:dry_run]
21
- puts "\n\nWARNING!!! This is a dry run\n\n\n"
22
- sleep 1
23
- end
24
-
25
- #@writer = Mongodb::Graphite::Agent::GraphiteWriter.new("localhost", 12)
26
- #@writer.write ({ "antani" => 5, "boh" => 1 })
27
- Mongodb::Graphite::Agent.run(opts)
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift File.expand_path("#{File.dirname(__FILE__)}/../lib")
4
+
5
+ require 'trollop'
6
+ require 'mongodb/graphite/agent'
7
+ require 'mongodb/graphite/agent/graphite_writer'
8
+
9
+ opts = Trollop::options do
10
+ opt :mongodb_username, "MongoDB username", :type => :string
11
+ opt :mongodb_host, "MongoDB host", :type => :string, :default => "localhost"
12
+ opt :mongodb_post, "MongoDB host", :type => :int, :default => 27017
13
+ opt :mongodb_password, "MongoDB password", :type => :string
14
+ opt :graphite_host, "Graphite host", :type => :string
15
+ opt :graphite_port, "Graphite port", :type => :string
16
+ opt :dry_run, "Dry run", :type => :boolean, :default => false
17
+ opt :verbose, "Verbose", :type => :boolean, :default => false
18
+ end
19
+
20
+ if opts[:dry_run]
21
+ puts "\n\nWARNING!!! This is a dry run\n\n\n"
22
+ sleep 1
23
+ end
24
+
25
+ Trollop::die :mongodb_host, "must be specified" unless opts[:mongodb_host]
26
+ Trollop::die :graphite_host, "(or --dry-run) must be specified " if (opts[:graphite_host].blank? and opts[:dry_run].blank?)
27
+
28
+ runner = Mongodb::Graphite::Agent::Runner.new(opts)
29
+ runner.run
@@ -1,33 +1,21 @@
1
-
2
-
3
- class Hash
4
- def hash_map
5
- self.inject({}) do |newhash, (k,v)|
6
- newhash[k] = yield(k, v)
7
- newhash
8
- end
9
- end
10
- end
11
-
12
- module Mongodb
13
- module Graphite
14
- module Agent
15
- class GraphiteWriter
16
- def initialize(host, port, verbose)
17
- @graphite = ::Graphite.new({:host => host, :port => port})
18
- @verbose = verbose
19
- end
20
-
21
- def write(metric_hash)
22
- puts "Sending data to graphite" if @verbose
23
- ap metric_hash if @verbose
24
- @metric_hash_with_hostname = metric_hash.map { |k, v| { "#{Socket.gethostname}.#{k}" => v } }.reduce Hash.new(), :merge
25
- @graphite.send_metrics @metric_hash_with_hostname
26
- #ap metric_hash.hash_map { |k, v| { "#{Socket.gethostname}.#{k}" => v }}
27
-
28
- #@graphite.send_metrics({"#{Socket.gethostname}.#{metric}" => value})
29
- end
30
- end
31
- end
32
- end
33
- end
1
+ module Mongodb
2
+ module Graphite
3
+ module Agent
4
+ class GraphiteWriter
5
+ def initialize(host, port, verbose)
6
+ @graphite = ::Graphite.new({:host => host, :port => port})
7
+ @verbose = verbose
8
+ end
9
+
10
+ def write(metric_hash)
11
+ @metric_hash_with_hostname = Hash[metric_hash.map { |k,v| ["#{Socket.gethostname}.mongodb.#{k}", v]}]
12
+ if @verbose
13
+ puts "Sending data to graphite..."
14
+ ap @metric_hash_with_hostname
15
+ end
16
+ @graphite.send_metrics @metric_hash_with_hostname
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,26 +1,26 @@
1
- require 'date'
2
-
3
- module Mongodb
4
- module Graphite
5
- module Agent
6
-
7
- class OpCountersSample
8
- attr_reader :values, :sample_time
9
-
10
- def initialize(values, sample_time = DateTime.now.to_s)
11
- @values = values
12
- @sample_time = sample_time
13
- end
14
-
15
- def marshal_dump
16
- [@sample_time, @values]
17
- end
18
-
19
- def marshal_load array
20
- @sample_time, @values = array
21
- end
22
-
23
- end
24
- end
25
- end
1
+ require 'date'
2
+
3
+ module Mongodb
4
+ module Graphite
5
+ module Agent
6
+
7
+ class OpCountersSample
8
+ attr_reader :values, :sample_time
9
+
10
+ def initialize(values, sample_time = DateTime.now.to_s)
11
+ @values = values
12
+ @sample_time = sample_time
13
+ end
14
+
15
+ def marshal_dump
16
+ [@sample_time, @values]
17
+ end
18
+
19
+ def marshal_load array
20
+ @sample_time, @values = array
21
+ end
22
+
23
+ end
24
+ end
25
+ end
26
26
  end
@@ -1,29 +1,29 @@
1
- module Mongodb
2
- module Graphite
3
- module Agent
4
-
5
- module Utils
6
- def self.merge_all
7
- self.inject({}) { |h1, h2|
8
- h1.merge! h2
9
- }
10
- end
11
-
12
- def self.json_descent(pre, json)
13
- json.map do |k, v|
14
- key = pre + [k]
15
- if v.is_a? BSON::OrderedHash
16
- json_descent(key, v)
17
- else
18
- {key.join('.') => v}
19
- end
20
- end
21
- end
22
-
23
- def self.to_hash(s)
24
- json_descent([], s).flatten.reduce Hash.new, :merge
25
- end
26
- end
27
- end
28
- end
29
- end
1
+ module Mongodb
2
+ module Graphite
3
+ module Agent
4
+
5
+ module Utils
6
+ def self.merge_all
7
+ self.inject({}) { |h1, h2|
8
+ h1.merge! h2
9
+ }
10
+ end
11
+
12
+ def self.json_descent(pre, json)
13
+ json.map do |k, v|
14
+ key = pre + [k]
15
+ if v.is_a? BSON::OrderedHash
16
+ json_descent(key, v)
17
+ else
18
+ {key.join('.') => v}
19
+ end
20
+ end
21
+ end
22
+
23
+ def self.to_hash(s)
24
+ json_descent([], s).flatten.reduce Hash.new, :merge
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,7 +1,7 @@
1
- module Mongodb
2
- module Graphite
3
- module Agent
4
- VERSION = "0.0.4"
5
- end
6
- end
7
- end
1
+ module Mongodb
2
+ module Graphite
3
+ module Agent
4
+ VERSION = "0.0.5"
5
+ end
6
+ end
7
+ end
@@ -1,75 +1,79 @@
1
- require 'rubygems'
2
- require 'bundler/setup'
3
- require 'mongo'
4
- require 'simple-graphite'
5
- require 'bson'
6
- require 'socket'
7
- require 'awesome_print'
8
- require 'time_difference'
9
- require 'mongodb/graphite/agent/utils'
10
- require 'mongodb/graphite/agent/op_counters_sample'
11
-
12
- module Mongodb
13
- module Graphite
14
- module Agent
15
- def self.run(opts)
16
- @connection = Mongo::MongoClient.new(opts.mongodb_host, opts.mongodb_port, :slave_ok => true)
17
- unless(opts[:mongodb_username].blank? && opts[:mongodb_password].blank?)
18
- @connection["admin"].authenticate(opts.mongodb_username, opts.mongodb_password)
19
- end
20
-
21
- @hash = @connection["local"].command('serverStatus' => 1)
22
-
23
- @graphite_writer = GraphiteWriter.new(opts[:graphite_host], opts[:graphite_port], opts[:verbose])
24
-
25
- #puts @hash["connections"]["current"]
26
- #puts @hash["connections"]["available"]
27
- #
28
- #puts @hash["backgroundFlushing"]["average_ms"]
29
- #
30
- #puts @hash["network"]["numRequests"]
31
- #puts @hash["network"]["bytesIn"]
32
- #puts @hash["network"]["bytesOut"]
33
- #
34
- #
35
- #puts @hash["cursors"]["totalOpen"]
36
- #
37
- #puts @hash["indexCounters"]["missRatio"]
38
-
39
- @asd = Utils.to_hash(@hash) #all metrics
40
-
41
-
42
- @current_sample = OpCountersSample.new Hash[@hash["opcounters"]]
43
- @previous_sample = @current_sample.dup
44
-
45
- if File.exist? 'lastsample'
46
- File.open('lastsample', 'r') do |file|
47
- @previous_sample = Marshal.load(file)
48
- #puts "Loaded object"
49
- #ap(@previous_sample)
50
- end
51
- end
52
-
53
- @delta = TimeDifference.between(Time.parse(@current_sample.sample_time), Time.parse(@previous_sample.sample_time))
54
- puts "Last sample was taken #{@delta.in_seconds.round(0)} seconds ago" if opts[:verbose]
55
-
56
- @previous_sample.values.keys.sort.each do |k|
57
- previous_sample_value = @previous_sample.values[k]
58
- current_sample_value = @current_sample.values[k]
59
- value_per_seconds = ((current_sample_value - previous_sample_value) / @delta.in_seconds).round(2)
60
- puts "#{k}: #{previous_sample_value} / #{current_sample_value}: #{value_per_seconds}/s" if opts[:verbose]
61
- end
62
-
63
- File.open('lastsample', 'w') do |file|
64
- Marshal.dump(@current_sample, file)
65
- end
66
-
67
- @graphite_writer.write( @asd.select {|k| k.match('^connection|^network\.|^cursors|^mem\.mapped|^indexCounters|^repl.oplog') } )
68
- #@graphite_writer.write("connections.current" => @hash["connections"]["current"]) unless(opts[:dry_run])
69
- end
70
- end
71
- end
72
- end
73
- class GraphiteWriter
74
-
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'mongo'
4
+ require 'simple-graphite'
5
+ require 'bson'
6
+ require 'socket'
7
+ require 'awesome_print'
8
+ require 'time_difference'
9
+ require 'mongodb/graphite/agent/utils'
10
+ require 'mongodb/graphite/agent/op_counters_sample'
11
+
12
+ module Mongodb
13
+ module Graphite
14
+ module Agent
15
+ class Runner
16
+
17
+ def initialize(opts)
18
+ @opts = opts
19
+ end
20
+
21
+ def run
22
+ connection = Mongo::MongoClient.new(@opts.mongodb_host, @opts.mongodb_port, :slave_ok => true)
23
+ unless (@opts[:mongodb_username].blank? && @opts[:mongodb_password].blank?)
24
+ connection["admin"].authenticate(@opts.mongodb_username, @opts.mongodb_password)
25
+ end
26
+
27
+ server_status_result = connection["local"].command('serverStatus' => 1)
28
+ metric_hash = Utils.to_hash(server_status_result).select { |k|
29
+ k.match('^connection|^network\.|^cursors|^mem\.mapped|^indexCounters|^repl.oplog')
30
+ }
31
+
32
+ opcounters_per_second_metric_hash = calculate_opcounters_per_second server_status_result["opcounters"]
33
+
34
+ if @opts[:verbose]
35
+ puts "Calculating metrics..."
36
+ ap metric_hash
37
+ ap opcounters_per_second_metric_hash
38
+ end
39
+
40
+
41
+
42
+ unless (@opts[:dry_run])
43
+ graphite_writer = GraphiteWriter.new(@opts[:graphite_host], @opts[:graphite_port], @opts[:verbose])
44
+ graphite_writer.write(metric_hash)
45
+ graphite_writer.write(opcounters_per_second_metric_hash)
46
+ end
47
+ end
48
+
49
+ def calculate_opcounters_per_second(opcounters)
50
+ current_sample = OpCountersSample.new Hash[opcounters]
51
+ previous_sample = current_sample.dup
52
+ result = {}
53
+
54
+ if File.exist? 'lastsample'
55
+ File.open('lastsample', 'r') do |file|
56
+ previous_sample = Marshal.load(file)
57
+ end
58
+ end
59
+
60
+ delta = TimeDifference.between(Time.parse(current_sample.sample_time), Time.parse(previous_sample.sample_time))
61
+ puts "Last sample was taken #{delta.in_seconds.round(0)} seconds ago"
62
+
63
+ previous_sample.values.keys.sort.each do |k|
64
+ previous_sample_value = previous_sample.values[k]
65
+ current_sample_value = current_sample.values[k]
66
+ value_per_seconds = ((current_sample_value - previous_sample_value) / delta.in_seconds).round(2)
67
+ result["#{k}_per_seconds"] = value_per_seconds
68
+ end
69
+
70
+ File.open('lastsample', 'w') do |file|
71
+ Marshal.dump(current_sample, file)
72
+ end
73
+
74
+ result
75
+ end
76
+ end
77
+ end
78
+ end
75
79
  end
@@ -1,31 +1,31 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'mongodb/graphite/agent/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "mongodb-graphite-agent"
8
- spec.version = Mongodb::Graphite::Agent::VERSION
9
- spec.authors = ["Michele Cantelli"]
10
- spec.email = ["michele.cantelli@jobrapido.com"]
11
- spec.description = ""
12
- spec.summary = ""
13
- spec.homepage = ""
14
- spec.license = "MIT"
15
-
16
- spec.files = `git ls-files`.split($/)
17
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
20
-
21
- spec.add_development_dependency "bundler", "~> 1.3"
22
- spec.add_development_dependency "rake"
23
-
24
- spec.add_dependency 'mongo'
25
- spec.add_dependency 'simple-graphite'
26
- spec.add_dependency 'bson'
27
- spec.add_dependency 'bson_ext'
28
- spec.add_dependency 'awesome_print'
29
- spec.add_dependency 'time_difference'
30
- spec.add_dependency 'trollop'
31
- end
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mongodb/graphite/agent/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mongodb-graphite-agent"
8
+ spec.version = Mongodb::Graphite::Agent::VERSION
9
+ spec.authors = ["Michele Cantelli"]
10
+ spec.email = ["michele.cantelli@jobrapido.com"]
11
+ spec.description = ""
12
+ spec.summary = ""
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_dependency 'mongo'
25
+ spec.add_dependency 'simple-graphite'
26
+ spec.add_dependency 'bson'
27
+ spec.add_dependency 'bson_ext'
28
+ spec.add_dependency 'awesome_print'
29
+ spec.add_dependency 'time_difference'
30
+ spec.add_dependency 'trollop'
31
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongodb-graphite-agent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-03 00:00:00.000000000 Z
12
+ date: 2013-07-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -175,7 +175,6 @@ files:
175
175
  - lib/mongodb/graphite/agent/utils.rb
176
176
  - lib/mongodb/graphite/agent/version.rb
177
177
  - mongodb-graphite-agent.gemspec
178
- - mongodb-graphite-agent.iml
179
178
  homepage: ''
180
179
  licenses:
181
180
  - MIT
@@ -189,21 +188,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
189
188
  - - ! '>='
190
189
  - !ruby/object:Gem::Version
191
190
  version: '0'
192
- segments:
193
- - 0
194
- hash: 461905061788265084
195
191
  required_rubygems_version: !ruby/object:Gem::Requirement
196
192
  none: false
197
193
  requirements:
198
194
  - - ! '>='
199
195
  - !ruby/object:Gem::Version
200
196
  version: '0'
201
- segments:
202
- - 0
203
- hash: 461905061788265084
204
197
  requirements: []
205
198
  rubyforge_project:
206
- rubygems_version: 1.8.25
199
+ rubygems_version: 1.8.24
207
200
  signing_key:
208
201
  specification_version: 3
209
202
  summary: ''
@@ -1,28 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <module type="RUBY_MODULE" version="4">
3
- <component name="NewModuleRootManager" inherit-compiler-output="true">
4
- <exclude-output />
5
- <content url="file://$MODULE_DIR$">
6
- <sourceFolder url="file://$MODULE_DIR$/lib" isTestSource="false" />
7
- </content>
8
- <orderEntry type="inheritedJdk" />
9
- <orderEntry type="sourceFolder" forTests="false" />
10
- <orderEntry type="library" scope="PROVIDED" name="activesupport (v4.0.0, RVM: ruby-1.9.3-p448) [gem]" level="application" />
11
- <orderEntry type="library" scope="PROVIDED" name="atomic (v1.1.10, RVM: ruby-1.9.3-p448) [gem]" level="application" />
12
- <orderEntry type="library" scope="PROVIDED" name="awesome_print (v1.1.0, RVM: ruby-1.9.3-p448) [gem]" level="application" />
13
- <orderEntry type="library" scope="PROVIDED" name="bson (v1.9.0, RVM: ruby-1.9.3-p448) [gem]" level="application" />
14
- <orderEntry type="library" scope="PROVIDED" name="bson_ext (v1.9.0, RVM: ruby-1.9.3-p448) [gem]" level="application" />
15
- <orderEntry type="library" scope="PROVIDED" name="bundler (v1.3.5, RVM: ruby-1.9.3-p448) [gem]" level="application" />
16
- <orderEntry type="library" scope="PROVIDED" name="i18n (v0.6.4, RVM: ruby-1.9.3-p448) [gem]" level="application" />
17
- <orderEntry type="library" scope="PROVIDED" name="minitest (v4.7.5, RVM: ruby-1.9.3-p448) [gem]" level="application" />
18
- <orderEntry type="library" scope="PROVIDED" name="mongo (v1.9.0, RVM: ruby-1.9.3-p448) [gem]" level="application" />
19
- <orderEntry type="library" scope="PROVIDED" name="multi_json (v1.7.7, RVM: ruby-1.9.3-p448) [gem]" level="application" />
20
- <orderEntry type="library" scope="PROVIDED" name="rake (v10.1.0, RVM: ruby-1.9.3-p448) [gem]" level="application" />
21
- <orderEntry type="library" scope="PROVIDED" name="simple-graphite (v2.1.0, RVM: ruby-1.9.3-p448) [gem]" level="application" />
22
- <orderEntry type="library" scope="PROVIDED" name="thread_safe (v0.1.0, RVM: ruby-1.9.3-p448) [gem]" level="application" />
23
- <orderEntry type="library" scope="PROVIDED" name="time_difference (v0.2.0, RVM: ruby-1.9.3-p448) [gem]" level="application" />
24
- <orderEntry type="library" scope="PROVIDED" name="trollop (v2.0, RVM: ruby-1.9.3-p448) [gem]" level="application" />
25
- <orderEntry type="library" scope="PROVIDED" name="tzinfo (v0.3.37, RVM: ruby-1.9.3-p448) [gem]" level="application" />
26
- </component>
27
- </module>
28
-