From 8850f225df5c8ecb3e11721d89bdd314b09c2731 Mon Sep 17 00:00:00 2001 From: Herman van Rink Date: Thu, 17 Jun 2021 22:08:32 +0200 Subject: [PATCH 1/6] Add performance data --- check_haproxy | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/check_haproxy b/check_haproxy index 9fa2e4b..69d089d 100755 --- a/check_haproxy +++ b/check_haproxy @@ -142,7 +142,7 @@ our (%opt, %data, %checks); ); # Somewhere to store messages -our (@critical, @warning); +our (@critical, @warning, @perfdata); sub main { my (@overrides); @@ -218,7 +218,8 @@ sub _report { croak('No message given') unless $message; _debug('report', "with status ${status} and message '${message}'"); - printf "HAPROXY %s: %s (%s %s)\n", (EXIT_STATUS)[$status], $message, $_program, $_version; + my $perfstring = join(' ', @perfdata); + printf "HAPROXY %s: %s | %s \n", (EXIT_STATUS)[$status], $message, $perfstring; exit $status; } @@ -473,6 +474,12 @@ sub build_checks { sub check_frontends { _debug('check_frontends', 'is starting check on frontends'); + + # First gather all perfdata, the next foreach finishes on the first critical/warning. + foreach my $service (sort keys %data) { + push(@perfdata, $service . '=' . $data{$service}{'sessions'}{'current'}); + } + foreach my $service (sort keys %data) { next unless $data{$service}{'type'} eq TYPE_FRONTEND; _debug('check_frontends', _('checking FRONTEND %s', $service)); @@ -515,6 +522,12 @@ sub check_frontends { sub check_backends { _debug('check_backends', 'is starting check on backends'); + + # First gather all perfdata, the next foreach finishes on the first critical/warning. + foreach my $service (sort keys %data) { + push(@perfdata, $service . '=' . $data{$service}{'sessions'}{'current'}); + } + # Run checks for each of the backends foreach my $service (sort keys %data) { next unless $data{$service}{'type'} eq TYPE_BACKEND; From 58895ce8bf1b0b456650dc6a7a1d4371af2b0cce Mon Sep 17 00:00:00 2001 From: Herman van Rink Date: Sun, 20 Jun 2021 21:48:06 +0200 Subject: [PATCH 2/6] Report more then just the first issue --- check_haproxy | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/check_haproxy b/check_haproxy index 9fa2e4b..fab5c82 100755 --- a/check_haproxy +++ b/check_haproxy @@ -201,11 +201,20 @@ sub main { } } - do_success( - _('HAProxy is functioning within established parameters. '. - '(%d frontends, %d backends, %d servers, %d services)', - $frontends, $backends, $servers, $services) - ); + if (@critical) { + $fullmessage = join(' ', @critical) . join(' ', @warnings); + _report(EXIT_CRITICAL, $fullmessage); + } + elsif (@warnings) { + $fullmessage = join(' ', @warnings); + _report(EXIT_WARNING, $fullmessage); + } + else { + _report(EXIT_OK, _('HAProxy is functioning within established parameters. '. + '(%d frontends, %d backends, %d servers, %d services)', + $frontends, $backends, $servers, $services) + ); + } } sub _debug { @@ -222,21 +231,15 @@ sub _report { exit $status; } -sub do_success { - _report(EXIT_OK, shift); -} - sub do_warning { my ($message) = shift; push(@warning, $message); - _report(EXIT_WARNING, $message); } sub do_critical { my ($message, $_croak) = (shift, shift); croak($message) if $opt{'debug'} and $_croak; push(@critical, $message); - _report(EXIT_CRITICAL, $message); } sub do_unknown { From 1990d623255b1f685ce21a85a13868fc95bd5727 Mon Sep 17 00:00:00 2001 From: Herman van Rink Date: Sun, 20 Jun 2021 22:15:40 +0200 Subject: [PATCH 3/6] Strict checking to catch a typo --- check_haproxy | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/check_haproxy b/check_haproxy index fab5c82..fbb6e8c 100755 --- a/check_haproxy +++ b/check_haproxy @@ -19,7 +19,7 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Configure the Perl Environment -use warnings; +use strict; use Carp; use Switch; @@ -202,11 +202,11 @@ sub main { } if (@critical) { - $fullmessage = join(' ', @critical) . join(' ', @warnings); + my $fullmessage = join(' ', @critical) . join(' ', @warning); _report(EXIT_CRITICAL, $fullmessage); } - elsif (@warnings) { - $fullmessage = join(' ', @warnings); + elsif (@warning) { + my $fullmessage = join(' ', @warning); _report(EXIT_WARNING, $fullmessage); } else { From 522aac255ca0dde8af84b6ab6b2f038761b63be7 Mon Sep 17 00:00:00 2001 From: Herman van Rink Date: Thu, 24 Jun 2021 09:18:57 +0200 Subject: [PATCH 4/6] better layout --- check_haproxy | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/check_haproxy b/check_haproxy index 4eb3938..67ab194 100755 --- a/check_haproxy +++ b/check_haproxy @@ -202,11 +202,12 @@ sub main { } if (@critical) { - my $fullmessage = join(' ', @critical) . join(' ', @warning); + my $fullmessage = join(', ', @critical); + $fullmessage .= ' Warnings: '. join(', ', @warning) if (@warning); _report(EXIT_CRITICAL, $fullmessage); } elsif (@warning) { - my $fullmessage = join(' ', @warning); + my $fullmessage = join(', ', @warning); _report(EXIT_WARNING, $fullmessage); } else { From 9ce595eb57511f3ef073e9dfcc0416c5d69bccf9 Mon Sep 17 00:00:00 2001 From: Herman van Rink Date: Thu, 24 Jun 2021 09:19:53 +0200 Subject: [PATCH 5/6] skip warning check if backend/frontend is already critical --- check_haproxy | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/check_haproxy b/check_haproxy index 67ab194..94e2783 100755 --- a/check_haproxy +++ b/check_haproxy @@ -242,6 +242,7 @@ sub do_critical { my ($message, $_croak) = (shift, shift); croak($message) if $opt{'debug'} and $_croak; push(@critical, $message); + return 1; } sub do_unknown { @@ -504,7 +505,7 @@ sub check_frontends { ($checks{$service}{'limit_warn'}*100), ($checks{$service}{'limit_crit'}*100))); - do_critical(_('Sessions for FRONTEND %s has passed %.2f%% of Limit (%d active, %d limit)', + my $ret = do_critical(_('Sessions for FRONTEND %s has passed %.2f%% of Limit (%d active, %d limit)', $service, ($checks{$service}{'limit_crit'}*100), $data{$service}{'sessions'}{'current'}, $data{$service}{'sessions'}{'limit'})) @@ -518,7 +519,7 @@ sub check_frontends { $data{$service}{'sessions'}{'limit'})) unless check_pass('t', $checks{$service}{'limit_warn'}, $data{$service}{'sessions'}{'current'}, - $data{$service}{'sessions'}{'limit'}); + $data{$service}{'sessions'}{'limit'}) or $ret; } @@ -559,12 +560,13 @@ sub check_backends { } } - do_critical(_($msg, $service, $checks{$service}{'be_crit'}, $up, $down, $disabled)) + my $ret = do_critical(_($msg, $service, $checks{$service}{'be_crit'}, $up, $down, $disabled)) unless check_pass($checks{$service}{'state'}, $checks{$service}{'be_crit'}, ($checks{$service}{'state'} eq 'u' ? $up : ($down+$disabled)), $total); + do_warning(_($msg, $service, $checks{$service}{'be_warn'}, $up, $down, $disabled)) unless check_pass($checks{$service}{'state'}, $checks{$service}{'be_warn'}, - ($checks{$service}{'state'} eq 'u' ? $up : ($down+$disabled)), $total); + ($checks{$service}{'state'} eq 'u' ? $up : ($down+$disabled)), $total) or $ret; next unless $data{$service}{'sessions'}{'limit'} > 0; _debug('check_frontends', 'running Sessions check'); @@ -574,7 +576,7 @@ sub check_backends { ($checks{$service}{'limit_warn'}*100), ($checks{$service}{'limit_crit'}*100))); - do_critical(_('Sessions for BACKEND %s has passed %d%% of Limit (%d active, %d limit)', + my $ret2 = do_critical(_('Sessions for BACKEND %s has passed %d%% of Limit (%d active, %d limit)', $service, ($checks{$service}{'limit_crit'}*100), $data{$service}{'sessions'}{'current'}, $data{$service}{'sessions'}{'limit'})) @@ -588,7 +590,7 @@ sub check_backends { $data{$service}{'sessions'}{'limit'})) unless check_pass('t', $checks{$service}{'limit_warn'}, $data{$service}{'sessions'}{'current'}, - $data{$service}{'sessions'}{'limit'}); + $data{$service}{'sessions'}{'limit'}) or $ret2; } } From c83a27f227f8ff48e2615359b2fba68d55727d63 Mon Sep 17 00:00:00 2001 From: Herman van Rink Date: Sun, 4 Jul 2021 11:16:05 +0200 Subject: [PATCH 6/6] skip warning check if backend/frontend is already critical for check_servers --- check_haproxy | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/check_haproxy b/check_haproxy index 94e2783..08ad794 100755 --- a/check_haproxy +++ b/check_haproxy @@ -613,7 +613,7 @@ sub check_servers { ($checks{$service}{'limit_warn'}*100), ($checks{$service}{'limit_crit'}*100))); - do_critical(_('Sessions for SERVER %s on BACKEND %s has passed %d%% of Limit (%d active, %d limit)', + my $ret = do_critical(_('Sessions for SERVER %s on BACKEND %s has passed %d%% of Limit (%d active, %d limit)', $server, $service, ($checks{$service}{'limit_crit'}*100), $data{$service}{'servers'}{$server}{'sessions'}{'current'}, $data{$service}{'servers'}{$server}{'sessions'}{'limit'})) @@ -627,7 +627,7 @@ sub check_servers { $data{$service}{'servers'}{$server}{'sessions'}{'limit'})) unless check_pass('t', $checks{$service}{'limit_warn'}, $data{$service}{'servers'}{$server}{'sessions'}{'current'}, - $data{$service}{'servers'}{$server}{'sessions'}{'limit'}); + $data{$service}{'servers'}{$server}{'sessions'}{'limit'}) or $ret; } @@ -639,7 +639,7 @@ sub check_servers { ($checks{$service}{'limit_warn'}*100), ($checks{$service}{'limit_crit'}*100))); - do_critical(_('Queue for SERVER %s on BACKEND has passed %d%% of Limit (%d active, %d limit)', + my $ret2 = do_critical(_('Queue for SERVER %s on BACKEND has passed %d%% of Limit (%d active, %d limit)', $server, $service, ($checks{$service}{'limit_crit'}*100), $data{$service}{'servers'}{$server}{'queued'}{'current'}, $data{$service}{'servers'}{$server}{'queued'}{'limit'})) @@ -653,7 +653,7 @@ sub check_servers { $data{$service}{'servers'}{$server}{'queued'}{'limit'})) unless check_pass('t', $checks{$service}{'limit_warn'}, $data{$service}{'servers'}{$server}{'queued'}{'current'}, - $data{$service}{'servers'}{$server}{'queued'}{'limit'}); + $data{$service}{'servers'}{$server}{'queued'}{'limit'}) or $ret2; } } }