Skip to content

Commit

Permalink
adding --connect option and use it
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
  • Loading branch information
majormoses committed Aug 7, 2018
1 parent 1534adb commit 4ec51e5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions bin/metrics-mongodb-replication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
7 changes: 7 additions & 0 deletions bin/metrics-mongodb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 2 additions & 1 deletion lib/sensu-plugins-mongodb/metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -369,14 +369,15 @@ 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)
else
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
Expand Down

0 comments on commit 4ec51e5

Please sign in to comment.