mongodb-graphite-agent 0.0.8 → 0.0.9
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/.gitignore +20 -20
 - data/Gemfile +2 -2
 - data/bin/mongodb-graphite-agent.rb +30 -30
 - data/lib/mongodb/graphite/agent/graphite_writer.rb +21 -21
 - data/lib/mongodb/graphite/agent/op_counters_sample.rb +25 -25
 - data/lib/mongodb/graphite/agent/utils.rb +29 -29
 - data/lib/mongodb/graphite/agent/version.rb +7 -7
 - data/lib/mongodb/graphite/agent.rb +84 -81
 - data/mongodb-graphite-agent.gemspec +31 -31
 - metadata +9 -3
 
    
        data/.gitignore
    CHANGED
    
    | 
         @@ -1,20 +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
         
     | 
| 
       20 
     | 
    
         
            -
            *.iml
         
     | 
| 
      
 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,30 +1,30 @@ 
     | 
|
| 
       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 : 
     | 
| 
       15 
     | 
    
         
            -
              opt : 
     | 
| 
       16 
     | 
    
         
            -
              opt : 
     | 
| 
       17 
     | 
    
         
            -
              opt : 
     | 
| 
       18 
     | 
    
         
            -
              opt : 
     | 
| 
       19 
     | 
    
         
            -
             
     | 
| 
       20 
     | 
    
         
            -
             
     | 
| 
       21 
     | 
    
         
            -
             
     | 
| 
       22 
     | 
    
         
            -
             
     | 
| 
       23 
     | 
    
         
            -
               
     | 
| 
       24 
     | 
    
         
            -
             
     | 
| 
       25 
     | 
    
         
            -
             
     | 
| 
       26 
     | 
    
         
            -
             
     | 
| 
       27 
     | 
    
         
            -
            Trollop::die :graphite_host, "(or --dry-run) must be specified 
     | 
| 
       28 
     | 
    
         
            -
             
     | 
| 
       29 
     | 
    
         
            -
            runner = Mongodb::Graphite::Agent::Runner.new(opts)
         
     | 
| 
       30 
     | 
    
         
            -
            runner.run
         
     | 
| 
      
 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 :mongodb_replicaset, "Set this flag if the host is part of a MongoDB replicaSet", :type => :boolean, :default => false
         
     | 
| 
      
 15 
     | 
    
         
            +
              opt :graphite_host, "Graphite host", :type => :string
         
     | 
| 
      
 16 
     | 
    
         
            +
              opt :graphite_port, "Graphite port", :type => :string
         
     | 
| 
      
 17 
     | 
    
         
            +
              opt :graphite_metrics_prefix, "Graphite metrics prefix", :type => :string, :default => Socket.gethostname
         
     | 
| 
      
 18 
     | 
    
         
            +
              opt :dry_run, "Dry run", :type => :boolean, :default => false
         
     | 
| 
      
 19 
     | 
    
         
            +
              opt :verbose, "Verbose", :type => :boolean, :default => false
         
     | 
| 
      
 20 
     | 
    
         
            +
            end
         
     | 
| 
      
 21 
     | 
    
         
            +
             
     | 
| 
      
 22 
     | 
    
         
            +
            if opts[:dry_run]
         
     | 
| 
      
 23 
     | 
    
         
            +
              puts "\n\nWARNING!!! This is a dry run\n\n\n"
         
     | 
| 
      
 24 
     | 
    
         
            +
              sleep 1
         
     | 
| 
      
 25 
     | 
    
         
            +
            end
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
      
 27 
     | 
    
         
            +
            Trollop::die :graphite_host, "(or --dry-run) must be specified" if (opts[:graphite_host].blank? and opts[:dry_run].blank?)
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
            runner = Mongodb::Graphite::Agent::Runner.new(opts)
         
     | 
| 
      
 30 
     | 
    
         
            +
            runner.run
         
     | 
| 
         @@ -1,21 +1,21 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            module Mongodb
         
     | 
| 
       2 
     | 
    
         
            -
              module Graphite
         
     | 
| 
       3 
     | 
    
         
            -
                module Agent
         
     | 
| 
       4 
     | 
    
         
            -
                  class GraphiteWriter
         
     | 
| 
       5 
     | 
    
         
            -
                    def initialize(opts)
         
     | 
| 
       6 
     | 
    
         
            -
                      @graphite = ::Graphite.new({:host => opts[:host], :port => opts[:port]})
         
     | 
| 
       7 
     | 
    
         
            -
                      @opts = opts
         
     | 
| 
       8 
     | 
    
         
            -
                    end
         
     | 
| 
       9 
     | 
    
         
            -
             
     | 
| 
       10 
     | 
    
         
            -
                    def write(metric_hash)
         
     | 
| 
       11 
     | 
    
         
            -
                      @metric_hash_with_hostname = Hash[metric_hash.map { |k,v| ["#{@opts[:metrics_prefix]}.#{k}", v]}]
         
     | 
| 
       12 
     | 
    
         
            -
                      if @opts[: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 
     | 
    
         
            +
            module Mongodb
         
     | 
| 
      
 2 
     | 
    
         
            +
              module Graphite
         
     | 
| 
      
 3 
     | 
    
         
            +
                module Agent
         
     | 
| 
      
 4 
     | 
    
         
            +
                  class GraphiteWriter
         
     | 
| 
      
 5 
     | 
    
         
            +
                    def initialize(opts)
         
     | 
| 
      
 6 
     | 
    
         
            +
                      @graphite = ::Graphite.new({:host => opts[:host], :port => opts[:port]})
         
     | 
| 
      
 7 
     | 
    
         
            +
                      @opts = opts
         
     | 
| 
      
 8 
     | 
    
         
            +
                    end
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
                    def write(metric_hash)
         
     | 
| 
      
 11 
     | 
    
         
            +
                      @metric_hash_with_hostname = Hash[metric_hash.map { |k,v| ["#{@opts[:metrics_prefix]}.#{k}", v]}]
         
     | 
| 
      
 12 
     | 
    
         
            +
                      if @opts[: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. 
     | 
| 
       5 
     | 
    
         
            -
                end
         
     | 
| 
       6 
     | 
    
         
            -
              end
         
     | 
| 
       7 
     | 
    
         
            -
            end
         
     | 
| 
      
 1 
     | 
    
         
            +
            module Mongodb
         
     | 
| 
      
 2 
     | 
    
         
            +
              module Graphite
         
     | 
| 
      
 3 
     | 
    
         
            +
                module Agent
         
     | 
| 
      
 4 
     | 
    
         
            +
                    VERSION = "0.0.9"
         
     | 
| 
      
 5 
     | 
    
         
            +
                end
         
     | 
| 
      
 6 
     | 
    
         
            +
              end
         
     | 
| 
      
 7 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -1,82 +1,85 @@ 
     | 
|
| 
       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 
     | 
    
         
            -
                       
     | 
| 
       33 
     | 
    
         
            -
             
     | 
| 
       34 
     | 
    
         
            -
                       
     | 
| 
       35 
     | 
    
         
            -
                         
     | 
| 
       36 
     | 
    
         
            -
             
     | 
| 
       37 
     | 
    
         
            -
             
     | 
| 
       38 
     | 
    
         
            -
                       
     | 
| 
       39 
     | 
    
         
            -
             
     | 
| 
       40 
     | 
    
         
            -
             
     | 
| 
       41 
     | 
    
         
            -
             
     | 
| 
       42 
     | 
    
         
            -
                       
     | 
| 
       43 
     | 
    
         
            -
             
     | 
| 
       44 
     | 
    
         
            -
             
     | 
| 
       45 
     | 
    
         
            -
             
     | 
| 
       46 
     | 
    
         
            -
             
     | 
| 
       47 
     | 
    
         
            -
             
     | 
| 
       48 
     | 
    
         
            -
             
     | 
| 
       49 
     | 
    
         
            -
             
     | 
| 
       50 
     | 
    
         
            -
             
     | 
| 
       51 
     | 
    
         
            -
             
     | 
| 
       52 
     | 
    
         
            -
             
     | 
| 
       53 
     | 
    
         
            -
             
     | 
| 
       54 
     | 
    
         
            -
             
     | 
| 
       55 
     | 
    
         
            -
             
     | 
| 
       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 
     | 
    
         
            -
             
     | 
| 
      
 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 
     | 
    
         
            +
                      unless (@opts[:mongodb_replicaset].blank?)
         
     | 
| 
      
 33 
     | 
    
         
            +
                        opcounters_per_second_metric_hash = calculate_opcounters_per_second server_status_result["opcountersRepl"]
         
     | 
| 
      
 34 
     | 
    
         
            +
                      else
         
     | 
| 
      
 35 
     | 
    
         
            +
                        opcounters_per_second_metric_hash = calculate_opcounters_per_second server_status_result["opcounters"]
         
     | 
| 
      
 36 
     | 
    
         
            +
                      end
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
                      if @opts[:verbose]
         
     | 
| 
      
 39 
     | 
    
         
            +
                        puts "Calculating metrics..."
         
     | 
| 
      
 40 
     | 
    
         
            +
                        ap metric_hash
         
     | 
| 
      
 41 
     | 
    
         
            +
                        ap opcounters_per_second_metric_hash
         
     | 
| 
      
 42 
     | 
    
         
            +
                      end
         
     | 
| 
      
 43 
     | 
    
         
            +
             
     | 
| 
      
 44 
     | 
    
         
            +
             
     | 
| 
      
 45 
     | 
    
         
            +
                      unless (@opts[:dry_run])
         
     | 
| 
      
 46 
     | 
    
         
            +
                        graphite_writer = GraphiteWriter.new({:host => @opts[:graphite_host],
         
     | 
| 
      
 47 
     | 
    
         
            +
                                                              :port => @opts[:graphite_port],
         
     | 
| 
      
 48 
     | 
    
         
            +
                                                              :verbose => @opts[:verbose],
         
     | 
| 
      
 49 
     | 
    
         
            +
                                                              :metrics_prefix => @opts[:graphite_metrics_prefix]})
         
     | 
| 
      
 50 
     | 
    
         
            +
                        graphite_writer.write(metric_hash)
         
     | 
| 
      
 51 
     | 
    
         
            +
                        graphite_writer.write(opcounters_per_second_metric_hash)
         
     | 
| 
      
 52 
     | 
    
         
            +
                      end
         
     | 
| 
      
 53 
     | 
    
         
            +
                    end
         
     | 
| 
      
 54 
     | 
    
         
            +
             
     | 
| 
      
 55 
     | 
    
         
            +
                    def calculate_opcounters_per_second(opcounters)
         
     | 
| 
      
 56 
     | 
    
         
            +
                      current_sample = OpCountersSample.new Hash[opcounters]
         
     | 
| 
      
 57 
     | 
    
         
            +
                      previous_sample = current_sample.dup
         
     | 
| 
      
 58 
     | 
    
         
            +
                      result = {}
         
     | 
| 
      
 59 
     | 
    
         
            +
             
     | 
| 
      
 60 
     | 
    
         
            +
                      if File.exist? 'lastsample'
         
     | 
| 
      
 61 
     | 
    
         
            +
                        File.open('lastsample', 'r') do |file|
         
     | 
| 
      
 62 
     | 
    
         
            +
                          previous_sample = Marshal.load(file)
         
     | 
| 
      
 63 
     | 
    
         
            +
                        end
         
     | 
| 
      
 64 
     | 
    
         
            +
                      end
         
     | 
| 
      
 65 
     | 
    
         
            +
             
     | 
| 
      
 66 
     | 
    
         
            +
                      delta = TimeDifference.between(Time.parse(current_sample.sample_time), Time.parse(previous_sample.sample_time))
         
     | 
| 
      
 67 
     | 
    
         
            +
                      puts "Last sample was taken #{delta.in_seconds.round(0)} seconds ago"
         
     | 
| 
      
 68 
     | 
    
         
            +
             
     | 
| 
      
 69 
     | 
    
         
            +
                      previous_sample.values.keys.sort.each do |k|
         
     | 
| 
      
 70 
     | 
    
         
            +
                        previous_sample_value = previous_sample.values[k]
         
     | 
| 
      
 71 
     | 
    
         
            +
                        current_sample_value = current_sample.values[k]
         
     | 
| 
      
 72 
     | 
    
         
            +
                        value_per_seconds = ((current_sample_value - previous_sample_value) / delta.in_seconds).round(2)
         
     | 
| 
      
 73 
     | 
    
         
            +
                        result["#{k}_per_seconds"] = value_per_seconds
         
     | 
| 
      
 74 
     | 
    
         
            +
                      end
         
     | 
| 
      
 75 
     | 
    
         
            +
             
     | 
| 
      
 76 
     | 
    
         
            +
                      File.open('lastsample', 'w') do |file|
         
     | 
| 
      
 77 
     | 
    
         
            +
                        Marshal.dump(current_sample, file)
         
     | 
| 
      
 78 
     | 
    
         
            +
                      end
         
     | 
| 
      
 79 
     | 
    
         
            +
             
     | 
| 
      
 80 
     | 
    
         
            +
                      result
         
     | 
| 
      
 81 
     | 
    
         
            +
                    end
         
     | 
| 
      
 82 
     | 
    
         
            +
                  end
         
     | 
| 
      
 83 
     | 
    
         
            +
                end
         
     | 
| 
      
 84 
     | 
    
         
            +
              end
         
     | 
| 
       82 
85 
     | 
    
         
             
            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      = "https://github.com/emmekappa/mongodb-graphite-agent"
         
     | 
| 
       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      = "https://github.com/emmekappa/mongodb-graphite-agent"
         
     | 
| 
      
 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 
     | 
    
         
            +
              version: 0.0.9
         
     | 
| 
       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- 
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2013-07-12 00:00:00.000000000 Z
         
     | 
| 
       13 
13 
     | 
    
         
             
            dependencies:
         
     | 
| 
       14 
14 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       15 
15 
     | 
    
         
             
              name: bundler
         
     | 
| 
         @@ -188,15 +188,21 @@ required_ruby_version: !ruby/object:Gem::Requirement 
     | 
|
| 
       188 
188 
     | 
    
         
             
              - - ! '>='
         
     | 
| 
       189 
189 
     | 
    
         
             
                - !ruby/object:Gem::Version
         
     | 
| 
       190 
190 
     | 
    
         
             
                  version: '0'
         
     | 
| 
      
 191 
     | 
    
         
            +
                  segments:
         
     | 
| 
      
 192 
     | 
    
         
            +
                  - 0
         
     | 
| 
      
 193 
     | 
    
         
            +
                  hash: 2166291128576152408
         
     | 
| 
       191 
194 
     | 
    
         
             
            required_rubygems_version: !ruby/object:Gem::Requirement
         
     | 
| 
       192 
195 
     | 
    
         
             
              none: false
         
     | 
| 
       193 
196 
     | 
    
         
             
              requirements:
         
     | 
| 
       194 
197 
     | 
    
         
             
              - - ! '>='
         
     | 
| 
       195 
198 
     | 
    
         
             
                - !ruby/object:Gem::Version
         
     | 
| 
       196 
199 
     | 
    
         
             
                  version: '0'
         
     | 
| 
      
 200 
     | 
    
         
            +
                  segments:
         
     | 
| 
      
 201 
     | 
    
         
            +
                  - 0
         
     | 
| 
      
 202 
     | 
    
         
            +
                  hash: 2166291128576152408
         
     | 
| 
       197 
203 
     | 
    
         
             
            requirements: []
         
     | 
| 
       198 
204 
     | 
    
         
             
            rubyforge_project: 
         
     | 
| 
       199 
     | 
    
         
            -
            rubygems_version: 1.8. 
     | 
| 
      
 205 
     | 
    
         
            +
            rubygems_version: 1.8.25
         
     | 
| 
       200 
206 
     | 
    
         
             
            signing_key: 
         
     | 
| 
       201 
207 
     | 
    
         
             
            specification_version: 3
         
     | 
| 
       202 
208 
     | 
    
         
             
            summary: ''
         
     |