From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id E338569134 for ; Mon, 1 Mar 2021 15:13:20 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id BB692211E1 for ; Mon, 1 Mar 2021 15:12:50 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id D253C2113C for ; Mon, 1 Mar 2021 15:12:47 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 85DA34580D for ; Mon, 1 Mar 2021 15:12:47 +0100 (CET) From: Stoiko Ivanov To: pmg-devel@lists.proxmox.com Date: Mon, 1 Mar 2021 15:12:19 +0100 Message-Id: <20210301141225.18394-3-s.ivanov@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210301141225.18394-1-s.ivanov@proxmox.com> References: <20210301141225.18394-1-s.ivanov@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.307 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_SBL 0.644 Contains an URL's NS IP listed in the Spamhaus SBL blocklist [backup.pm] URIBL_SBL_A 0.1 Contains URL's A record listed in the Spamhaus SBL blocklist [backup.pm] Subject: [pmg-devel] [PATCH pmg-api v2 2/3] backup: fix #3146 add email notifications X-BeenThere: pmg-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Mail Gateway development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 01 Mar 2021 14:13:20 -0000 this patch addresses the missing email notification for scheduled backup related tasks, which we have in all our other products, for our mail product. the parameter names are inspired by PBS' datastore config. the default is 'never' in order to stay consistent with the current code. it uses the templateing system for the notification, because this results in less code and a bit of added flexibility for the users. the recipient address is currently hardcoded to the admin address in pmg.conf as we also send the (admin) pmgreport there, and I did not want to overengineer this (even more). I shortly considered adding a $SIG{'__DIE__'} handler to the run_backup API call but dropped the idea due to the warning in perlvar(1). Signed-off-by: Stoiko Ivanov --- src/Makefile | 1 + src/PMG/API2/PBS/Job.pm | 34 ++++++++++++++++++++------- src/PMG/Backup.pm | 35 ++++++++++++++++++++++++++++ src/PMG/PBSConfig.pm | 7 ++++++ src/templates/backup-notification.tt | 19 +++++++++++++++ 5 files changed, 88 insertions(+), 8 deletions(-) create mode 100644 src/templates/backup-notification.tt diff --git a/src/Makefile b/src/Makefile index 67e6878..8891a3c 100644 --- a/src/Makefile +++ b/src/Makefile @@ -43,6 +43,7 @@ TEMPLATES = \ clamd.conf.in \ postgresql.conf.in \ pg_hba.conf.in \ + backup-notification.tt \ TEMPLATES_FILES = $(addprefix templates/, ${TEMPLATES}) diff --git a/src/PMG/API2/PBS/Job.pm b/src/PMG/API2/PBS/Job.pm index c095d25..fb2d914 100644 --- a/src/PMG/API2/PBS/Job.pm +++ b/src/PMG/API2/PBS/Job.pm @@ -273,6 +273,13 @@ __PACKAGE__->register_method ({ optional => 1, default => 1, }, + notify => { + description => "Specify when to notify via e-mail", + type => 'string', + enum => [ 'always', 'error', 'never' ], + optional => 1, + default => 'never', + }, }, }, returns => { type => "string" }, @@ -292,6 +299,7 @@ __PACKAGE__->register_method ({ die "PBS remote '$remote' is disabled\n" if $remote_config->{disable}; $param->{statistic} //= $remote_config->{'include-statistics'} // 1; + my $notify = $param->{notify} // $remote_config->{notify} // 'never'; my $pbs = PVE::PBSClient->new($remote_config, $remote, $conf->{secret_dir}); @@ -301,7 +309,10 @@ __PACKAGE__->register_method ({ my $worker = sub { my $upid = shift; - print "starting update of current backup state\n"; + my $full_log = ""; + my $log = sub { print "$_[0]\n"; $full_log .= "$_[0]\n"; }; + + $log->("starting update of current backup state\n"); eval { -d $backup_dir || mkdir $backup_dir; @@ -312,25 +323,32 @@ __PACKAGE__->register_method ({ rmtree $backup_dir; }; if (my $err = $@) { + $log->($err); + PMG::Backup::send_backup_notification($notify, $remote, $full_log, $err); rmtree $backup_dir; die "backup failed: $err\n"; - } - print "backup finished\n"; + $log->("backup finished\n"); my $group = "host/$node"; if (defined(my $prune_opts = $conf->prune_options($remote))) { - print "starting prune of $group\n"; - my $res = $pbs->prune_group(undef, $prune_opts, $group); - + $log->("starting prune of $group\n"); + my $res = eval { $pbs->prune_group(undef, $prune_opts, $group) }; + if (my $err = $@) { + $log->($err); + PMG::Backup::send_backup_notification($notify, $remote, $full_log, $err); + die "pruning failed: $err\n"; + } foreach my $pruned (@$res){ my $time = strftime("%FT%TZ", gmtime($pruned->{'backup-time'})); my $snap = $pruned->{'backup-type'} . '/' . $pruned->{'backup-id'} . '/' . $time; - print "pruned snapshot: $snap\n"; + $log->("pruned snapshot: $snap\n"); } - print "prune finished\n"; + $log->("prune finished\n"); } + PMG::Backup::send_backup_notification($notify, $remote, $full_log, undef); + return; }; diff --git a/src/PMG/Backup.pm b/src/PMG/Backup.pm index 303bbb4..2f96412 100644 --- a/src/PMG/Backup.pm +++ b/src/PMG/Backup.pm @@ -5,6 +5,7 @@ use warnings; use Data::Dumper; use File::Basename; use File::Path; +use POSIX qw(strftime); use PVE::JSONSchema qw(get_standard_option); use PVE::Tools; @@ -376,4 +377,38 @@ sub pmg_restore { die $err if $err; } +sub send_backup_notification { + my ($notify_on, $target, $log, $err) = @_; + + return if !$notify_on; + return if $notify_on eq 'never'; + return if $notify_on eq 'error' && !$err; + + my $cfg = PMG::Config->new(); + my $email = $cfg->get ('admin', 'email'); + if (!$email) { + warn "not sending notifcation: no admin email configured\n"; + return; + } + + my $nodename = PVE::INotify::nodename(); + my $fqdn = PVE::Tools::get_fqdn($nodename); + + + my $vars = { + hostname => $nodename, + fqdn => $fqdn, + date => strftime("%F", localtime()), + target => $target, + log => $log, + err => $err, + }; + + my $tt = PMG::Config::get_template_toolkit(); + + my $mailfrom = "Proxmox Mail Gateway "; + PMG::Utils::finalize_report($tt, 'backup-notification.tt', $vars, $mailfrom, $email); + +} + 1; diff --git a/src/PMG/PBSConfig.pm b/src/PMG/PBSConfig.pm index 29cff22..965d162 100644 --- a/src/PMG/PBSConfig.pm +++ b/src/PMG/PBSConfig.pm @@ -90,6 +90,12 @@ sub properties { description => "Username or API token ID on the Proxmox Backup Server" }), fingerprint => get_standard_option('fingerprint-sha256'), + notify => { + description => "Specify when to notify via e-mail", + type => 'string', + enum => [ 'always', 'error', 'never' ], + optional => 1, + }, 'include-statistics' => { description => "Include statistics in scheduled backups", type => 'boolean', @@ -107,6 +113,7 @@ sub options { username => { optional => 1 }, password => { optional => 1 }, fingerprint => { optional => 1 }, + notify => { optional => 1 }, 'include-statistics' => { optional => 1 }, 'keep-last' => { optional => 1 }, 'keep-hourly' => { optional => 1 }, diff --git a/src/templates/backup-notification.tt b/src/templates/backup-notification.tt new file mode 100644 index 0000000..3f956b0 --- /dev/null +++ b/src/templates/backup-notification.tt @@ -0,0 +1,19 @@ +[%- IF err -%] +[%- SET titleprefix = "Backup failed" -%] +[%- ELSE %] +[%- SET titleprefix = "Backup successful" -%] +[%- END -%] +[%- IF target -%] +[%- SET titlesuffix = "to ${target} - ($fqdn)" -%] +[%- ELSE %] +[%- SET titlesuffix = "($fqdn)" -%] +[%- END -%] + + + [% titleprefix %] [% titlesuffix %] + + + Detailed backup logs:

+
[% log %]
+ + -- 2.20.1