Skip to content

Commit 0a1caf1

Browse files
authored
Merge pull request #13 from benprew/refactor
Refactor check_haproxy.rb
2 parents 94d1cbb + 4aec764 commit 0a1caf1

9 files changed

+404
-65
lines changed

Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.PHONY: test
2+
3+
test:
4+
test/test_check_haproxy.sh

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
nagios-checks
22
=============
33

4+
[![Build Status](https://travis-ci.org/benprew/nagios-checks.svg?branch=master)](https://travis-ci.org/benprew/nagios-checks)
5+
46
Various Nagios check scripts.
57

68
check_haproxy

check_haproxy.rb

+40-65
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
@proxies = []
1717
@errors = []
1818
@perfdata = []
19-
exit_code = OK
2019
options = OpenStruct.new
2120
options.proxies = []
2221

@@ -138,81 +137,57 @@ def haproxy_response(options)
138137
end
139138
end
140139

140+
def exit_code(code=0)
141+
@exit_code ||= OK
142+
@exit_code = code if code > @exit_code
143+
@exit_code
144+
end
145+
146+
header = nil
147+
141148
haproxy_response(options).each do |line|
149+
142150
if line =~ /^# /
143-
HAPROXY_COLUMN_NAMES = line[2..-1].split(',')
151+
header = line[2..-1].split(',')
144152
next
145-
elsif !defined? HAPROXY_COLUMN_NAMES
153+
elsif !defined? header
146154
puts "ERROR: CSV header is missing"
147155
exit UNKNOWN
148156
end
149157

150-
row = HAPROXY_COLUMN_NAMES.zip(CSV.parse(line)[0]).reduce({}) { |hash, val| hash.merge({val[0] => val[1]}) }
158+
row = header.zip(CSV.parse_line(line)).reduce({}) { |hash, val| hash.merge({val[0] => val[1]}) }
151159

152160
next unless options.proxies.empty? || options.proxies.include?(row['pxname'])
153161
next if ['statistics', 'admin_stats', 'stats'].include? row['pxname']
162+
next if row['status'] == 'no check'
154163

155164
role = row['act'].to_i > 0 ? 'active ' : (row['bck'].to_i > 0 ? 'backup ' : '')
156-
message = sprintf("%s: %s %s%s", row['pxname'], row['status'], role, row['svname'])
157-
perf_id = "#{row['pxname']}".downcase
158-
159-
if row['svname'] == 'FRONTEND'
160-
if row['slim'].to_i == 0
161-
session_percent_usage = 0
162-
else
163-
session_percent_usage = row['scur'].to_i * 100 / row['slim'].to_i
164-
end
165-
@perfdata << "#{perf_id}_sessions=#{session_percent_usage}%;#{options.warning ? options.warning : ""};#{options.critical ? options.critical : ""};;"
166-
@perfdata << "#{perf_id}_rate=#{row['rate']};;;;#{row['rate_max']}"
167-
if options.critical && session_percent_usage > options.critical.to_i
168-
@errors << sprintf("%s has way too many sessions (%s/%s) on %s proxy",
169-
row['svname'],
170-
row['scur'],
171-
row['slim'],
172-
row['pxname'])
173-
exit_code = CRITICAL
174-
elsif options.warning && session_percent_usage > options.warning.to_i
175-
@errors << sprintf("%s has too many sessions (%s/%s) on %s proxy",
176-
row['svname'],
177-
row['scur'],
178-
row['slim'],
179-
row['pxname'])
180-
exit_code = WARNING if exit_code == OK || exit_code == UNKNOWN
181-
end
182-
183-
if row['status'] != 'OPEN' && !row['status'].include?('UP')
184-
@errors << message
185-
exit_code = CRITICAL
186-
end
187-
188-
elsif row['svname'] == 'BACKEND'
189-
# It has no point to check sessions number for backends, against the alert limits,
190-
# as the SLIM number is actually coming from the "fullconn" parameter.
191-
# So we just collect perfdata. See the following url for more info:
192-
# http://comments.gmane.org/gmane.comp.web.haproxy/9715
193-
current_sessions = row['scur'].to_i
194-
@perfdata << "#{perf_id}_sessions=#{current_sessions};;;;"
195-
@perfdata << "#{perf_id}_rate=#{row['rate']};;;;#{row['rate_max']}"
196-
if row['status'] != 'OPEN' && !row['status'].include?('UP')
197-
@errors << message
198-
exit_code = CRITICAL
199-
end
165+
if row['slim'].to_i == 0
166+
session_percent_usage = 0
167+
else
168+
session_percent_usage = row['scur'].to_i * 100 / row['slim'].to_i
169+
end
200170

201-
elsif row['status'] != 'no check'
202-
@proxies << message
171+
proxy_name = sprintf("%s %s %s %s", row['pxname'], row['svname'], row['status'], role)
172+
message = sprintf("%s\tsess=%s/%s(%d%%) smax=%s",
173+
proxy_name,
174+
row['scur'],
175+
row['slim'] || '-1',
176+
session_percent_usage,
177+
row['smax'])
178+
@proxies << message
179+
180+
if row['status'].include?('DOWN')
181+
@errors << message
182+
exit_code CRITICAL
183+
end
203184

204-
if !row['status'].include?('UP')
205-
@errors << message
206-
exit_code = WARNING if exit_code == OK || exit_code == UNKNOWN
207-
else
208-
if row['slim'].to_i == 0
209-
session_percent_usage = 0
210-
else
211-
session_percent_usage = row['scur'].to_i * 100 / row['slim'].to_i
212-
end
213-
@perfdata << "#{perf_id}-#{row['svname']}_sessions=#{session_percent_usage}%;;;;"
214-
@perfdata << "#{perf_id}-#{row['svname']}_rate=#{row['rate']};;;;#{row['rate_max']}"
215-
end
185+
if options.critical && session_percent_usage >= options.critical.to_i
186+
@errors << sprintf("%s - too many sessions %s/%s(%d%%)", proxy_name, row['scur'], row['slim'], session_percent_usage)
187+
exit_code CRITICAL
188+
elsif options.warning && session_percent_usage >= options.warning.to_i
189+
@errors << sprintf("%s - too many sessions %s/%s(%d%%)", proxy_name, row['scur'], row['slim'], session_percent_usage)
190+
exit_code WARNING
216191
end
217192
end
218193

@@ -222,11 +197,11 @@ def haproxy_response(options)
222197

223198
if @proxies.length == 0
224199
@errors << "No proxies listed as up or down"
225-
exit_code = UNKNOWN if exit_code == OK
200+
exit_code UNKNOWN
226201
end
227202

228-
puts "HAPROXY " + status[exit_code] + ": " + @errors.join('; ') + "|" + @perfdata.join(" ")
229-
puts @proxies
203+
puts "HAPROXY " + status[exit_code] + ": " + @errors.join('; ')
204+
puts @proxies.sort
230205

231206
exit exit_code
232207

test/haproxy/1wt_eu.csv;

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# pxname,svname,qcur,qmax,scur,smax,slim,stot,bin,bout,dreq,dresp,ereq,econ,eresp,wretr,wredis,status,weight,act,bck,chkfail,chkdown,lastchg,downtime,qlimit,pid,iid,sid,throttle,lbtot,tracked,type,rate,rate_lim,rate_max,check_status,check_code,check_duration,hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,req_rate,req_rate_max,req_tot,cli_abrt,srv_abrt,comp_in,comp_out,comp_byp,comp_rsp,lastsess,last_chk,last_agt,qtime,ctime,rtime,ttime,agent_status,agent_code,agent_duration,check_desc,agent_desc,check_rise,check_fall,check_health,agent_rise,agent_fall,agent_health,addr,cookie,mode,algo,conn_rate,conn_rate_max,conn_tot,intercepted,dcon,dses,
2+
http-in,FRONTEND,,,5,25,100,3161987,609712281,6469035959,2232237,0,10310,,,,,OPEN,,,,,,,,,1,2,0,,,,0,3,0,89,,,,0,876125,59515,55972,244,2230104,,3,89,3211133,,,0,0,0,0,,,,,,,,,,,,,,,,,,,,,http,,3,89,3181215,354602,0,0,
3+
http-in,IPv4-direct,,,1,25,100,2291028,367022152,1064567588,2219307,0,17,,,,,OPEN,,,,,,,,,1,2,1,,,,3,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,http,,,,,,0,0,
4+
http-in,IPv4-cached,,,0,14,100,812576,223558890,4917830438,6637,0,1,,,,,OPEN,,,,,,,,,1,2,2,,,,3,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,http,,,,,,0,0,
5+
http-in,IPv6-direct,,,0,12,100,45357,15381777,292639605,2665,0,10292,,,,,OPEN,,,,,,,,,1,2,3,,,,3,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,http,,,,,,0,0,
6+
http-in,local,,,0,0,100,0,0,0,0,0,0,,,,,OPEN,,,,,,,,,1,2,4,,,,3,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,http,,,,,,0,0,
7+
http-in,local-https,,,0,5,100,32254,3749462,193998328,3628,0,0,,,,,OPEN,,,,,,,,,1,2,5,,,,3,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,http,,,,,,0,0,
8+
www,www,0,0,0,14,20,459085,169826821,3914623823,,0,,10,15,46,2,DOWN,1,1,0,26,7,885894,895155,,1,3,1,,459078,,2,0,,68,L7STS,404,2,0,410203,11256,37455,1,0,,,,,42593,2,,,,,885895,Not Found,,0,3,37,513,,,,Layer7 wrong status,,2,3,0,,,,,,http,,,,,,,,
9+
www,bck,0,1,4,10,10,97391,38690561,1971792825,,0,,2,0,2,0,UP,1,0,1,9,2,4028243,1154,,1,3,2,,97394,,2,0,,59,L7OK,200,2,0,90656,1418,5292,0,0,,,,,19650,2,,,,,20,OK,,0,1,17,1044,,,,Layer7 check passed,,2,5,6,,,,,,http,,,,,,,,
10+
www,BACKEND,0,0,0,21,100,558269,208605416,5886751764,1716,0,,71,15,48,2,UP,1,0,1,,2,4028243,1154,,1,3,0,,556472,,1,0,,68,,,,0,500859,12674,44473,220,43,,,,558269,62274,4,0,0,0,0,20,,,0,1,17,1044,,,,,,,,,,,,,,http,,,,,,,,
11+
git,www,0,0,0,2,2,1464,580155,27040897,,0,,0,0,0,0,DOWN,1,1,0,21,7,885894,895119,,1,4,1,,1334,,2,0,,2,L7STS,404,1,0,1210,0,254,0,0,,,,,15,0,,,,,886879,Not Found,,421,1,1089,2042,,,,Layer7 wrong status,,2,3,0,,,,,,http,,,,,,,,
12+
git,bck,0,7,0,2,2,356,129900,11454350,,0,,0,0,0,0,UP,1,0,1,9,2,4028243,1160,,1,4,2,,356,,2,0,,2,L7OK,200,2,0,325,0,31,0,0,,,,,27,0,,,,,3209,OK,,52,1,537,1334,,,,Layer7 check passed,,2,3,4,,,,,,http,,,,,,,,
13+
git,BACKEND,0,10,0,13,2,1820,750189,38495247,0,0,,0,0,0,0,UP,1,0,1,,2,4028243,1160,,1,4,0,,1690,,1,0,,4,,,,0,1535,0,285,0,0,,,,1820,42,0,0,0,0,0,3209,,,262,1,1080,2352,,,,,,,,,,,,,,http,,,,,,,,
14+
demo,BACKEND,0,0,1,10,20,19867,7087787,483986641,40,0,,24,0,0,0,UP,0,0,0,,0,5344748,,,1,17,0,,0,,1,1,,8,,,,0,19129,243,0,24,470,,,,19866,86,158,0,0,0,0,0,,,0,0,0,2065,,,,,,,,,,,,,,http,,,,,,,,

0 commit comments

Comments
 (0)