Skip to content

Commit

Permalink
Move duplicated code to libraries (#71)
Browse files Browse the repository at this point in the history
* Add plugin stub for tests

* Add tests for queue metrics

* Add tests for exchange metrics

* Add tests for queue check

* Add base class for metrics (#65)

* Add common check class

* Move common options into a module

* Common rabbitmq_info getter

* Fix Rubocop offenses

* Split module and classes in separate files

* Update Changelog

* Remove require_relative

* Use ruby_dig for older rubies
  • Loading branch information
rthouvenin authored and majormoses committed Sep 6, 2017
1 parent dea41d4 commit 2c5f865
Show file tree
Hide file tree
Showing 13 changed files with 469 additions and 250 deletions.
9 changes: 6 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ This CHANGELOG follows the format listed at [Keep A Changelog](http://keepachang
- check-rabbitmq-consumers.rb: Added ability to select queues with regular expressions. (@jtduby)

### Added
- ruby 2.4 support (@majormoses)

- ruby 2.4 support (@majormoses)
- RabbitMQ module with common code (@rthouvenin)
- Check and Metrics base classes (@rthouvenin)
### Changed
- metrics-rabbitmq-queue, metrics-rabbitmq-exchange, check-rabbitmq-queue: use the new base classes (@rthouvenin)
### Fixed
- PR template spelling (@majormoses)
- PR template spelling (@majormoses)

## [3.2.0] - 2017-06-20
### Added
Expand Down
70 changes: 2 additions & 68 deletions bin/check-rabbitmq-queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,10 @@
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.

require 'sensu-plugin/check/cli'
require 'socket'
require 'carrot-top'
require 'inifile'
require 'sensu-plugins-rabbitmq'

# main plugin class
class CheckRabbitMQMessages < Sensu::Plugin::Check::CLI
option :host,
description: 'RabbitMQ management API host',
long: '--host HOST',
default: 'localhost'

option :port,
description: 'RabbitMQ management API port',
long: '--port PORT',
proc: proc(&:to_i),
default: 15_672

option :vhost,
description: 'RabbitMQ vhost',
short: '-v',
long: '--vhost VHOST',
default: ''

option :ssl,
description: 'Enable SSL for connection to the API',
long: '--ssl',
boolean: true,
default: false

option :username,
description: 'RabbitMQ management API user',
long: '--username USER',
default: 'guest'

option :password,
description: 'RabbitMQ management API password',
long: '--password PASSWORD',
default: 'guest'

class CheckRabbitMQMessages < Sensu::Plugin::RabbitMQ::Check
option :queue,
description: 'RabbitMQ queue to monitor',
long: '--queue queue_names',
Expand Down Expand Up @@ -103,36 +67,6 @@ class CheckRabbitMQMessages < Sensu::Plugin::Check::CLI
boolean: true,
default: false

option :ini,
description: 'Configuration ini file',
short: '-i',
long: '--ini VALUE'

def acquire_rabbitmq_info
begin
if config[:ini]
ini = IniFile.load(config[:ini])
section = ini['auth']
username = section['username']
password = section['password']
else
username = config[:username]
password = config[:password]
end

rabbitmq_info = CarrotTop.new(
host: config[:host],
port: config[:port],
user: username,
password: password,
ssl: config[:ssl]
)
rescue
warning 'could not get rabbitmq info'
end
rabbitmq_info
end

def run
@crit = []
@warn = []
Expand Down
92 changes: 3 additions & 89 deletions bin/metrics-rabbitmq-exchange.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,44 +24,10 @@
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.

require 'sensu-plugin/metric/cli'
require 'socket'
require 'carrot-top'
require 'inifile'
require 'sensu-plugins-rabbitmq'

# main plugin class
class RabbitMQExchangeMetrics < Sensu::Plugin::Metric::CLI::Graphite
option :host,
description: 'RabbitMQ management API host',
long: '--host HOST',
default: 'localhost'

option :port,
description: 'RabbitMQ management API port',
long: '--port PORT',
proc: proc(&:to_i),
default: 15_672

option :vhost,
description: 'Regular expression for filtering the RabbitMQ vhost',
short: '-v',
long: '--vhost VHOST'

option :username,
description: 'RabbitMQ management API user',
long: '--username USER',
default: 'guest'

option :password,
description: 'RabbitMQ management API password',
long: '--password PASSWORD',
default: 'guest'

option :scheme,
description: 'Metric naming scheme, text to prepend to $exchange_name.$metric',
long: '--scheme SCHEME',
default: "#{Socket.gethostname}.rabbitmq"

class RabbitMQExchangeMetrics < Sensu::Plugin::RabbitMQ::Metrics
option :filter,
description: 'Regular expression for filtering exchanges',
long: '--filter REGEX'
Expand All @@ -70,61 +36,9 @@ class RabbitMQExchangeMetrics < Sensu::Plugin::Metric::CLI::Graphite
description: 'Regular expression for filtering metrics in each exchange',
long: '--metrics REGEX'

option :ssl,
description: 'Enable SSL for connection to the API',
long: '--ssl',
boolean: true,
default: false

option :ini,
description: 'Configuration ini file',
short: '-i',
long: '--ini VALUE'

def acquire_rabbitmq_exchanges
begin
if config[:ini]
ini = IniFile.load(config[:ini])
section = ini['auth']
username = section['username']
password = section['password']
else
username = config[:username]
password = config[:password]
end

rabbitmq_info = CarrotTop.new(
host: config[:host],
port: config[:port],
user: username,
password: password,
ssl: config[:ssl]
)
rescue
warning 'could not get rabbitmq exchange info'
end

if config[:vhost]
return rabbitmq_info.exchanges.select { |x| x['vhost'].match(config[:vhost]) }
end

rabbitmq_info.exchanges
end

def dotted_keys(hash, prefix = '', keys = [])
hash.each do |k, v|
if v.is_a? Hash
keys = dotted_keys(v, prefix + k + '.', keys)
else
keys << prefix + k
end
end
keys
end

def run
timestamp = Time.now.to_i
acquire_rabbitmq_exchanges.each do |exchange|
acquire_rabbitmq_info(:exchanges).each do |exchange|
if config[:filter]
next unless exchange['name'].match(config[:filter])
end
Expand Down
92 changes: 3 additions & 89 deletions bin/metrics-rabbitmq-queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,44 +28,10 @@
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.

require 'sensu-plugin/metric/cli'
require 'socket'
require 'carrot-top'
require 'inifile'
require 'sensu-plugins-rabbitmq'

# main plugin class
class RabbitMQMetrics < Sensu::Plugin::Metric::CLI::Graphite
option :host,
description: 'RabbitMQ management API host',
long: '--host HOST',
default: 'localhost'

option :port,
description: 'RabbitMQ management API port',
long: '--port PORT',
proc: proc(&:to_i),
default: 15_672

option :vhost,
description: 'Regular expression for filtering the RabbitMQ vhost',
short: '-v',
long: '--vhost VHOST'

option :username,
description: 'RabbitMQ management API user',
long: '--username USER',
default: 'guest'

option :password,
description: 'RabbitMQ management API password',
long: '--password PASSWORD',
default: 'guest'

option :scheme,
description: 'Metric naming scheme, text to prepend to $queue_name.$metric',
long: '--scheme SCHEME',
default: "#{Socket.gethostname}.rabbitmq"

class RabbitMQQueueMetrics < Sensu::Plugin::RabbitMQ::Metrics
option :filter,
description: 'Regular expression for filtering queues',
long: '--filter REGEX'
Expand All @@ -75,61 +41,9 @@ class RabbitMQMetrics < Sensu::Plugin::Metric::CLI::Graphite
long: '--metrics REGEX',
default: '^messages$|consumers|drain_time|avg_egress'

option :ssl,
description: 'Enable SSL for connection to the API',
long: '--ssl',
boolean: true,
default: false

option :ini,
description: 'Configuration ini file',
short: '-i',
long: '--ini VALUE'

def acquire_rabbitmq_queues
begin
if config[:ini]
ini = IniFile.load(config[:ini])
section = ini['auth']
username = section['username']
password = section['password']
else
username = config[:username]
password = config[:password]
end

rabbitmq_info = CarrotTop.new(
host: config[:host],
port: config[:port],
user: username,
password: password,
ssl: config[:ssl]
)
rescue
warning 'could not get rabbitmq queue info'
end

if config[:vhost]
return rabbitmq_info.queues.select { |x| x['vhost'].match(config[:vhost]) }
end

rabbitmq_info.queues
end

def dotted_keys(hash, prefix = '', keys = [])
hash.each do |k, v|
if v.is_a? Hash
keys = dotted_keys(v, prefix + k + '.', keys)
else
keys << prefix + k
end
end
keys
end

def run
timestamp = Time.now.to_i
acquire_rabbitmq_queues.each do |queue|
acquire_rabbitmq_info(:queues).each do |queue|
if config[:filter]
next unless queue['name'].match(config[:filter])
end
Expand Down
4 changes: 4 additions & 0 deletions lib/sensu-plugins-rabbitmq.rb
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
require 'ruby_dig'

require 'sensu-plugins-rabbitmq/version'
require 'sensu-plugins-rabbitmq/check'
require 'sensu-plugins-rabbitmq/metrics'
17 changes: 17 additions & 0 deletions lib/sensu-plugins-rabbitmq/check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'sensu-plugin/check/cli'
require 'sensu-plugins-rabbitmq/rabbitmq'

module Sensu
module Plugin
module RabbitMQ
class Check < Sensu::Plugin::Check::CLI
include Sensu::Plugin::RabbitMQ::Common

# To avoid complaints from mother class at the end of tests (at_exit handler)
def run
ok
end
end
end
end
end
34 changes: 34 additions & 0 deletions lib/sensu-plugins-rabbitmq/metrics.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'sensu-plugin/metric/cli'
require 'socket'
require 'sensu-plugins-rabbitmq/rabbitmq'

module Sensu
module Plugin
module RabbitMQ
class Metrics < Sensu::Plugin::Metric::CLI::Graphite
include Sensu::Plugin::RabbitMQ::Common

option :scheme,
description: 'Metric naming scheme',
long: '--scheme SCHEME',
default: "#{Socket.gethostname}.rabbitmq"

def dotted_keys(hash, prefix = '', keys = [])
hash.each do |k, v|
if v.is_a? Hash
keys = dotted_keys(v, prefix + k + '.', keys)
else
keys << prefix + k
end
end
keys
end

# To avoid complaints from mother class at the end of tests (at_exit handler)
def run
ok
end
end
end
end
end
Loading

0 comments on commit 2c5f865

Please sign in to comment.