Class: Mongodb::Graphite::Agent::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/mongodb/graphite/agent.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Runner

Returns a new instance of Runner.



19
20
21
# File 'lib/mongodb/graphite/agent.rb', line 19

def initialize(opts)
  @opts = opts
end

Instance Method Details

#calculate_opcounters_per_second(opcounters) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mongodb/graphite/agent.rb', line 56

def calculate_opcounters_per_second(opcounters)
  current_sample = OpCountersSample.new Hash[opcounters]
  previous_sample = current_sample.dup
  result = {}

  if File.exist? 'lastsample'
    File.open('lastsample', 'r') do |file|
      previous_sample = Marshal.load(file)
    end
  end

  delta = TimeDifference.between(Time.parse(current_sample.sample_time), Time.parse(previous_sample.sample_time))
  puts "Last sample was taken #{delta.in_seconds.round(0)} seconds ago"

  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)
    result["#{k}_per_seconds"] = value_per_seconds
  end

  File.open('lastsample', 'w') do |file|
    Marshal.dump(current_sample, file)
  end

  result
end

#runObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mongodb/graphite/agent.rb', line 23

def run
  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

  server_status_result = connection["local"].command('serverStatus' => 1)
  metric_hash = Utils.to_hash(server_status_result).select { |k|
    k.match('^connection|^network\.|^cursors|^mem\.mapped|^indexCounters|^repl.oplog')
  }

  opcounters_per_second_metric_hash = calculate_opcounters_per_second server_status_result["opcounters"]
  total_collections_hash = CollectionSizeCalculator.new(connection).calculate

  if @opts[:verbose]
    puts "Calculating metrics..."
    ap metric_hash
    ap opcounters_per_second_metric_hash
    ap total_collections_hash
  end


  unless (@opts[:dry_run])
    graphite_writer = GraphiteWriter.new({:host => @opts[:graphite_host],
                                          :port => @opts[:graphite_port],
                                          :verbose => @opts[:verbose],
                                          :metrics_prefix => @opts[:graphite_metrics_prefix]})
    graphite_writer.write(metric_hash)
    graphite_writer.write(opcounters_per_second_metric_hash)
    graphite_writer.write(total_collections_hash)
  end
end