git.lirion.de

Of git, get, and gud

summaryrefslogtreecommitdiffstats
path: root/nagios-plugins-contrib-24.20190301~bpo9+1/check_haproxy_stats/check_haproxy_stats.pl
blob: 9698f7819572d928f49498a19cb546a2ef3fd40b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env perl 
# vim: se et ts=4:

#
# Copyright (C) 2012, Giacomo Montagner 
# 
# This program is free software; you can redistribute it and/or modify it 
# under the same terms as Perl 5.10.1. 
# For more details, see http://dev.perl.org/licenses/artistic.html
# 
# This program is distributed in the hope that it will be
# useful, but without any warranty; without even the implied
# warranty of merchantability or fitness for a particular purpose.
#

our $VERSION = "1.0.1";

# CHANGELOG:
#   1.0.0   - first release
#   1.0.1   - fixed empty message if all proxies are OK
#

use strict;
use warnings;
use 5.010.001;
use File::Basename qw/basename/;
use IO::Socket::UNIX;
use Getopt::Long;

sub usage {
    my $me = basename $0;
    print <. 
    $me is distributed under GPL and the Artistic License 2.0

SEE ALSO
    Check out online haproxy documentation at 
    
EOU
}

my %check_statuses = (
    UNK     => "unknown",
    INI     => "initializing",
    SOCKERR => "socket error",
    L4OK    => "layer 4 check OK",
    L4CON   => "connection error",
    L4TMOUT => "layer 1-4 timeout",
    L6OK    => "layer 6 check OK",
    L6TOUT  => "layer 6 (SSL) timeout",
    L6RSP   => "layer 6 protocol error",
    L7OK    => "layer 7 check OK",
    L7OKC   => "layer 7 conditionally OK",
    L7TOUT  => "layer 7 (HTTP/SMTP) timeout",
    L7RSP   => "layer 7 protocol error",
    L7STS   => "layer 7 status error",
);

my  _æ_ status_names = (qw/OK WARNING CRITICAL UNKNOWN/);

# Defaults
my $swarn = 80.0;
my $scrit = 90.0;
my $sock  = "/var/run/haproxy.sock";
my $dump;
my $proxy;
my $help;

# Read command line
Getopt::Long::Configure ("bundling");
GetOptions (
    "c|critical=i"    => \$scrit,
    "d|dump"          => \$dump,
    "h|help"          => \$help,
    "p|proxy=s"       => \$proxy,
    "s|sock|socket=s" => \$sock, 
    "w|warning=i"     => \$swarn,
);

# Want help?
if ($help) {
    usage;
    exit 3;
}

# Connect to haproxy socket and get stats
my $haproxy = new IO::Socket::UNIX (
    Peer => $sock,
    Type => SOCK_STREAM,
);
die "Unable to connect to haproxy socket: $ _æ_ " unless $haproxy;
print $haproxy "show stat\n" or die "Print to socket failed: $!";

# Dump stats and exit if requested
if ($dump) {
    while (<$haproxy>) {
        print;
    }
    exit 0;
}

# Get labels from first output line and map them to their position in the line
my $labels = <$haproxy>;
chomp($labels);
$labels =~ s/^# // or die "Data format not supported."; 
my  _æ_ labels = split /,/, $labels;
{ 
    no strict "refs";
    my $idx = 0;
    map { $$_ = $idx++ }  _æ_ labels;
}

# Variables I will use from here on:
our $pxname;
our $svname;
our $status;

my  _æ_ proxies = split ',', $proxy if $proxy;
my $exitcode = 0;
my $msg;
my $checked = 0;
while (<$haproxy>) {
    chomp;
    next if /^[[:space:]]*$/;
    my  _æ_ data = split /,/, $_;
    if ( _æ_ proxies) { next unless grep {$data[$pxname] eq $_}  _æ_ proxies; };

    # Is session limit enforced? 
    our $slim;
    if ($data[$slim]) {
        # Check current session # against limit
        our $scur;
        my $sratio = $data[$scur]/$data[$slim];
        if ($sratio >= $scrit || $sratio >= $swarn) {
            $exitcode = $sratio >= $scrit ? 2 : 
                $exitcode < 2 ? 1 : $exitcode;
            $msg .= sprintf "%s:%s sessions: %.2f%%; ", $data[$pxname], $data[$svname], $sratio;
        }
    }

    # Check of BACKENDS
    if ($data[$svname] eq 'BACKEND') {
        if ($data[$status] ne 'UP') {
            $msg .= sprintf "BACKEND: %s is %s; ", $data[$pxname], $data[$status];
            $exitcode = 2;
        }
    # Check of FRONTENDS
    } elsif ($data[$svname] eq 'FRONTEND') {
        if ($data[$status] ne 'OPEN') {
            $msg .= sprintf "FRONTEND: %s is %s; ", $data[$pxname], $data[$status];
            $exitcode = 2;
        }
    # Check of servers
    } else {
        if ($data[$status] ne 'UP') {
            next if $data[$status] eq 'no check';   # Ignore server if no check is configured to be run
            $exitcode = 2;
            our $check_status;
            $msg .= sprintf "server: %s:%s is %s", $data[$pxname], $data[$svname], $data[$status];
            $msg .= sprintf " (check status: %s)", $check_statuses{$data[$check_status]} if $check_statuses{$data[$check_status]};
            $msg .= "; ";
        }
    }
    ++$checked;
}

unless ($msg) {
    $msg =  _æ_ proxies ? sprintf("checked proxies: %s", join ', ', sort  _æ_ proxies) : "checked $checked proxies.";
}
say "Check haproxy $status_names[$exitcode] - $msg";
exit $exitcode;