Skip to content

Allow querying specific node for check-rabbitmq-node-health #70

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This CHANGELOG follows the format listed at [Keep A Changelog](http://keepachang

## [Unreleased]
### Added
- check-rabbitmq-node-health: Proper node definition support (@Infraded)
- ruby 2.4 support (@majormoses)

### Fixed
Expand Down
48 changes: 40 additions & 8 deletions bin/check-rabbitmq-node-health.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env ruby
# encoding: UTF-8

#
# RabbitMQ check node health plugin
# ===
Expand Down Expand Up @@ -29,6 +30,7 @@
require 'json'
require 'rest_client'
require 'inifile'
require 'socket'

# main plugin class
class CheckRabbitMQNodeHealth < Sensu::Plugin::Check::CLI
Expand Down Expand Up @@ -56,6 +58,21 @@ class CheckRabbitMQNodeHealth < Sensu::Plugin::Check::CLI
long: '--port PORT',
default: '15672'

option :node_prefix,
description: 'RabbitMQ node prefix (eg. rabbit in rabbit@hostname)',
long: '--prefix NODENAME',
default: 'rabbit'

option :node_use_fqdn,
description: 'Use FQDN for determining node hostname',
long: '--fqdn',
boolean: true,
default: false

option :node_host,
description: 'RabbitMQ node name override (eg. hostname in rabbit@hostname)',
long: '--nodehost HOSTNAME'

option :ssl,
description: 'Enable SSL for connection to the API',
long: '--ssl',
Expand Down Expand Up @@ -136,12 +153,26 @@ def run
end

def node_healthy?
host = config[:host]
port = config[:port]
username = config[:username]
password = config[:password]
ssl = config[:ssl]
verify_ssl = config[:verify_ssl_off]
host = config[:host]
port = config[:port]
username = config[:username]
password = config[:password]
ssl = config[:ssl]
verify_ssl = config[:verify_ssl_off]
node_prefix = config[:node_prefix]

# Determine node hostname to query, as this may not be the same as connection hostname
node_host = if config[:node_host]
Copy link
Member

@majormoses majormoses Aug 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to address the ABC size I'd move this into its own own function such as determine_hostname or set_hostname

# Statically set by config
config[:node_host]
elsif config[:node_use_fqdn]
# Resolved from system running check, full FQDN, the same as RABBITMQ_USE_LONGNAME
Socket.gethostname
else
# Default of short hostname, resolved from system
Socket.gethostname.partition('.').first
end

if config[:ini]
ini = IniFile.load(config[:ini])
section = ini['auth']
Expand All @@ -154,14 +185,15 @@ def node_healthy?

begin
url_prefix = ssl ? 'https' : 'http'
node = "#{node_prefix}@#{node_host}"
resource = RestClient::Resource.new(
"#{url_prefix}://#{host}:#{port}/api/nodes",
"#{url_prefix}://#{host}:#{port}/api/nodes/#{node}",
user: username,
password: password,
verify_ssl: !verify_ssl
)
# Parse our json data
nodeinfo = JSON.parse(resource.get)[0]
nodeinfo = JSON.parse(resource.get)

# Determine % memory consumed
pmem = format('%.2f', nodeinfo['mem_used'].fdiv(nodeinfo['mem_limit']) * 100)
Expand Down