-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathcheck_haproxy.rb
executable file
·248 lines (207 loc) · 7.54 KB
/
check_haproxy.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/ruby
require 'optparse'
require 'open-uri'
require 'ostruct'
require 'csv'
OK = 0
WARNING = 1
CRITICAL = 2
UNKNOWN = 3
status = ['OK', 'WARN', 'CRIT', 'UNKN']
@proxies = []
@errors = []
@perfdata = []
exit_code = OK
options = OpenStruct.new
options.proxies = []
op = OptionParser.new do |opts|
opts.banner = 'Usage: check_haproxy.rb [options]'
opts.separator ''
opts.separator 'Specific options:'
# Required arguments
opts.on("-u", "--url URL", "Statistics URL to check (eg. http://demo.1wt.eu/)") do |v|
options.url = v
options.url += "/;csv" unless options.url =~ /;/
end
# Optional Arguments
opts.on("-p", "--proxies [PROXIES]", "Only check these proxies (eg. proxy1,proxy2,proxylive)") do |v|
options.proxies = v.split(/,/)
end
opts.on("-U", "--user [USER]", "Basic auth user to login as") do |v|
options.user = v
end
opts.on("-P", "--password [PASSWORD]", "Basic auth password") do |v|
options.password = v
end
opts.on("-w", "--warning [WARNING]", "Pct of active sessions (eg 85, 90)") do |v|
options.warning = v
end
opts.on("-c", "--critical [CRITICAL]", "Pct of active sessions (eg 90, 95)") do |v|
options.critical = v
end
opts.on( '-k', '--insecure', 'Allow insecure TLS/SSL connections' ) do
options.insecure_ssl = true
end
opts.on( '--http-error-critical', 'Throw critical when connection to HAProxy is refused or returns error code' ) do
options.http_error_critical = true
end
opts.on( '-h', '--help', 'Display this screen' ) do
puts opts
exit 3
end
end
op.parse!
unless options.url
puts 'ERROR: URL is required'
puts op
exit UNKNOWN
end
if options.warning && !options.warning.to_i.between?(0, 100)
puts 'ERROR: warning must be between 0 and 100'
puts op
exit UNKNOWN
end
if options.critical && !options.critical.to_i.between?(0, 100)
puts 'ERROR: critical must be between 0 and 100'
puts op
exit UNKNOWN
end
if options.warning && options.critical && options.warning.to_i > options.critical.to_i
puts 'ERROR: warning must be below critical'
puts op
exit UNKNOWN
end
def open_options(options)
open_opts = {
:http_basic_authentication => [options.user, options.password]
}
# allows https with invalid certificate on ruby 1.9
# src: http://snippets.aktagon.com/snippets/370-hack-for-using-openuri-with-ssl
if options.insecure_ssl && RUBY_VERSION =~ /1\.9/
open_opts[:ssl_verify_mode] = OpenSSL::SSL::VERIFY_NONE
end
open_opts
end
def haproxy_response(options)
tries = 2
if options.url =~ /https/
require 'openssl'
# allows https with invalid certificate on ruby 1.8
# src: http://snippets.aktagon.com/snippets/370-hack-for-using-openuri-with-ssl
if options.insecure_ssl && RUBY_VERSION =~ /1\.8/
OpenSSL::SSL.const_set :VERIFY_PEER, OpenSSL::SSL::VERIFY_NONE
end
end
begin
open(options.url, open_options(options))
rescue OpenURI::HTTPError => e
puts "ERROR: #{e.message}"
options.http_error_critical ? exit(CRITICAL) : exit(UNKNOWN)
rescue Errno::ECONNREFUSED => e
puts "ERROR: #{e.message}"
options.http_error_critical ? exit(CRITICAL) : exit(UNKNOWN)
rescue RuntimeError => e
if e.message =~ /redirection forbidden/
options.url = e.message.gsub(/.*-> (.*)/, '\1') # extract redirect URL
retry if (tries -= 1) > 0
raise
else
exit UNKNOWN
end
end
end
haproxy_response(options).each do |line|
if line =~ /^# /
HAPROXY_COLUMN_NAMES = line[2..-1].split(',')
next
elsif !defined? HAPROXY_COLUMN_NAMES
puts "ERROR: CSV header is missing"
exit UNKNOWN
end
row = HAPROXY_COLUMN_NAMES.zip(CSV.parse(line)[0]).reduce({}) { |hash, val| hash.merge({val[0] => val[1]}) }
next unless options.proxies.empty? || options.proxies.include?(row['pxname'])
next if ['statistics', 'admin_stats', 'stats'].include? row['pxname']
role = row['act'].to_i > 0 ? 'active ' : (row['bck'].to_i > 0 ? 'backup ' : '')
message = sprintf("%s: %s %s%s", row['pxname'], row['status'], role, row['svname'])
perf_id = "#{row['pxname']}".downcase
if row['svname'] == 'FRONTEND'
if row['slim'].to_i == 0
session_percent_usage = 0
else
session_percent_usage = row['scur'].to_i * 100 / row['slim'].to_i
end
@perfdata << "#{perf_id}_sessions=#{session_percent_usage}%;#{options.warning ? options.warning : ""};#{options.critical ? options.critical : ""};;"
@perfdata << "#{perf_id}_rate=#{row['rate']};;;;#{row['rate_max']}"
if options.critical && session_percent_usage > options.critical.to_i
@errors << sprintf("%s has way too many sessions (%s/%s) on %s proxy",
row['svname'],
row['scur'],
row['slim'],
row['pxname'])
exit_code = CRITICAL
elsif options.warning && session_percent_usage > options.warning.to_i
@errors << sprintf("%s has too many sessions (%s/%s) on %s proxy",
row['svname'],
row['scur'],
row['slim'],
row['pxname'])
exit_code = WARNING if exit_code == OK || exit_code == UNKNOWN
end
if row['status'] != 'OPEN' && row['status'] != 'UP'
@errors << message
exit_code = CRITICAL
end
elsif row['svname'] == 'BACKEND'
# It has no point to check sessions number for backends, against the alert limits,
# as the SLIM number is actually coming from the "fullconn" parameter.
# So we just collect perfdata. See the following url for more info:
# http://comments.gmane.org/gmane.comp.web.haproxy/9715
current_sessions = row['scur'].to_i
@perfdata << "#{perf_id}_sessions=#{current_sessions};;;;"
@perfdata << "#{perf_id}_rate=#{row['rate']};;;;#{row['rate_max']}"
if row['status'] != 'OPEN' && row['status'] != 'UP'
@errors << message
exit_code = CRITICAL
end
elsif row['status'] != 'no check'
@proxies << message
if row['status'] != 'UP'
@errors << message
exit_code = WARNING if exit_code == OK || exit_code == UNKNOWN
else
if row['slim'].to_i == 0
session_percent_usage = 0
else
session_percent_usage = row['scur'].to_i * 100 / row['slim'].to_i
end
@perfdata << "#{perf_id}-#{row['svname']}_sessions=#{session_percent_usage}%;;;;"
@perfdata << "#{perf_id}-#{row['svname']}_rate=#{row['rate']};;;;#{row['rate_max']}"
end
end
end
if @errors.length == 0
@errors << sprintf("%d proxies found", @proxies.length)
end
if @proxies.length == 0
@errors << "No proxies listed as up or down"
exit_code = UNKNOWN if exit_code == OK
end
puts "HAPROXY " + status[exit_code] + ": " + @errors.join('; ') + "|" + @perfdata.join(" ")
puts @proxies
exit exit_code
=begin
Copyright (C) 2013 Ben Prew
Copyright (C) 2013 Mark Ruys, Peercode <[email protected]>
Copyright (C) 2015 Hector Sanjuan. Nugg.ad <[email protected]>
Copyright (C) 2015 Roger Torrentsgeneros <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
=end