-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcheck_haproxy.rb
executable file
·223 lines (180 loc) · 6.43 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
#!/usr/bin/ruby
require 'optparse'
require 'open-uri'
require 'ostruct'
require 'csv'
VERSION = :'3.2.0'
OK = 0
WARNING = 1
CRITICAL = 2
UNKNOWN = 3
status = ['OK', 'WARN', 'CRIT', 'UNKN']
@proxies = []
@errors = { OK => [], WARNING => [], CRITICAL => [], UNKNOWN => [] }
options = OpenStruct.new
options.proxies = nil
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 =~ /csv$/
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('--cookie [COOKIE]', 'Login/Session cookie') do |v|
options.cookie = v
end
opts.on('-h', '--help', 'Display this screen') do
puts opts
exit 3
end
opts.on('--version', 'Show version and exit') do
puts "check_haproxy.rb v#{VERSION}"
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]
}
open_opts['Cookie'] = options.cookie if options.cookie
# 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
return 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
return URI.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
header = nil
haproxy_response(options).each do |line|
if line =~ /^# /
header = line[2..-1].split(',')
next
elsif !header
puts "ERROR: CSV header is missing: #{line}"
exit UNKNOWN
end
row = header.zip(CSV.parse_line(line)).reduce({}) { |hash, val| hash.merge({val[0] => val[1]}) }
next if options.proxies && !options.proxies.include?(row['pxname'])
next if ['statistics', 'admin_stats', 'stats'].include? row['pxname']
next if row['status'] == 'no check'
role = %w[BACKEND FRONTEND].include?(row['svname']) || row['bck'].to_i == 0 ? :act : :bck
if row['slim'].to_i == 0
session_percent_usage = 0
else
session_percent_usage = row['scur'].to_i * 100 / row['slim'].to_i
end
proxy_name = sprintf("%s %s (%s) %s", row['pxname'], row['svname'], role, row['status'])
message = sprintf("%s\tsess=%s/%s(%d%%) smax=%s",
proxy_name,
row['scur'],
row['slim'] || '-1',
session_percent_usage,
row['smax'])
@proxies << message
if role == :act && row['status'] == 'DOWN'
err_level = row['svname'] == 'BACKEND' ? CRITICAL : WARNING
@errors[err_level] << message
end
if options.critical && session_percent_usage >= options.critical.to_i
@errors[CRITICAL] << sprintf("%s - too many sessions %s/%s(%d%%)", proxy_name, row['scur'], row['slim'], session_percent_usage)
elsif options.warning && session_percent_usage >= options.warning.to_i
@errors[WARNING] << sprintf("%s - too many sessions %s/%s(%d%%)", proxy_name, row['scur'], row['slim'], session_percent_usage)
end
end
@errors[OK] << "#{@proxies.length} proxies found"
@errors[UNKNOWN] << "No proxies listed as up or down" if @proxies.empty?
[CRITICAL, WARNING, UNKNOWN, OK].each do |exit_code|
next if @errors[exit_code].empty?
puts "HAPROXY #{status[exit_code]}: #{@errors[exit_code].join('; ')}"
puts @proxies.sort
exit exit_code
end
=begin
Copyright (C) 2013 Ben Prew
Copyright (C) 2013 Mark Ruys, Peercode <mark.ruys@peercode.nl>
Copyright (C) 2015 Hector Sanjuan. Nugg.ad <hector.sanjuan@nugg.ad>
Copyright (C) 2015 Roger Torrentsgeneros <roger.torrentsgeneros@softonic.com>
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