Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replicaset status fix #55

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--tag ~integration
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 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?
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 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',
Expand Down
4 changes: 4 additions & 0 deletions lib/sensu-plugins-mongodb/metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -369,13 +369,17 @@ 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
unless connect.nil?
client_opts[:connect] = connect
end
unless db_user.nil?
client_opts[:user] = db_user
client_opts[:password] = db_password
Expand Down
67 changes: 67 additions & 0 deletions test/lib/sensu-plugins-mongodb/metrics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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