forked from inuits/monitoring-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_rabbitmq-sync.rb
executable file
·82 lines (69 loc) · 1.8 KB
/
check_rabbitmq-sync.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env ruby
# Author: Jan Tlusty
# Email: honza@inuits.eu
# Date: Tue Aug 09 2016
require 'net/http'
require 'uri'
require 'json'
require 'optparse'
def usage(optparse)
puts optparse
raise OptionParser::MissingArgument
end
options = {}
optparse = OptionParser.new do |opts|
opts.on('-h', '--host HOST', "Mandatory Host") do |f|
options[:host] = f
end
opts.on('-u', '--user USER', "Mandatory User Name") do |f|
options[:user] = f
end
opts.on('-p', '--password PASSWORD', "Mandatory Password") do |f|
options[:password] = f
end
opts.on('-v', '--vhost VHOST', "Mandatory Vhost") do |f|
options[:vhost] = f
end
opts.on('-P', '--port PORT', "Mandatory Port") do |f|
options[:port] = f
end
end
optparse.parse!
if options[:host].nil? or options[:user].nil? or options[:password].nil? or options[:vhost].nil? or options[:port].nil?
usage(optparse)
end
if options[:host] !~ /^https?:\/\/.*/
options[:host] = 'http://' + options[:host]
end
begin
uri = URI::parse("#{options[:host]}:#{options[:port]}/api/queues/#{options[:vhost]}")
req = Net::HTTP::Get.new(uri)
req.basic_auth options[:user], options[:password]
res = Net::HTTP.start(uri.hostname, uri.port) {|http|
http.request(req)
}
rescue
puts 'Unable to log in to the app.'
exit 3
end
begin
json = JSON.parse(res.body)
msg=''
json.each do |queue|
if queue.key?('slave_nodes')
if queue['slave_nodes'].sort != queue['synchronised_slave_nodes'].sort
msg += "#{queue['name']}: slave_nodes = #{queue['slave_nodes'].sort}, synchronized slave nodes = #{queue['synchronised_slave_nodes'].sort} "
end
end
end
rescue
puts "Something went wrong, rabbitmq returned #{res.body}"
exit 3
end
if msg == ''
puts 'All slave nodes are synchronized'
exit 0
else
puts msg
exit 1
end