From 4dd5aa252b718b4492fa1c8d055b76c56986b5b8 Mon Sep 17 00:00:00 2001 From: Eric Swanson <32274079+swanson8r@users.noreply.github.com> Date: Mon, 25 Sep 2017 13:15:18 -0400 Subject: [PATCH 1/3] custom_query perfdata multi-row variable names Allow custom_query checks with multiple rows of output to populate the second returned column as the perfdata variable name. Pass the result value as the perfdata value. Helpful for count(*) GROUP BY queries. --- check_postgres.pl | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/check_postgres.pl b/check_postgres.pl index b7df607d..25af7226 100755 --- a/check_postgres.pl +++ b/check_postgres.pl @@ -4398,9 +4398,13 @@ sub check_custom_query { my $goodrow = 0; - ## The other column tells it the name to use as the perfdata value + ## If there is a single row and at least 2 columns, + ## the other column tells it the name to use as the perfdata value + ## If there are multiple rows and at least 2 columns, + ## use the second column value as the perfdata name, + ## use the result value as the perfdata value. my $perfname; - + my $grandtotal = @{$db->{slurp}}; for my $r (@{$db->{slurp}}) { my $result = $r->{result}; if (! defined $perfname) { @@ -4413,8 +4417,13 @@ sub check_custom_query { } $goodrow++; if ($perfname) { - $db->{perf} .= sprintf ' %s=%s;%s;%s', - perfname($perfname), $r->{$perfname}, $warning, $critical; + if ($grandtotal > 1) { + $db->{perf} = sprintf ' %s=%s;%s;%s', + perfname($r->{$perfname}), $result, $warning, $critical; + } else { + $db->{perf} .= sprintf ' %s=%s;%s;%s', + perfname($perfname), $r->{$perfname}, $warning, $critical; + } } my $gotmatch = 0; if (! defined $result) { From 40ad64687132efa9b5bbef627d4ff9f692687d55 Mon Sep 17 00:00:00 2001 From: Eric Swanson <32274079+swanson8r@users.noreply.github.com> Date: Mon, 25 Sep 2017 15:15:53 -0400 Subject: [PATCH 2/3] avoid duplicate perfdata for multirow custom_query Addresses concern raised in [Issue #69](https://github.com/bucardo/check_postgres/issues/69) --- check_postgres.pl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/check_postgres.pl b/check_postgres.pl index 25af7226..9b802f76 100755 --- a/check_postgres.pl +++ b/check_postgres.pl @@ -4404,6 +4404,7 @@ sub check_custom_query { ## use the second column value as the perfdata name, ## use the result value as the perfdata value. my $perfname; + my $perfdata; my $grandtotal = @{$db->{slurp}}; for my $r (@{$db->{slurp}}) { my $result = $r->{result}; @@ -4418,8 +4419,11 @@ sub check_custom_query { $goodrow++; if ($perfname) { if ($grandtotal > 1) { - $db->{perf} = sprintf ' %s=%s;%s;%s', + $perfdata = sprintf ' %s=%s;%s;%s', perfname($r->{$perfname}), $result, $warning, $critical; + if ($perfdata ne $db->{perf}){ + $db->{perf} .= $perfdata; + } } else { $db->{perf} .= sprintf ' %s=%s;%s;%s', perfname($perfname), $r->{$perfname}, $warning, $critical; From a71996c56d7b88b9976672f6ef88ad2e10948771 Mon Sep 17 00:00:00 2001 From: Eric Swanson <32274079+swanson8r@users.noreply.github.com> Date: Mon, 25 Sep 2017 16:09:54 -0400 Subject: [PATCH 3/3] initialize perfdata variables before comparison Fixes error ` Use of uninitialized value in string ne` --- check_postgres.pl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/check_postgres.pl b/check_postgres.pl index 9b802f76..035c5142 100755 --- a/check_postgres.pl +++ b/check_postgres.pl @@ -4404,7 +4404,10 @@ sub check_custom_query { ## use the second column value as the perfdata name, ## use the result value as the perfdata value. my $perfname; - my $perfdata; + my $perfdata = ''; + if (! defined $db->{perf}){ + $db->{perf} = ''; + } my $grandtotal = @{$db->{slurp}}; for my $r (@{$db->{slurp}}) { my $result = $r->{result};