no utf8;
use HTML::Entities;
+use JSON;
use PVE::ProcFSTools;
use PVE::Network;
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', {
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}";
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;