lib/mongodb/graphite/agent.rb in mongodb-graphite-agent-0.0.3 vs lib/mongodb/graphite/agent.rb in mongodb-graphite-agent-0.0.4

- old
+ new

@@ -10,18 +10,20 @@ require 'mongodb/graphite/agent/op_counters_sample' module Mongodb module Graphite module Agent - def self.run - @connection = Mongo::MongoClient.new("localhost", 27017, :slave_ok => true) + def self.run(opts) + @connection = Mongo::MongoClient.new(opts.mongodb_host, opts.mongodb_port, :slave_ok => true) + unless(opts[:mongodb_username].blank? && opts[:mongodb_password].blank?) + @connection["admin"].authenticate(opts.mongodb_username, opts.mongodb_password) + end @hash = @connection["local"].command('serverStatus' => 1) + @graphite_writer = GraphiteWriter.new(opts[:graphite_host], opts[:graphite_port], opts[:verbose]) - @g = ::Graphite.new({:host => "localhost", :port => 2003}) - #puts @hash["connections"]["current"] #puts @hash["connections"]["available"] # #puts @hash["backgroundFlushing"]["average_ms"] # @@ -35,13 +37,10 @@ #puts @hash["indexCounters"]["missRatio"] @asd = Utils.to_hash(@hash) #all metrics -#ap(@asd) - - @current_sample = OpCountersSample.new Hash[@hash["opcounters"]] @previous_sample = @current_sample.dup if File.exist? 'lastsample' File.open('lastsample', 'r') do |file| @@ -49,25 +48,28 @@ #puts "Loaded object" #ap(@previous_sample) end end - puts "Delta: " @delta = TimeDifference.between(Time.parse(@current_sample.sample_time), Time.parse(@previous_sample.sample_time)) - puts @delta.in_seconds + puts "Last sample was taken #{@delta.in_seconds.round(0)} seconds ago" if opts[:verbose] @previous_sample.values.keys.sort.each do |k| previous_sample_value = @previous_sample.values[k] current_sample_value = @current_sample.values[k] value_per_seconds = ((current_sample_value - previous_sample_value) / @delta.in_seconds).round(2) - puts "#{k}: #{previous_sample_value} / #{current_sample_value}: #{value_per_seconds}/s" + puts "#{k}: #{previous_sample_value} / #{current_sample_value}: #{value_per_seconds}/s" if opts[:verbose] end File.open('lastsample', 'w') do |file| Marshal.dump(@current_sample, file) end - #@g.send_metrics({"#{Socket.gethostname}.connections.current" => @hash["connections"]["current"]}) + @graphite_writer.write( @asd.select {|k| k.match('^connection|^network\.|^cursors|^mem\.mapped|^indexCounters|^repl.oplog') } ) + #@graphite_writer.write("connections.current" => @hash["connections"]["current"]) unless(opts[:dry_run]) end end end end +class GraphiteWriter + +end \ No newline at end of file