diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..b5751b5 --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--tag ~integration diff --git a/.travis.yml b/.travis.yml index a458d8b..4936d5a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,9 @@ cache: - bundler install: - bundle install +# If integration tests are accepted, we'd require this to run them +# services: +# - docker rvm: - 2.1 - 2.2 diff --git a/CHANGELOG.md b/CHANGELOG.md index ba12117..11d6a4b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ This project adheres to [Semantic Versioning](http://semver.org/). This CHANGELOG follows the format located [here](https://github.com/sensu-plugins/community/blob/master/HOW_WE_CHANGELOG.md) ## [Unreleased] +### Breaking Changes +- `--connect` now defaults to `:direct` so that Server status/Replicaset stats are current host's rather than all metrics in the replica set (@naemono) + +### Added +- exposed `--connect` which controls how it connects. Valid values are `:direct`, `:replica_set` (mongo client default), and `:sharded`. (@naemono) (@majormoses) ## [2.1.0] - 2018-12-27 ### Added diff --git a/Gemfile b/Gemfile index c564131..19da580 100644 --- a/Gemfile +++ b/Gemfile @@ -2,3 +2,7 @@ source 'https://rubygems.org' # Specify your gem's dependencies in sensu-plugins-mongodb.gemspec gemspec + +group :test do + gem 'docker-api' +end diff --git a/bin/metrics-mongodb-replication.rb b/bin/metrics-mongodb-replication.rb index 87c681e..1c23e56 100755 --- a/bin/metrics-mongodb-replication.rb +++ b/bin/metrics-mongodb-replication.rb @@ -103,6 +103,13 @@ class MongoDB < Sensu::Plugin::Metric::CLI::Graphite long: '--debug', default: false + option :connect, + description: 'The connection method to use', + long: '--connect METHOD', + in: %i(direct replica_set sharded none), + proc: proc(&:to_sym), + default: :none + def get_mongo_doc(command) rs = @db.command(command) unless rs.successful? diff --git a/bin/metrics-mongodb.rb b/bin/metrics-mongodb.rb index cfa26d2..c5e6c01 100755 --- a/bin/metrics-mongodb.rb +++ b/bin/metrics-mongodb.rb @@ -99,6 +99,13 @@ class MongoDB < Sensu::Plugin::Metric::CLI::Graphite long: '--require-master', default: false + option :connect, + description: 'The connection method to use', + long: '--connect METHOD', + in: %i(direct replica_set sharded none), + proc: proc(&:to_sym), + default: :none + option :exclude_db_sizes, description: 'Exclude database sizes', long: '--exclude-db-sizes', diff --git a/lib/sensu-plugins-mongodb/metrics.rb b/lib/sensu-plugins-mongodb/metrics.rb index 813a705..cd94ad5 100644 --- a/lib/sensu-plugins-mongodb/metrics.rb +++ b/lib/sensu-plugins-mongodb/metrics.rb @@ -369,6 +369,7 @@ def get_mongo_client(db_name) ssl_key = @config[:ssl_key] ssl_ca_cert = @config[:ssl_ca_cert] ssl_verify = @config[:ssl_verify] + connect = @config[:connect] if Gem.loaded_specs['mongo'].version < Gem::Version.new('2.0.0') MongoClient.new(host, port) @@ -376,6 +377,9 @@ def get_mongo_client(db_name) address_str = "#{host}:#{port}" client_opts = {} client_opts[:database] = db_name + unless connect.nil? + client_opts[:connect] = connect + end unless db_user.nil? client_opts[:user] = db_user client_opts[:password] = db_password diff --git a/test/lib/sensu-plugins-mongodb/metrics_spec.rb b/test/lib/sensu-plugins-mongodb/metrics_spec.rb index 3a3723b..89285cb 100644 --- a/test/lib/sensu-plugins-mongodb/metrics_spec.rb +++ b/test/lib/sensu-plugins-mongodb/metrics_spec.rb @@ -301,3 +301,70 @@ end end end + +describe 'integration tests', :integration do + @container = nil + before(:all) do + require 'docker' + Docker::Image.create('fromImage' => 'flqw/docker-mongo-local-replicaset:latest') + @container = Docker::Container.create( + 'Image' => 'flqw/docker-mongo-local-replicaset', + 'ExposedPorts' => { + '27001/tcp' => {}, + '27002/tcp' => {}, + '27003/tcp' => {}, + }, + 'HostConfig' => { + 'PortBindings' => { + '27001/tcp' => [{ 'HostPort' => '27001' }], + '27002/tcp' => [{ 'HostPort' => '27002' }], + '27003/tcp' => [{ 'HostPort' => '27003' }], + } + } + ) + @container.start + sleep(15) + end + + it 'get metrics from primary in direct mode' do + @config = { + host: 'localhost', + port: 27001, + connect: :direct, + debug: true + } + metrics = SensuPluginsMongoDB::Metrics.new(@config) + metrics.connect_mongo_db('admin') + result = metrics.server_metrics + expect(result['metrics.replicaset.state']).to eq 1 + end + + it 'get metrics from secondary in direct mode' do + @config = { + host: 'localhost', + port: 27002, + connect: :direct, + debug: true + } + metrics = SensuPluginsMongoDB::Metrics.new(@config) + metrics.connect_mongo_db('admin') + result = metrics.server_metrics + expect(result['metrics.replicaset.state']).to eq 2 + end + + it 'get metrics from secondary without :connect options redirects to primary' do + @config = { + host: 'localhost', + port: 27002, + debug: true + } + metrics = SensuPluginsMongoDB::Metrics.new(@config) + metrics.connect_mongo_db('admin') + result = metrics.server_metrics + expect(result['metrics.replicaset.state']).to eq 1 + end + + after(:all) do + @container.delete(:force => true) + end +end \ No newline at end of file