Skip to content

Commit 7327538

Browse files
authored
Merge pull request #8 from pulecp/master
fix issue #7
2 parents b9b4a3b + 9c0bf4b commit 7327538

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

Diff for: check_haproxy.rb

+43-4
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
CRITICAL = 2
1111
UNKNOWN = 3
1212

13+
1314
status = ['OK', 'WARN', 'CRIT', 'UNKN']
1415

1516
@proxies = []
1617
@errors = []
1718
@perfdata = []
1819
exit_code = OK
20+
http_error_critical = false
1921

2022
options = OpenStruct.new
2123
options.proxies = []
@@ -53,6 +55,23 @@
5355
options.critical = v
5456
end
5557

58+
opts.on( '-s', '--ssl', 'Enable TLS/SSL' ) do
59+
require 'openssl'
60+
end
61+
62+
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
69+
end
70+
71+
opts.on( '--http-error-critical', 'Throw critical when connection to HAProxy is refused or returns error code' ) do
72+
http_error_critical = true
73+
end
74+
5675
opts.on( '-h', '--help', 'Display this screen' ) do
5776
puts opts
5877
exit 3
@@ -85,16 +104,28 @@
85104
exit UNKNOWN
86105
end
87106

107+
108+
tries = 2
109+
88110
begin
89111
f = open(options.url, :http_basic_authentication => [options.user, options.password])
90112
rescue OpenURI::HTTPError => e
91113
puts "ERROR: #{e.message}"
92-
exit UNKNOWN
114+
http_error_critical ? exit CRITICAL : exit UNKNOWN
93115
rescue Errno::ECONNREFUSED => e
94116
puts "ERROR: #{e.message}"
95-
exit UNKNOWN
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
125+
end
96126
end
97127

128+
98129
f.each do |line|
99130

100131
if line =~ /^# /
@@ -115,7 +146,11 @@
115146
perf_id = "#{row['pxname']}".downcase
116147

117148
if row['svname'] == 'FRONTEND'
118-
session_percent_usage = row['scur'].to_i * 100 / row['slim'].to_i
149+
if row['slim'].to_i == 0
150+
session_percent_usage = 0
151+
else
152+
session_percent_usage = row['scur'].to_i * 100 / row['slim'].to_i
153+
end
119154
@perfdata << "#{perf_id}_sessions=#{session_percent_usage}%;#{options.warning ? options.warning : ""};#{options.critical ? options.critical : ""};;"
120155
@perfdata << "#{perf_id}_rate=#{row['rate']};;;;#{row['rate_max']}"
121156
if options.critical && session_percent_usage > options.critical.to_i
@@ -159,7 +194,11 @@
159194
@errors << message
160195
exit_code = WARNING if exit_code == OK || exit_code == UNKNOWN
161196
else
162-
session_percent_usage = row['scur'].to_i * 100 / row['slim'].to_i
197+
if row['slim'].to_i == 0
198+
session_percent_usage = 0
199+
else
200+
session_percent_usage = row['scur'].to_i * 100 / row['slim'].to_i
201+
end
163202
@perfdata << "#{perf_id}-#{row['svname']}_sessions=#{session_percent_usage}%;;;;"
164203
@perfdata << "#{perf_id}-#{row['svname']}_rate=#{row['rate']};;;;#{row['rate_max']}"
165204
end

0 commit comments

Comments
 (0)