Debian Codegraph @

check_haproxy_stats/check_haproxy_stats.pl

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 #!/usr/bin/env perl
2 # vim: se et ts=4:
3
4 #
5 # Copyright (C) 2012, Giacomo Montagner <[email protected]>
6 # 2015, Yann Fertat, Romain Dessort, Jeff Palmer,
7 # Christophe Drevet-Droguet <[email protected]>
8 #
9 # This program is free software; you can redistribute it and/or modify it
10 # under the same terms as Perl 5.10.1.
11 # For more details, see http://dev.perl.org/licenses/artistic.html
12 #
13 # This program is distributed in the hope that it will be
14 # useful, but without any warranty; without even the implied
15 # warranty of merchantability or fitness for a particular purpose.
16 #
17
18 our $VERSION = "1.1.3";
19
20 open(STDERR, ">&STDOUT");
21
22 # CHANGELOG:
23 # 1.0.0 - first release
24 # 1.0.1 - fixed empty message if all proxies are OK
25 # 1.0.2 - add perfdata
26 # 1.0.3 - redirect stderr to stdout
27 # 1.0.4 - fix undef vars
28 # 1.0.5 - fix thresholds
29 # 1.1.0 - support for HTTP interface
30 # 1.1.1 - drop perl 5.10 requirement
31 # 1.1.2 - add support for ignoring DRAIN state
32 # 1.1.3 - add support ignoring hosts matching a regex
33
34 use strict;
35 use warnings;
36 use File::Basename qw/basename/;
37 use IO::Socket::UNIX;
38 use Getopt::Long;
39 my $lwp = eval {
40 require LWP::Simple;
41 LWP::Simple->import;
42 1;
43 };
44
45 sub usage {
46 my $me = basename $0;
47 print <<EOU;
48 NAME
49 $me - check haproxy stats for errors, using UNIX socket interface
50
51 SYNOPSIS
52 $me [OPTIONS]
53
54 DESCRIPTION
55 Get haproxy statistics via UNIX socket and parse information searching for errors.
56
57 OPTIONS
58 -c, --critical
59 Set critical threshold for sessions number (chacks current number of sessions
60 against session limit, if enforced) to the specified percentage.
61 If no session limit (slim) was specified for the given proxy, this option has
62 no effect.
63
64 -d, --dump
65 Just dump haproxy stats and exit;
66
67 -h, --help
68 Print this message.
69
70 -m, --ignore-maint
71 Assume servers in MAINT state to be ok.
72
73 -i, --ignore-regex
74 Ignore servers that match the given regex.
75
76 -n, --ignore-drain
77 Assume servers in DRAIN state to be ok.
78
79 -p, --proxy
80 Check only named proxies, not every one. Use comma to separate proxies
81 in list.
82
83 -P, --no-proxy
84 Do not check named proxies. Use comma to separate proxies in list.
85
86 -s, --sock, --socket
87 Use named UNIX socket instead of default (/var/run/haproxy.sock)
88
89 -U, --url
90 Use HTTP URL instead of socket. The LWP::Simple perl module is used if
91 available. Otherwise, it falls back to using the external command `curl`.
92
93 -u, --user, --username
94 Username for the HTTP URL
95
96 -x, --pass, --password
97 Password for the HTTP URL
98
99 -w, --warning
100 Set warning threshold for sessions number to the specified percentage (see -c)
101
102 CHECKS AND OUTPUT
103 $me checks every proxy (or the named ones, if -p was given)
104 for status. It returns an error if any of the checked FRONTENDs is not OPEN,
105 any of the checked BACKENDs is not UP, or any of the checkes servers is not UP;
106 $me reports any problem it found.
107
108 EXAMPLES
109 $me -s /var/spool/haproxy/sock
110 Use /var/spool/haproxy/sock to communicate with haproxy.
111
112 $me -p proxy1,proxy2 -w 60 -c 80
113 Check only proxies named "proxy1" and "proxy2", and set sessions number
114 thresholds to 60% and 80%.
115
116 AUTHOR
117 Written by Giacomo Montagner
118
119 REPORTING BUGS
120 Please report any bug to bugs\@entirelyunlike.net
121
122 COPYRIGHT
123 Copyright (C) 2012 Giacomo Montagner <giacomo\@entirelyunlike.net>.
124 $me is distributed under GPL and the Artistic License 2.0
125
126 SEE ALSO
127 Check out online haproxy documentation at <http://haproxy.1wt.eu/>
128
129 EOU
130 }
131
132 my %check_statuses = (
133 UNK => "unknown",
134 INI => "initializing",
135 SOCKERR => "socket error",
136 L4OK => "layer 4 check OK",
137 L4CON => "connection error",
138 L4TMOUT => "layer 1-4 timeout",
139 L6OK => "layer 6 check OK",
140 L6TOUT => "layer 6 (SSL) timeout",
141 L6RSP => "layer 6 protocol error",
142 L7OK => "layer 7 check OK",
143 L7OKC => "layer 7 conditionally OK",
144 L7TOUT => "layer 7 (HTTP/SMTP) timeout",
145 L7RSP => "layer 7 protocol error",
146 L7STS => "layer 7 status error",
147 );
148
149 my @status_names = (qw/OK WARNING CRITICAL UNKNOWN/);
150
151 # Defaults
152 my $swarn = 80.0;
153 my $scrit = 90.0;
154 my $sock = "/var/run/haproxy.sock";
155 my $url;
156 my $user = '';
157 my $pass = '';
158 my $dump;
159 my $ignore_maint;
160 my $ignore_drain;
161 my $ignore_regex;
162 my $proxy;
163 my $no_proxy;
164 my $help;
165
166 # Read command line
167 Getopt::Long::Configure ("bundling");
168 GetOptions (
169 "c|critical=i" => \$scrit,
170 "d|dump" => \$dump,
171 "h|help" => \$help,
172 "m|ignore-maint" => \$ignore_maint,
173 "n|ignore-drain" => \$ignore_drain,
174 "p|proxy=s" => \$proxy,
175 "i|ignore-regex=s" => \$ignore_regex,
176 "P|no-proxy=s" => \$no_proxy,
177 "s|sock|socket=s" => \$sock,
178 "U|url=s" => \$url,
179 "u|user|username=s" => \$user,
180 "x|pass|password=s" => \$pass,
181 "w|warning=i" => \$swarn,
182 );
183
184 # Want help?
185 if ($help) {
186 usage;
187 exit 3;
188 }
189
190 my $haproxy;
191 if ($url and $lwp) {
192 my $geturl = $url;
193 if ($user ne '') {
194 $url =~ /^([^:]*:\/\/)(.*)/;
195 $geturl = $1.$user.':'.$pass.'@'.$2;
196 }
197 $geturl .= ';csv';
198 $haproxy = get($geturl);
199 } elsif ($url) {
200 my $haproxyio;
201 my $getcmd = "curl --insecure -s --fail "
202 . "--user '$user:$pass' '".$url.";csv'";
203 open $haproxyio, "-|", $getcmd;
204 while (<$haproxyio>) {
205 $haproxy .= $_;
206 }
207 close($haproxyio);
208 } else {
209 # Connect to haproxy socket and get stats
210 my $haproxyio = new IO::Socket::UNIX (
211 Peer => $sock,
212 Type => SOCK_STREAM,
213 );
214 die "Unable to connect to haproxy socket: $sock\n$@" unless $haproxyio;
215 print $haproxyio "show stat\n" or die "Print to socket failed: $!";
216 $haproxy = '';
217 while (<$haproxyio>) {
218 $haproxy .= $_;
219 }
220 close($haproxyio);
221 }
222
223 # Dump stats and exit if requested
224 if ($dump) {
225 print($haproxy);
226 exit 0;
227 }
228
229 # Get labels from first output line and map them to their position in the line
230 my @hastats = ( split /\n/, $haproxy );
231 my $labels = $hastats[0];
232 die "Unable to retrieve haproxy stats" unless $labels;
233 chomp($labels);
234 $labels =~ s/^# // or die "Data format not supported.";
235 my @labels = split /,/, $labels;
236 {
237 no strict "refs";
238 my $idx = 0;
239 map { $$_ = $idx++ } @labels;
240 }
241
242 # Variables I will use from here on:
243 our $pxname;
244 our $svname;
245 our $status;
246 our $slim;
247 our $scur;
248
249 my @proxies = split ',', $proxy if $proxy;
250 my @no_proxies = split ',', $no_proxy if $no_proxy;
251 my $exitcode = 0;
252 my $msg;
253 my $checked = 0;
254 my $perfdata = "";
255
256 # Remove excluded proxies from the list if both -p and -P options are
257 # specified.
258 my %hash;
259 @hash{@no_proxies} = undef;
260 @proxies = grep{ not exists $hash{$_} } @proxies;
261
262 foreach (@hastats) {
263 chomp;
264 next if /^#/;
265 next if /^[[:space:]]*$/;
266 my @data = split /,/, $_;
267 if (@proxies) { next unless grep {$data[$pxname] eq $_} @proxies; };
268 if (@no_proxies) { next if grep {$data[$pxname] eq $_} @no_proxies; };
269
270 # Is session limit enforced?
271 if ($data[$slim]) {
272 $perfdata .= sprintf "%s-%s=%u;%u;%u;0;%u;", $data[$pxname], $data[$svname], $data[$scur], $swarn * $data[$slim] / 100, $scrit * $data[$slim] / 100, $data[$slim];
273
274 # Check current session # against limit
275 my $sratio = $data[$scur]/$data[$slim];
276 if ($sratio >= $scrit / 100 || $sratio >= $swarn / 100) {
277 $exitcode = $sratio >= $scrit / 100 ? 2 :
278 $exitcode < 2 ? 1 : $exitcode;
279 $msg .= sprintf "%s:%s sessions: %.2f%%; ", $data[$pxname], $data[$svname], $sratio * 100;
280 }
281 }
282
283 # Check of BACKENDS
284 if ($data[$svname] eq 'BACKEND') {
285 next if ($ignore_regex && $data[$pxname] =~ ".*${ignore_regex}.*");
286 if ($data[$status] ne 'UP') {
287 $msg .= sprintf "BACKEND: %s is %s; ", $data[$pxname], $data[$status];
288 $exitcode = 2;
289 }
290 # Check of FRONTENDS
291 } elsif ($data[$svname] eq 'FRONTEND') {
292 if ($data[$status] ne 'OPEN') {
293 $msg .= sprintf "FRONTEND: %s is %s; ", $data[$pxname], $data[$status];
294 $exitcode = 2;
295 }
296 # Check of servers
297 } else {
298 if ($data[$status] ne 'UP') {
299 next if ($ignore_maint && $data[$status] eq 'MAINT');
300 next if ($ignore_drain && $data[$status] eq 'DRAIN');
301 next if ($ignore_regex && $data[$svname] =~ ".*${ignore_regex}.*");
302 next if $data[$status] eq 'no check'; # Ignore server if no check is configured to be run
303 next if $data[$svname] eq 'sock-1';
304 $exitcode = 2;
305 our $check_status;
306 $msg .= sprintf "server: %s:%s is %s", $data[$pxname], $data[$svname], $data[$status];
307 $msg .= sprintf " (check status: %s)", $check_statuses{$data[$check_status]} if $check_statuses{$data[$check_status]};
308 $msg .= "; ";
309 }
310 }
311 ++$checked;
312 }
313
314 unless ($msg) {
315 $msg = @proxies ? sprintf("checked proxies: %s", join ', ', sort @proxies) : "checked $checked proxies.";
316 }
317 print "Check haproxy $status_names[$exitcode] - $msg|$perfdata\n";
318 exit $exitcode;
319