-
Notifications
You must be signed in to change notification settings - Fork 57
/
check_topology-latency.rb
93 lines (78 loc) · 2.09 KB
/
check_topology-latency.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
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env ruby
# Author: Jan Tlusty
# Email: honza@inuits.eu
# Date: Thu Mar 02 2017
require 'net/http'
require 'uri'
require 'json'
require 'optparse'
host = 'http://localhost'
port = 8888
warning_latency = 1000
critical_latency= 1200
def usage(optparse)
puts optparse
puts 'Usage: ./storm.rb -h <host> -p <port>'
end
optparse = OptionParser.new do |opts|
opts.on('-h', '--host HOSTNAME', "Hostname") do |f|
host = f
end
opts.on('-p', '--port PORT', "Port") do |f|
port = f
end
opts.on('-c', '--critical CRITICAL', "Critical latency") do |f|
critical_latency = f.to_i
end
opts.on('-w', '--warning WARNING', "Warning latency") do |f|
warning_latency = f.to_i
end
opts.on('-H', '--help', "Displays this message") do
usage(optparse)
exit 0
end
end
optparse.parse!
if warning_latency >= critical_latency
puts "Error: Warning latency must be lower than critical latency"
exit 3
end
if host !~ /^https?:\/\/.*/
host = 'http://' + host
end
topologies = []
exit_code = 0
max_latency = 0
message = ''
uri_summary = URI::parse("#{host}:#{port}/api/v1/topology/summary")
req_summary = Net::HTTP::Get.new(uri_summary)
res_summary = Net::HTTP.start(uri_summary.hostname, uri_summary.port) {|http|
http.request(req_summary)}
json_summary = JSON.parse(res_summary.body)
json_summary['topologies'].each do |topology|
topologies.push(
{'name' => topology['name'],
'id' => topology['id'],}
)
end
topologies.each do |topology|
uri = URI::parse("#{host}:#{port}/api/v1/topology/#{topology['id']}")
req = Net::HTTP::Get.new(uri)
res = Net::HTTP.start(uri.hostname, uri.port) {|http|
http.request(req)}
json = JSON.parse(res.body)
latency = json['topologyStats'][0]['completeLatency'].to_i
if latency > max_latency
max_latency = latency
end
message += "#{topology['name']}: #{latency}ms; "
end
if max_latency >= critical_latency
exit_code = 2
elsif max_latency >= warning_latency
exit_code = 1
else
exit_code = 0
end
puts message
exit exit_code