-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_cli.rb
executable file
·84 lines (83 loc) · 2.04 KB
/
app_cli.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
#!/usr/bin/env ruby
require 'json'
require 'open-uri'
#require 'erb'
#require 'sinatra/reloader'
require 'graphite-api'
#
# Function
#
$rtc_threshold = 10
$bql_threshold = 256
$btqs_threshold = 2147483648
$graphite_host = "Graphite_host:2003"
#
# Methods
#
def get_td_status(url)
status_url = "http://#{url}:24220/api/plugins.json"
begin
status_html = open("#{status_url}").read
rescue => exception
case exception
when OpenURI::HTTPError
puts "status URL does not access."
stats = "critical"
exit 1
end
else
JSON.parser.new(status_html).parse()
end
end
#
def post_graphite(host, type, rtc, bql, btqs)
client = GraphiteAPI.new( graphite: $graphite_host )
client.metrics(
"#{host}.#{type}.retry_count" => rtc,
"#{host}.#{type}.buffer_queue_length" => bql,
"#{host}.#{type}.buffer_total_queued_size" => btqs
)
end
#
def check_stats(rtc, bql, btqs)
if ( rtc >= $rtc_threshold or bql >= $bql_threshold or btqs >= $btqs_threshold )
return "warning"
else
return "normally"
end
end
#
def write_result(host, type, rtc, bql, btqs, stats)
result = {}
result[:host] = host
result[:type] = type
result[:retry_count] = rtc
result[:buffer_queue_length] = bql
result[:buffer_total_queued_size] = btqs
result[:stats] = stats
puts result
end
#
# Main
#
file = open("./list.txt")
while text = file.gets do
arr = text.split(",")
parsed = get_td_status("#{arr[0]}")
configs = parsed['plugins'].size.to_i
host = "#{arr[0]}".gsub(".", "-")
#
(0..(configs-1)).each do |config|
type = parsed['plugins'][config]['type'] rescue nil
rtc = parsed['plugins'][config]['retry_count'] rescue nil
bql = parsed['plugins'][config]['buffer_queue_length'] rescue nil
btqs = parsed['plugins'][config]['buffer_total_queued_size'] rescue nil
#
if ( rtc != nil and bql != nil and btqs != nil )
# post to graphite
post_graphite(host, type, rtc, bql, btqs)
stats = check_stats(rtc, bql, btqs)
end
write_result(host, type, rtc, bql, btqs, stats)
end
end