[pmg-devel] applied: [PATCH pmg-api v2] adapt journalctl invocation to buster
Fabian Grünbichler
f.gruenbichler at proxmox.com
Fri Aug 9 12:21:29 CEST 2019
with a small follow-up to handle some potential errors more gracefully:
diff --git a/src/PMG/Utils.pm b/src/PMG/Utils.pm
index 3a97e79..2dc22ad 100644
--- a/src/PMG/Utils.pm
+++ b/src/PMG/Utils.pm
@@ -1309,8 +1309,11 @@ sub scan_journal_for_rbl_rejects {
my $parser = sub {
my $log = decode_json(shift);
- $rbl_scan_last_cursor = $log->{__CURSOR};
+ $rbl_scan_last_cursor = $log->{__CURSOR} if defined($log->{__CURSOR});
+
my $message = $log->{MESSAGE};
+ return if !defined($message);
+
if ($message =~ m/^NOQUEUE:\sreject:.*550 5.7.1 Service unavailable/) {
$rbl_count++;
} elsif ($message =~ m/^PREGREET\s\d+\safter\s/) {
On August 9, 2019 9:07 am, Stoiko Ivanov wrote:
> With Debian Buster the behavior of `journalctl` has changed when it finds
> no entries for a given selection:
> * The exit code was 0 in stretch, but is 1 in buster
> * The output changed slightly - a header got added
>
> Since PMG::Utils::scan_journal_for_rbl_rejects uses journalctl for reading the
> IPs blocked by postscreen it needs to adapt for the new behavior (otherwise
> run_command dies because of the exit code 1)
>
> The patch addresses the problem by using the json-output of `journalctl`, which
> still exits with 0 if no entries are present . Additionally the json-output
> adds the current cursor to the output by default, removing the need to
> explicitly scanning for it.(the exit code of 1 was due to '--show-cursor'
> without a single result line).
>
> Signed-off-by: Stoiko Ivanov <s.ivanov at proxmox.com>
> ---
> changes v1->v2:
> * Adapted Dominik and Fabian's feedback by using journalctl's json output
> * without '--show-cursor' the exit status is '0' even if no entries are
> present
> src/PMG/Utils.pm | 18 ++++++++----------
> 1 file changed, 8 insertions(+), 10 deletions(-)
>
> diff --git a/src/PMG/Utils.pm b/src/PMG/Utils.pm
> index aa6aac7..65684b6 100644
> --- a/src/PMG/Utils.pm
> +++ b/src/PMG/Utils.pm
> @@ -27,6 +27,7 @@ use utf8;
> no utf8;
>
> use HTML::Entities;
> +use JSON;
>
> use PVE::ProcFSTools;
> use PVE::Network;
> @@ -1300,23 +1301,20 @@ sub scan_journal_for_rbl_rejects {
> my $pregreet_count = 0;
>
> my $parser = sub {
> - my $line = shift;
> -
> - if ($line =~ m/^--\scursor:\s(\S+)$/) {
> - $rbl_scan_last_cursor = $1;
> - return;
> - }
> + my $log = decode_json(shift);
>
> - if ($line =~ m/\s$identifier\[\d+\]:\sNOQUEUE:\sreject:.*550 5.7.1 Service unavailable;/) {
> + $rbl_scan_last_cursor = $log->{__CURSOR};
> + my $message = $log->{MESSAGE};
> + if ($message =~ m/^NOQUEUE:\sreject:.*550 5.7.1 Service unavailable/) {
> $rbl_count++;
> - } elsif ($line =~ m/\s$identifier\[\d+\]:\sPREGREET\s\d+\safter\s/) {
> + } elsif ($message =~ m/^PREGREET\s\d+\safter\s/) {
> $pregreet_count++;
> }
> };
>
> # limit to last 5000 lines to avoid long delays
> - my $cmd = ['journalctl', '--show-cursor', '-o', 'short-unix', '--no-pager',
> - '--identifier', $identifier, '-n', 5000];
> + my $cmd = ['journalctl', '-o', 'json', '--output-fields', '__CURSOR,MESSAGE',
> + '--no-pager', '--identifier', $identifier, '-n', 5000];
>
> if (defined($rbl_scan_last_cursor)) {
> push @$cmd, "--after-cursor=${rbl_scan_last_cursor}";
> --
> 2.20.1
>
>
> _______________________________________________
> pmg-devel mailing list
> pmg-devel at pve.proxmox.com
> https://pve.proxmox.com/cgi-bin/mailman/listinfo/pmg-devel
>
>
More information about the pmg-devel
mailing list