]> git.proxmox.com Git - pmg-api.git/blobdiff - src/PMG/Utils.pm
adapt journalctl invocation to buster
[pmg-api.git] / src / PMG / Utils.pm
index aa6aac770ad77d29b9bba0a3fe54f62cab1bbed9..3a97e7932f8f6b2fec8400296f5b8d734aa63a0c 100644 (file)
@@ -27,6 +27,7 @@ use utf8;
 no utf8;
 
 use HTML::Entities;
+use JSON;
 
 use PVE::ProcFSTools;
 use PVE::Network;
@@ -37,6 +38,12 @@ use PMG::AtomicFile;
 use PMG::MailQueue;
 use PMG::SMTPPrinter;
 
+use base 'Exporter';
+
+our @EXPORT_OK = qw(
+postgres_admin_cmd
+);
+
 my $valid_pmg_realms = ['pam', 'pmg', 'quarantine'];
 
 PVE::JSONSchema::register_standard_option('realm', {
@@ -1300,23 +1307,20 @@ sub scan_journal_for_rbl_rejects {
     my $pregreet_count = 0;
 
     my $parser = sub {
-       my $line = shift;
+       my $log = decode_json(shift);
 
-       if ($line =~ m/^--\scursor:\s(\S+)$/) {
-           $rbl_scan_last_cursor = $1;
-           return;
-       }
-
-       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}";
@@ -1378,4 +1382,48 @@ sub cond_add_default_locale {
     system("dpkg-reconfigure locales -f noninteractive");
 }
 
+sub postgres_admin_cmd {
+    my ($cmd, $options, @params) = @_;
+
+    $cmd = ref($cmd) ? $cmd : [ $cmd ];
+
+    my $save_uid = POSIX::getuid();
+    my $pg_uid = getpwnam('postgres') || die "getpwnam postgres failed\n";
+
+    PVE::Tools::setresuid(-1, $pg_uid, -1) ||
+       die "setresuid postgres ($pg_uid) failed - $!\n";
+
+    PVE::Tools::run_command([@$cmd, '-U', 'postgres', @params], %$options);
+
+    PVE::Tools::setresuid(-1, $save_uid, -1) ||
+       die "setresuid back failed - $!\n";
+}
+
+sub get_pg_server_version {
+    my $major_ver;
+    my $parser = sub {
+       my $line = shift;
+       # example output:
+       # 9.6.13
+       # 11.4 (Debian 11.4-1)
+       # see https://www.postgresql.org/support/versioning/
+       my ($first_comp) = ($line =~ m/^\s*([0-9]+)/);
+       if ($first_comp < 10) {
+           ($major_ver) = ($line =~ m/^([0-9]+\.[0-9]+)\.[0-9]+/);
+       } else {
+           $major_ver = $first_comp;
+       }
+
+    };
+    eval {
+       postgres_admin_cmd('psql', { outfunc => $parser }, '--quiet',
+       '--tuples-only', '--no-align', '--command', 'show server_version;');
+    };
+
+    die "Unable to determine currently running Postgresql server version\n"
+       if ($@ || !defined($major_ver));
+
+    return $major_ver;
+}
+
 1;