Skip to content

Commit

Permalink
Fix picks only first nodes in the cluster bug (sensu-plugins#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
rocdove committed Jul 10, 2017
1 parent a1beaa5 commit 80fafb2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This CHANGELOG follows the format listed at [Keep A Changelog](http://keepachang

### Fixed
- PR template spelling (@majormoses)
- check-rabbitmq-node-health.rb: Fixes picks only first nodes in the cluster (@rocdove)

## [3.2.0] - 2017-06-20
### Added
Expand Down
62 changes: 39 additions & 23 deletions bin/check-rabbitmq-node-health.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ def run
end
end

def _get_state(status, new)
if status == 'ok'
return new
elsif status == 'warning' and new == 'critical'
return new
else
return status
end
end

def node_healthy?
host = config[:host]
port = config[:port]
Expand All @@ -155,62 +165,68 @@ def node_healthy?
begin
url_prefix = ssl ? 'https' : 'http'
resource = RestClient::Resource.new(
"#{url_prefix}://#{host}:#{port}/api/nodes",
"#{url_prefix}://#{host}:#{port}/api/nodes/rabbit@#{host}",
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)
# Determine % sockets consumed
psocket = format('%.2f', nodeinfo['sockets_used'].fdiv(nodeinfo['sockets_total']) * 100)
# Determine % file descriptors consumed
# Non-numeric value fails silently to handle fd_used = 'unknown' on OSX
# Non-numeric value fd_used = 'unknown' on OSX
if nodeinfo['fd_used'].is_a?(Numeric)
pfd = format('%.2f', nodeinfo['fd_used'].fdiv(nodeinfo['fd_total']) * 100)
end

# build status and message
status = 'ok'
message = 'Server is healthy'
message = ''

# criticals
if pmem.to_f >= config[:memcrit]
message = "Memory usage is critical: #{pmem}%"
status = 'critical'
elsif psocket.to_f >= config[:socketcrit]
message = "Socket usage is critical: #{psocket}%"
status = 'critical'
elsif pfd.to_f >= config[:fdcrit]
message = "File Descriptor usage is critical: #{pfd}%"
status = 'critical'
# warnings
message += "Memory usage is critical: #{pmem}%;"
status = _get_state(status, 'critical')
elsif pmem.to_f >= config[:memwarn]
message = "Memory usage is at warning: #{pmem}%"
status = 'warning'
message += "Memory usage is at warning: #{pmem}%;"
status = _get_state(status, 'warning')
end

if psocket.to_f >= config[:socketcrit]
message += "Socket usage is critical: #{psocket}%;"
status = _get_state(status, 'critical')
elsif psocket.to_f >= config[:socketwarn]
message = "Socket usage is at warning: #{psocket}%"
status = 'warning'
elsif pfd.to_f >= config[:fdwarn]
message = "File Descriptor usage is at warning: #{pfd}%"
status = 'warning'
message += "Socket usage is at warning: #{psocket}%;"
status = _get_state(status, 'warning')
end

# Non-numeric value don't deal on OSX
if pfd.nil?
if pfd.to_f >= config[:fdcrit]
message += "File Descriptor usage is critical: #{pfd}%;"
status = _get_state(status, 'critical')
elsif pfd.to_f >= config[:fdwarn]
message += "File Descriptor usage is at warning: #{pfd}%;"
status = _get_state(status, 'warning')
end
end

# If we are set to watch alarms then watch those and set status and messages accordingly
if config[:watchalarms] == 'true'
if nodeinfo['mem_alarm'] == true
status = 'critical'
status = _get_state(status, 'critical')
message += ' Memory Alarm ON'
end

if nodeinfo['disk_free_alarm'] == true
status = 'critical'
status = _get_state(status, 'critical')
message += ' Disk Alarm ON'
end
end
message = 'Server is healthy.' if status == 'ok'

{ 'status' => status, 'message' => message }
rescue Errno::ECONNREFUSED => e
Expand Down

0 comments on commit 80fafb2

Please sign in to comment.