From aeffa4e048d0a3147539c03f67001c8a55bd498e Mon Sep 17 00:00:00 2001 From: naemono Date: Mon, 2 Oct 2017 11:37:55 -0500 Subject: [PATCH 1/9] Ensure server + replicaset metrics are pulled for local host, not using 'read preference' --- lib/sensu-plugins-mongodb/metrics.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/sensu-plugins-mongodb/metrics.rb b/lib/sensu-plugins-mongodb/metrics.rb index 813a705..f3fd824 100644 --- a/lib/sensu-plugins-mongodb/metrics.rb +++ b/lib/sensu-plugins-mongodb/metrics.rb @@ -376,6 +376,7 @@ def get_mongo_client(db_name) address_str = "#{host}:#{port}" client_opts = {} client_opts[:database] = db_name + client_opts[:connect] = :direct unless db_user.nil? client_opts[:user] = db_user client_opts[:password] = db_password From 1534adbd7a9c030778676ddd823d9820384c4b40 Mon Sep 17 00:00:00 2001 From: naemono Date: Mon, 2 Oct 2017 11:40:44 -0500 Subject: [PATCH 2/9] Changelog update --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 05fa008..8326451 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ 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] +### Fixed +- Ensure Server status/Replicaset stats are current host's, not host from read preference (@naemon) ## [2.0.2] - 2018-03-17 ### Fixed From c9ed3f80154b4d86a48e9c6c3271cc1331be6e13 Mon Sep 17 00:00:00 2001 From: Ben Abrams Date: Mon, 6 Aug 2018 17:32:51 -0700 Subject: [PATCH 3/9] adding `--connect` option and use it - when retrieving metrics it will now pull only local metrics rather than all nodes in the reiplca set. To revert this behavior you can add `--connect replica_set` - control of connection type for metric gatherering scripts Signed-off-by: Ben Abrams --- CHANGELOG.md | 7 +++++-- bin/metrics-mongodb-replication.rb | 7 +++++++ bin/metrics-mongodb.rb | 7 +++++++ lib/sensu-plugins-mongodb/metrics.rb | 3 ++- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8326451..797966f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +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] -### Fixed -- Ensure Server status/Replicaset stats are current host's, not host from read preference (@naemon) +### 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 (@naemon) + +### Added +- exposed `--connect` which controls how it connects. Valid values are `:direct`, `:replica_set` (mongo client default), and `:sharded`. (@naemon) (@majormoses) ## [2.0.2] - 2018-03-17 ### Fixed diff --git a/bin/metrics-mongodb-replication.rb b/bin/metrics-mongodb-replication.rb index 87c681e..55be9f6 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`, + in: %i(direct replica_set sharded), + proc: proc(&:to_sym), + default: :direct + 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 3011b35..def18b3 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`, + in: %i(direct replica_set sharded), + proc: proc(&:to_sym), + default: :direct + def run Mongo::Logger.logger.level = Logger::FATAL @debug = config[:debug] diff --git a/lib/sensu-plugins-mongodb/metrics.rb b/lib/sensu-plugins-mongodb/metrics.rb index f3fd824..6400eba 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,7 +377,7 @@ def get_mongo_client(db_name) address_str = "#{host}:#{port}" client_opts = {} client_opts[:database] = db_name - client_opts[:connect] = :direct + client_opts[:connect] = connect unless db_user.nil? client_opts[:user] = db_user client_opts[:password] = db_password From 422493a197040da7a390da827a199cf236b334aa Mon Sep 17 00:00:00 2001 From: Ben Abrams Date: Sat, 24 Apr 2021 12:33:46 -0700 Subject: [PATCH 4/9] Update bin/metrics-mongodb.rb --- bin/metrics-mongodb.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/metrics-mongodb.rb b/bin/metrics-mongodb.rb index 56e200e..2081b31 100755 --- a/bin/metrics-mongodb.rb +++ b/bin/metrics-mongodb.rb @@ -101,7 +101,7 @@ class MongoDB < Sensu::Plugin::Metric::CLI::Graphite option :connect, description: 'The connection method to use', - long: `--connect`, + long: '--connect METHOD', in: %i(direct replica_set sharded), proc: proc(&:to_sym), default: :direct From 4e082f1f0ce3006d6df9ad26d8f10b911bc193a3 Mon Sep 17 00:00:00 2001 From: Ben Abrams Date: Sat, 24 Apr 2021 12:34:09 -0700 Subject: [PATCH 5/9] fix connection argument --- bin/metrics-mongodb-replication.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/metrics-mongodb-replication.rb b/bin/metrics-mongodb-replication.rb index 55be9f6..323625e 100755 --- a/bin/metrics-mongodb-replication.rb +++ b/bin/metrics-mongodb-replication.rb @@ -105,7 +105,7 @@ class MongoDB < Sensu::Plugin::Metric::CLI::Graphite option :connect, description: 'The connection method to use', - long: `--connect`, + long: '--connect METHOD', in: %i(direct replica_set sharded), proc: proc(&:to_sym), default: :direct From 9e603e61ed44042f151ca47df5d9e8b859d6caea Mon Sep 17 00:00:00 2001 From: Michael Montgomery Date: Mon, 26 Apr 2021 11:48:54 -0500 Subject: [PATCH 6/9] Fix connect option Allow for a 'none' to be default to not break existing behavior Add integration test with full mongodb replicaset to show/ensure behavior --- .rspec | 1 + CHANGELOG.md | 4 +- Gemfile | 4 ++ bin/metrics-mongodb.rb | 6 +- lib/sensu-plugins-mongodb/metrics.rb | 4 +- .../lib/sensu-plugins-mongodb/metrics_spec.rb | 67 +++++++++++++++++++ 6 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 .rspec 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/CHANGELOG.md b/CHANGELOG.md index 35f44a3..11d6a4b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,10 +5,10 @@ This CHANGELOG follows the format located [here](https://github.com/sensu-plugin ## [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 (@naemon) +- `--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`. (@naemon) (@majormoses) +- 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.rb b/bin/metrics-mongodb.rb index 56e200e..c5e6c01 100755 --- a/bin/metrics-mongodb.rb +++ b/bin/metrics-mongodb.rb @@ -101,10 +101,10 @@ class MongoDB < Sensu::Plugin::Metric::CLI::Graphite option :connect, description: 'The connection method to use', - long: `--connect`, - in: %i(direct replica_set sharded), + long: '--connect METHOD', + in: %i(direct replica_set sharded none), proc: proc(&:to_sym), - default: :direct + default: :none option :exclude_db_sizes, description: 'Exclude database sizes', diff --git a/lib/sensu-plugins-mongodb/metrics.rb b/lib/sensu-plugins-mongodb/metrics.rb index 6400eba..cd94ad5 100644 --- a/lib/sensu-plugins-mongodb/metrics.rb +++ b/lib/sensu-plugins-mongodb/metrics.rb @@ -377,7 +377,9 @@ def get_mongo_client(db_name) address_str = "#{host}:#{port}" client_opts = {} client_opts[:database] = db_name - client_opts[:connect] = connect + 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 From 519107ca86ebfdd22a59f141dba61c5ae458b453 Mon Sep 17 00:00:00 2001 From: Michael Montgomery Date: Mon, 26 Apr 2021 11:50:50 -0500 Subject: [PATCH 7/9] Show travis.yml updates needed for docker support --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) 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 From d05b967a7f795d9e2719025777023843954cdafa Mon Sep 17 00:00:00 2001 From: Michael Montgomery Date: Mon, 26 Apr 2021 11:54:14 -0500 Subject: [PATCH 8/9] Add proper connect options to other bin file --- bin/metrics-mongodb-replication.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/metrics-mongodb-replication.rb b/bin/metrics-mongodb-replication.rb index 323625e..e2964a0 100755 --- a/bin/metrics-mongodb-replication.rb +++ b/bin/metrics-mongodb-replication.rb @@ -106,8 +106,9 @@ class MongoDB < Sensu::Plugin::Metric::CLI::Graphite option :connect, description: 'The connection method to use', long: '--connect METHOD', - in: %i(direct replica_set sharded), + in: %i(direct replica_set sharded none), proc: proc(&:to_sym), + default: :none default: :direct def get_mongo_doc(command) From 6a1331525215936f0a2523c41ad151d6f83af683 Mon Sep 17 00:00:00 2001 From: Michael Montgomery Date: Mon, 26 Apr 2021 11:55:58 -0500 Subject: [PATCH 9/9] Fix typo --- bin/metrics-mongodb-replication.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/bin/metrics-mongodb-replication.rb b/bin/metrics-mongodb-replication.rb index e2964a0..1c23e56 100755 --- a/bin/metrics-mongodb-replication.rb +++ b/bin/metrics-mongodb-replication.rb @@ -109,7 +109,6 @@ class MongoDB < Sensu::Plugin::Metric::CLI::Graphite in: %i(direct replica_set sharded none), proc: proc(&:to_sym), default: :none - default: :direct def get_mongo_doc(command) rs = @db.command(command)