From c9ed3f80154b4d86a48e9c6c3271cc1331be6e13 Mon Sep 17 00:00:00 2001 From: Ben Abrams Date: Mon, 6 Aug 2018 17:32:51 -0700 Subject: [PATCH] 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