Skip to content

Commit de6e0b5

Browse files
committed
Enable openssl as needed. fixes #9
1 parent 7327538 commit de6e0b5

File tree

1 file changed

+52
-41
lines changed

1 file changed

+52
-41
lines changed

Diff for: check_haproxy.rb

+52-41
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,14 @@
1717
@errors = []
1818
@perfdata = []
1919
exit_code = OK
20-
http_error_critical = false
21-
2220
options = OpenStruct.new
2321
options.proxies = []
2422

2523
op = OptionParser.new do |opts|
2624
opts.banner = 'Usage: check_haproxy.rb [options]'
2725

28-
opts.separator ""
29-
opts.separator "Specific options:"
26+
opts.separator ''
27+
opts.separator 'Specific options:'
3028

3129
# Required arguments
3230
opts.on("-u", "--url URL", "Statistics URL to check (eg. http://demo.1wt.eu/)") do |v|
@@ -55,21 +53,12 @@
5553
options.critical = v
5654
end
5755

58-
opts.on( '-s', '--ssl', 'Enable TLS/SSL' ) do
59-
require 'openssl'
60-
end
61-
6256
opts.on( '-k', '--insecure', 'Allow insecure TLS/SSL connections' ) do
63-
require 'openssl'
64-
65-
# allows https with invalid certificate on ruby 1.8+
66-
#
67-
# src: also://snippets.aktagon.com/snippets/370-hack-for-using-openuri-with-ssl
68-
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
57+
options.insecure_ssl = true
6958
end
7059

7160
opts.on( '--http-error-critical', 'Throw critical when connection to HAProxy is refused or returns error code' ) do
72-
http_error_critical = true
61+
options.http_error_critical = true
7362
end
7463

7564
opts.on( '-h', '--help', 'Display this screen' ) do
@@ -80,58 +69,80 @@
8069

8170
op.parse!
8271

83-
if !options.url
84-
puts "ERROR: URL is required"
72+
unless options.url
73+
puts 'ERROR: URL is required'
8574
puts op
8675
exit UNKNOWN
8776
end
8877

89-
if options.warning && ! options.warning.to_i.between?(0, 100)
90-
puts "ERROR: warning should be between 0 and 100"
78+
if options.warning && !options.warning.to_i.between?(0, 100)
79+
puts 'ERROR: warning must be between 0 and 100'
9180
puts op
9281
exit UNKNOWN
9382
end
9483

95-
if options.critical && ! options.critical.to_i.between?(0, 100)
96-
puts "ERROR: critical should be between 0 and 100"
84+
if options.critical && !options.critical.to_i.between?(0, 100)
85+
puts 'ERROR: critical must be between 0 and 100'
9786
puts op
9887
exit UNKNOWN
9988
end
10089

10190
if options.warning && options.critical && options.warning.to_i > options.critical.to_i
102-
puts "ERROR: warning should be below critical"
91+
puts 'ERROR: warning must be below critical'
10392
puts op
10493
exit UNKNOWN
10594
end
10695

96+
def open_options(options)
97+
open_opts = {
98+
:http_basic_authentication => [options.user, options.password]
99+
}
107100

108-
tries = 2
109-
110-
begin
111-
f = open(options.url, :http_basic_authentication => [options.user, options.password])
112-
rescue OpenURI::HTTPError => e
113-
puts "ERROR: #{e.message}"
114-
http_error_critical ? exit CRITICAL : exit UNKNOWN
115-
rescue Errno::ECONNREFUSED => e
116-
puts "ERROR: #{e.message}"
117-
http_error_critical ? exit CRITICAL : exit UNKNOWN
118-
rescue Exception => e
119-
if e.message =~ /redirection forbidden/
120-
options.url = e.message.gsub(/.*-> (.*)/, '\1') # extract redirect URL
121-
retry if (tries -= 1) > 0
122-
raise
123-
else
124-
exit UNKNOWN
101+
# allows https with invalid certificate on ruby 1.9
102+
# src: http://snippets.aktagon.com/snippets/370-hack-for-using-openuri-with-ssl
103+
if options.insecure_ssl && RUBY_VERSION =~ /1\.9/
104+
open_opts[:ssl_verify_mode] = OpenSSL::SSL::VERIFY_NONE
125105
end
106+
107+
open_opts
126108
end
127109

110+
def haproxy_response(options)
111+
tries = 2
128112

129-
f.each do |line|
113+
if options.url =~ /https/
114+
require 'openssl'
115+
# allows https with invalid certificate on ruby 1.8
116+
# src: http://snippets.aktagon.com/snippets/370-hack-for-using-openuri-with-ssl
117+
if options.insecure_ssl && RUBY_VERSION =~ /1\.8/
118+
OpenSSL::SSL.const_set :VERIFY_PEER, OpenSSL::SSL::VERIFY_NONE
119+
end
120+
end
121+
122+
begin
123+
open(options.url, open_options(options))
124+
rescue OpenURI::HTTPError => e
125+
puts "ERROR: #{e.message}"
126+
options.http_error_critical ? exit(CRITICAL) : exit(UNKNOWN)
127+
rescue Errno::ECONNREFUSED => e
128+
puts "ERROR: #{e.message}"
129+
options.http_error_critical ? exit(CRITICAL) : exit(UNKNOWN)
130+
rescue RuntimeError => e
131+
if e.message =~ /redirection forbidden/
132+
options.url = e.message.gsub(/.*-> (.*)/, '\1') # extract redirect URL
133+
retry if (tries -= 1) > 0
134+
raise
135+
else
136+
exit UNKNOWN
137+
end
138+
end
139+
end
130140

141+
haproxy_response(options).each do |line|
131142
if line =~ /^# /
132143
HAPROXY_COLUMN_NAMES = line[2..-1].split(',')
133144
next
134-
elsif ! defined? HAPROXY_COLUMN_NAMES
145+
elsif !defined? HAPROXY_COLUMN_NAMES
135146
puts "ERROR: CSV header is missing"
136147
exit UNKNOWN
137148
end

0 commit comments

Comments
 (0)