]> git.proxmox.com Git - pmg-api.git/commitdiff
adapt journalctl invocation to buster
authorStoiko Ivanov <s.ivanov@proxmox.com>
Fri, 9 Aug 2019 07:07:30 +0000 (09:07 +0200)
committerFabian Grünbichler <f.gruenbichler@proxmox.com>
Fri, 9 Aug 2019 10:11:21 +0000 (12:11 +0200)
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@proxmox.com>
src/PMG/Utils.pm

index f9e07b669f28abf18c440ceb3e0a402816beb506..3a97e7932f8f6b2fec8400296f5b8d734aa63a0c 100644 (file)
@@ -27,6 +27,7 @@ use utf8;
 no utf8;
 
 use HTML::Entities;
+use JSON;
 
 use PVE::ProcFSTools;
 use PVE::Network;
@@ -1306,23 +1307,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}";