Skip to content

Commit 3e4611a

Browse files
author
Neil Greenwood
authored
Merge pull request #11 from neil-greenwood/master
Update to sensu-plugins-rabbitmq v7.0.0
2 parents cc27405 + 0ed12fb commit 3e4611a

19 files changed

+1655
-521
lines changed
Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
11
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
24
#
35
# RabbitMQ check alive plugin
46
# ===
57
#
8+
# DESCRIPTION:
69
# This plugin checks if RabbitMQ server is alive using the REST API
710
#
11+
# PLATFORMS:
12+
# Linux, Windows, BSD, Solaris
13+
#
14+
# DEPENDENCIES:
15+
# RabbitMQ rabbitmq_management plugin
16+
# gem: sensu-plugin
17+
# gem: rest-client
18+
#
19+
# LICENSE:
820
# Copyright 2012 Abhijith G <abhi@runa.com> and Runa Inc.
921
#
1022
# Released under the same terms as Sensu (the MIT license); see LICENSE
1123
# for details.
1224

13-
require 'rubygems' if RUBY_VERSION < '1.9.0'
1425
require 'sensu-plugin/check/cli'
1526
require 'json'
1627
require 'rest_client'
28+
require 'inifile'
1729

18-
class CheckRabbitMQ < Sensu::Plugin::Check::CLI
30+
# main plugin class
31+
class CheckRabbitMQAlive < Sensu::Plugin::Check::CLI
1932
option :host,
2033
description: 'RabbitMQ host',
2134
short: '-w',
@@ -52,6 +65,17 @@ class CheckRabbitMQ < Sensu::Plugin::Check::CLI
5265
boolean: true,
5366
default: false
5467

68+
option :verify_ssl_off,
69+
description: 'Do not check validity of SSL cert. Use for self-signed certs, etc (insecure)',
70+
long: '--verify_ssl_off',
71+
boolean: true,
72+
default: false
73+
74+
option :ini,
75+
description: 'Configuration ini file',
76+
short: '-i',
77+
long: '--ini VALUE'
78+
5579
def run
5680
res = vhost_alive?
5781

@@ -65,21 +89,34 @@ def run
6589
end
6690

6791
def vhost_alive?
68-
host = config[:host]
69-
port = config[:port]
70-
username = config[:username]
71-
password = config[:password]
72-
vhost = config[:vhost]
73-
ssl = config[:ssl]
92+
host = config[:host]
93+
port = config[:port]
94+
vhost = config[:vhost]
95+
ssl = config[:ssl]
96+
verify_ssl = config[:verify_ssl_off]
97+
if config[:ini]
98+
ini = IniFile.load(config[:ini])
99+
section = ini['auth']
100+
username = section['username']
101+
password = section['password']
102+
else
103+
username = config[:username]
104+
password = config[:password]
105+
end
74106

75107
begin
76-
resource = RestClient::Resource.new "http#{ssl ? 's' : ''}://#{host}:#{port}/api/aliveness-test/#{vhost}", username, password
108+
resource = RestClient::Resource.new(
109+
"http#{ssl ? 's' : ''}://#{host}:#{port}/api/aliveness-test/#{vhost}",
110+
user: username,
111+
password: password,
112+
verify_ssl: !verify_ssl
113+
)
77114
# Attempt to parse response (just to trigger parse exception)
78115
_response = JSON.parse(resource.get) == { 'status' => 'ok' }
79116
{ 'status' => 'ok', 'message' => 'RabbitMQ server is alive' }
80117
rescue Errno::ECONNREFUSED => e
81118
{ 'status' => 'critical', 'message' => e.message }
82-
rescue => e
119+
rescue StandardError => e
83120
{ 'status' => 'unknown', 'message' => e.message }
84121
end
85122
end
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
#
5+
# RabbitMQ check amqp alive plugin
6+
# ===
7+
#
8+
# DESCRIPTION:
9+
# This plugin checks if RabbitMQ server is alive using AMQP
10+
#
11+
# PLATFORMS:
12+
# Linux, BSD, Solaris
13+
#
14+
# DEPENDENCIES:
15+
# gem: sensu-plugin
16+
# gem: bunny
17+
#
18+
# LICENSE:
19+
# Copyright 2013 Milos Gajdos
20+
#
21+
# Released under the same terms as Sensu (the MIT license); see LICENSE
22+
# for details.
23+
24+
require 'sensu-plugin/check/cli'
25+
require 'bunny'
26+
require 'inifile'
27+
28+
# main plugin class
29+
class CheckRabbitAMQPAlive < Sensu::Plugin::Check::CLI
30+
option :host,
31+
description: 'RabbitMQ host',
32+
short: '-w',
33+
long: '--host HOST',
34+
default: 'localhost'
35+
36+
option :vhost,
37+
description: 'RabbitMQ vhost',
38+
short: '-v',
39+
long: '--vhost VHOST',
40+
default: '%2F'
41+
42+
option :username,
43+
description: 'RabbitMQ username',
44+
short: '-u',
45+
long: '--username USERNAME',
46+
default: 'guest'
47+
48+
option :password,
49+
description: 'RabbitMQ password',
50+
short: '-p',
51+
long: '--password PASSWORD',
52+
default: 'guest'
53+
54+
option :port,
55+
description: 'RabbitMQ AMQP port',
56+
short: '-P',
57+
long: '--port PORT',
58+
default: '5672'
59+
60+
option :ssl,
61+
description: 'Enable SSL for connection to RabbitMQ',
62+
long: '--ssl',
63+
boolean: true,
64+
default: false
65+
66+
option :tls_cert,
67+
description: 'TLS Certificate to use when connecting',
68+
long: '--tls-cert CERT',
69+
default: nil
70+
71+
option :tls_key,
72+
description: 'TLS Private Key to use when connecting',
73+
long: '--tls-key KEY',
74+
default: nil
75+
76+
option :no_verify_peer,
77+
description: 'Disable peer verification',
78+
long: '--no-verify-peer',
79+
boolean: true,
80+
default: true
81+
82+
option :heartbeat,
83+
description: 'Client heartbeat interval',
84+
long: '--heartbeat HB',
85+
proc: proc(&:to_i),
86+
default: 0
87+
88+
option :threaded,
89+
description: 'Enables threaded connections',
90+
long: '--threaded THREAD',
91+
boolean: true,
92+
default: true
93+
94+
option :ini,
95+
description: 'Configuration ini file',
96+
short: '-i',
97+
long: '--ini VALUE'
98+
99+
def run
100+
res = vhost_alive?
101+
102+
if res['status'] == 'ok'
103+
ok res['message']
104+
elsif res['status'] == 'critical'
105+
critical res['message']
106+
else
107+
unknown res['message']
108+
end
109+
end
110+
111+
def vhost_alive?
112+
host = config[:host]
113+
port = config[:port]
114+
vhost = config[:vhost]
115+
ssl = config[:ssl]
116+
tls_cert = config[:tls_cert]
117+
tls_key = config[:tls_key]
118+
no_verify_peer = config[:no_verify_peer]
119+
heartbeat = config[:heartbeat]
120+
threaded = config[:threaded]
121+
122+
if config[:ini]
123+
ini = IniFile.load(config[:ini])
124+
section = ini['auth']
125+
username = section['username']
126+
password = section['password']
127+
else
128+
username = config[:username]
129+
password = config[:password]
130+
end
131+
132+
begin
133+
conn = Bunny.new("amqp#{ssl ? 's' : ''}://#{username}:#{password}@#{host}:#{port}/#{vhost}",
134+
tls_cert: tls_cert,
135+
tls_key: tls_key,
136+
verify_peer: no_verify_peer,
137+
heartbeat: heartbeat,
138+
threaded: threaded)
139+
conn.start
140+
conn.close if conn.connected?
141+
{ 'status' => 'ok', 'message' => 'RabbitMQ server is alive' }
142+
rescue Bunny::PossibleAuthenticationFailureError
143+
{ 'status' => 'critical', 'message' => 'Possible authentication failure' }
144+
rescue Bunny::TCPConnectionFailed
145+
{ 'status' => 'critical', 'message' => 'TCP connection refused' }
146+
rescue StandardError => e
147+
{ 'status' => 'unknown', 'message' => e.message }
148+
end
149+
end
150+
end

plugins/rabbitmq/rabbitmq-cluster-health.rb renamed to plugins/rabbitmq/check-rabbitmq-cluster-health.rb

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
11
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
24
#
35
# RabbitMQ check cluster nodes health plugin
46
# ===
57
#
8+
# DESCRIPTION:
69
# This plugin checks if RabbitMQ server's cluster nodes are in a running state.
710
# It also accepts and optional list of nodes and verifies that those nodes are
811
# present in the cluster.
912
# The plugin is based on the RabbitMQ alive plugin by Abhijith G.
1013
#
11-
# Todo:
12-
# - Add ability to specify http vs. https
14+
# PLATFORMS:
15+
# Linux, Windows, BSD, Solaris
16+
#
17+
# DEPENDENCIES:
18+
# RabbitMQ rabbitmq_management plugin
19+
# gem: sensu-plugin
20+
# gem: rest-client
1321
#
22+
# LICENSE:
1423
# Copyright 2012 Abhijith G <abhi@runa.com> and Runa Inc.
1524
# Copyright 2014 Tim Smith <tim@cozy.co> and Cozy Services Ltd.
1625
#
1726
# Released under the same terms as Sensu (the MIT license); see LICENSE
1827
# for details.
1928

20-
require 'rubygems' if RUBY_VERSION < '1.9.0'
2129
require 'sensu-plugin/check/cli'
2230
require 'json'
2331
require 'rest_client'
32+
require 'inifile'
2433

34+
# main plugin class
2535
class CheckRabbitMQCluster < Sensu::Plugin::Check::CLI
2636
option :host,
2737
description: 'RabbitMQ host',
@@ -53,6 +63,28 @@ class CheckRabbitMQCluster < Sensu::Plugin::Check::CLI
5363
long: '--nodes NODE1,NODE2',
5464
default: ''
5565

66+
option :ssl,
67+
description: 'Enable SSL for connection to the API',
68+
long: '--ssl',
69+
boolean: true,
70+
default: false
71+
72+
option :verify_ssl_off,
73+
description: 'Do not check validity of SSL cert. Use for self-signed certs, etc (insecure)',
74+
long: '--verify_ssl_off',
75+
boolean: true,
76+
default: false
77+
78+
option :ssl_ca_file,
79+
description: 'Path to SSL CA .crt',
80+
long: '--ssl_ca_file CA_PATH',
81+
default: ''
82+
83+
option :ini,
84+
description: 'Configuration ini file',
85+
short: '-i',
86+
long: '--ini VALUE'
87+
5688
def run
5789
res = cluster_healthy?
5890

@@ -81,14 +113,35 @@ def failed_nodes?(servers_status)
81113
end
82114

83115
def cluster_healthy?
84-
host = config[:host]
85-
port = config[:port]
86-
username = config[:username]
87-
password = config[:password]
88-
nodes = config[:nodes].split(',')
116+
host = config[:host]
117+
port = config[:port]
118+
ssl = config[:ssl]
119+
verify_ssl = config[:verify_ssl_off]
120+
nodes = config[:nodes].split(',')
121+
ssl_ca_file = config[:ssl_ca_file]
122+
if config[:ini]
123+
ini = IniFile.load(config[:ini])
124+
section = ini['auth']
125+
username = section['username']
126+
password = section['password']
127+
else
128+
username = config[:username]
129+
password = config[:password]
130+
end
89131

90132
begin
91-
resource = RestClient::Resource.new "http://#{host}:#{port}/api/nodes", username, password
133+
url_prefix = ssl ? 'https' : 'http'
134+
options = {
135+
user: username,
136+
password: password,
137+
verify_ssl: !verify_ssl
138+
}
139+
options[:ssl_ca_file] = ssl_ca_file unless ssl_ca_file.empty?
140+
141+
resource = RestClient::Resource.new(
142+
"#{url_prefix}://#{host}:#{port}/api/nodes",
143+
options
144+
)
92145
# create a hash of the server names and their running state
93146
servers_status = Hash[JSON.parse(resource.get).map { |server| [server['name'], server['running']] }]
94147

@@ -100,16 +153,16 @@ def cluster_healthy?
100153

101154
# build status and message
102155
status = failed_nodes.empty? && missing_nodes.empty? ? 'ok' : 'critical'
103-
if failed_nodes.empty?
104-
message = "#{servers_status.keys.count} healthy cluster nodes"
105-
else
106-
message = "#{failed_nodes.count} failed cluster node: #{failed_nodes.sort.join(',')}"
107-
end
108-
message.prepend("#{missing_nodes.count } node(s) not found: #{missing_nodes.join(',')}. ") unless missing_nodes.empty?
156+
message = if failed_nodes.empty?
157+
"#{servers_status.keys.count} healthy cluster nodes"
158+
else
159+
"#{failed_nodes.count} failed cluster node: #{failed_nodes.sort.join(',')}"
160+
end
161+
message.prepend("#{missing_nodes.count} node(s) not found: #{missing_nodes.join(',')}. ") unless missing_nodes.empty?
109162
{ 'status' => status, 'message' => message }
110163
rescue Errno::ECONNREFUSED => e
111164
{ 'status' => 'critical', 'message' => e.message }
112-
rescue => e
165+
rescue StandardError => e
113166
{ 'status' => 'unknown', 'message' => e.message }
114167
end
115168
end

0 commit comments

Comments
 (0)