Skip to content

Commit b9b4a3b

Browse files
committed
Merge pull request benprew#6 from trutx/master
Adding some error management
2 parents 3c46b99 + 41099ab commit b9b4a3b

File tree

1 file changed

+72
-66
lines changed

1 file changed

+72
-66
lines changed

Diff for: check_haproxy.rb

+72-66
Original file line numberDiff line numberDiff line change
@@ -85,75 +85,83 @@
8585
exit UNKNOWN
8686
end
8787

88-
open(options.url, :http_basic_authentication => [options.user, options.password]) do |f|
89-
f.each do |line|
90-
91-
if line =~ /^# /
92-
HAPROXY_COLUMN_NAMES = line[2..-1].split(',')
93-
next
94-
elsif ! defined? HAPROXY_COLUMN_NAMES
95-
puts "ERROR: CSV header is missing"
96-
exit UNKNOWN
88+
begin
89+
f = open(options.url, :http_basic_authentication => [options.user, options.password])
90+
rescue OpenURI::HTTPError => e
91+
puts "ERROR: #{e.message}"
92+
exit UNKNOWN
93+
rescue Errno::ECONNREFUSED => e
94+
puts "ERROR: #{e.message}"
95+
exit UNKNOWN
96+
end
97+
98+
f.each do |line|
99+
100+
if line =~ /^# /
101+
HAPROXY_COLUMN_NAMES = line[2..-1].split(',')
102+
next
103+
elsif ! defined? HAPROXY_COLUMN_NAMES
104+
puts "ERROR: CSV header is missing"
105+
exit UNKNOWN
106+
end
107+
108+
row = HAPROXY_COLUMN_NAMES.zip(CSV.parse(line)[0]).reduce({}) { |hash, val| hash.merge({val[0] => val[1]}) }
109+
110+
next unless options.proxies.empty? || options.proxies.include?(row['pxname'])
111+
next if ['statistics', 'admin_stats', 'stats'].include? row['pxname']
112+
113+
role = row['act'].to_i > 0 ? 'active ' : (row['bck'].to_i > 0 ? 'backup ' : '')
114+
message = sprintf("%s: %s %s%s", row['pxname'], row['status'], role, row['svname'])
115+
perf_id = "#{row['pxname']}".downcase
116+
117+
if row['svname'] == 'FRONTEND'
118+
session_percent_usage = row['scur'].to_i * 100 / row['slim'].to_i
119+
@perfdata << "#{perf_id}_sessions=#{session_percent_usage}%;#{options.warning ? options.warning : ""};#{options.critical ? options.critical : ""};;"
120+
@perfdata << "#{perf_id}_rate=#{row['rate']};;;;#{row['rate_max']}"
121+
if options.critical && session_percent_usage > options.critical.to_i
122+
@errors << sprintf("%s has way too many sessions (%s/%s) on %s proxy",
123+
row['svname'],
124+
row['scur'],
125+
row['slim'],
126+
row['pxname'])
127+
exit_code = CRITICAL
128+
elsif options.warning && session_percent_usage > options.warning.to_i
129+
@errors << sprintf("%s has too many sessions (%s/%s) on %s proxy",
130+
row['svname'],
131+
row['scur'],
132+
row['slim'],
133+
row['pxname'])
134+
exit_code = WARNING if exit_code == OK || exit_code == UNKNOWN
97135
end
98136

99-
row = HAPROXY_COLUMN_NAMES.zip(CSV.parse(line)[0]).reduce({}) { |hash, val| hash.merge({val[0] => val[1]}) }
137+
if row['status'] != 'OPEN' && row['status'] != 'UP'
138+
@errors << message
139+
exit_code = CRITICAL
140+
end
100141

101-
next unless options.proxies.empty? || options.proxies.include?(row['pxname'])
102-
next if ['statistics', 'admin_stats', 'stats'].include? row['pxname']
142+
elsif row['svname'] == 'BACKEND'
143+
# It has no point to check sessions number for backends, against the alert limits,
144+
# as the SLIM number is actually coming from the "fullconn" parameter.
145+
# So we just collect perfdata. See the following url for more info:
146+
# http://comments.gmane.org/gmane.comp.web.haproxy/9715
147+
current_sessions = row['scur'].to_i
148+
@perfdata << "#{perf_id}_sessions=#{current_sessions};;;;"
149+
@perfdata << "#{perf_id}_rate=#{row['rate']};;;;#{row['rate_max']}"
150+
if row['status'] != 'OPEN' && row['status'] != 'UP'
151+
@errors << message
152+
exit_code = CRITICAL
153+
end
103154

104-
role = row['act'].to_i > 0 ? 'active ' : (row['bck'].to_i > 0 ? 'backup ' : '')
105-
message = sprintf("%s: %s %s%s", row['pxname'], row['status'], role, row['svname'])
106-
perf_id = "#{row['pxname']}".downcase
155+
elsif row['status'] != 'no check'
156+
@proxies << message
107157

108-
if row['svname'] == 'FRONTEND'
158+
if row['status'] != 'UP'
159+
@errors << message
160+
exit_code = WARNING if exit_code == OK || exit_code == UNKNOWN
161+
else
109162
session_percent_usage = row['scur'].to_i * 100 / row['slim'].to_i
110-
@perfdata << "#{perf_id}_sessions=#{session_percent_usage}%;#{options.warning ? options.warning : ""};#{options.critical ? options.critical : ""};;"
111-
@perfdata << "#{perf_id}_rate=#{row['rate']};;;;#{row['rate_max']}"
112-
if options.critical && session_percent_usage > options.critical.to_i
113-
@errors << sprintf("%s has way too many sessions (%s/%s) on %s proxy",
114-
row['svname'],
115-
row['scur'],
116-
row['slim'],
117-
row['pxname'])
118-
exit_code = CRITICAL
119-
elsif options.warning && session_percent_usage > options.warning.to_i
120-
@errors << sprintf("%s has too many sessions (%s/%s) on %s proxy",
121-
row['svname'],
122-
row['scur'],
123-
row['slim'],
124-
row['pxname'])
125-
exit_code = WARNING if exit_code == OK || exit_code == UNKNOWN
126-
end
127-
128-
if row['status'] != 'OPEN' && row['status'] != 'UP'
129-
@errors << message
130-
exit_code = CRITICAL
131-
end
132-
133-
elsif row['svname'] == 'BACKEND'
134-
# It has no point to check sessions number for backends, against the alert limits,
135-
# as the SLIM number is actually coming from the "fullconn" parameter.
136-
# So we just collect perfdata. See the following url for more info:
137-
# http://comments.gmane.org/gmane.comp.web.haproxy/9715
138-
current_sessions = row['scur'].to_i
139-
@perfdata << "#{perf_id}_sessions=#{current_sessions};;;;"
140-
@perfdata << "#{perf_id}_rate=#{row['rate']};;;;#{row['rate_max']}"
141-
if row['status'] != 'OPEN' && row['status'] != 'UP'
142-
@errors << message
143-
exit_code = CRITICAL
144-
end
145-
146-
elsif row['status'] != 'no check'
147-
@proxies << message
148-
149-
if row['status'] != 'UP'
150-
@errors << message
151-
exit_code = WARNING if exit_code == OK || exit_code == UNKNOWN
152-
else
153-
session_percent_usage = row['scur'].to_i * 100 / row['slim'].to_i
154-
@perfdata << "#{perf_id}-#{row['svname']}_sessions=#{session_percent_usage}%;;;;"
155-
@perfdata << "#{perf_id}-#{row['svname']}_rate=#{row['rate']};;;;#{row['rate_max']}"
156-
end
163+
@perfdata << "#{perf_id}-#{row['svname']}_sessions=#{session_percent_usage}%;;;;"
164+
@perfdata << "#{perf_id}-#{row['svname']}_rate=#{row['rate']};;;;#{row['rate_max']}"
157165
end
158166
end
159167
end
@@ -176,17 +184,15 @@
176184
Copyright (C) 2013 Ben Prew
177185
Copyright (C) 2013 Mark Ruys, Peercode <[email protected]>
178186
Copyright (C) 2015 Hector Sanjuan. Nugg.ad <[email protected]>
179-
187+
Copyright (C) 2015 Roger Torrentsgeneros <[email protected]>
180188
This program is free software: you can redistribute it and/or modify
181189
it under the terms of the GNU General Public License as published by
182190
the Free Software Foundation, either version 3 of the License, or
183191
(at your option) any later version.
184-
185192
This program is distributed in the hope that it will be useful,
186193
but WITHOUT ANY WARRANTY; without even the implied warranty of
187194
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
188195
GNU General Public License for more details.
189-
190196
You should have received a copy of the GNU General Public License
191197
along with this program. If not, see <http://www.gnu.org/licenses/>.
192198
=end

0 commit comments

Comments
 (0)