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 EC03E9B8FC for ; Tue, 21 Nov 2023 11:22:11 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id D48E364E9 for ; Tue, 21 Nov 2023 11:22:11 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (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 for ; Tue, 21 Nov 2023 11:22:11 +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 CD10640E79 for ; Tue, 21 Nov 2023 11:22:10 +0100 (CET) From: Lukas Wagner To: pve-devel@lists.proxmox.com Date: Tue, 21 Nov 2023 11:22:06 +0100 Message-Id: <20231121102208.82219-3-l.wagner@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231121102208.82219-1-l.wagner@proxmox.com> References: <20231121102208.82219-1-l.wagner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.009 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - Subject: [pve-devel] [PATCH v2 pve-manager 2/4] vzdump: support 'notification-mode' parameter X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 21 Nov 2023 10:22:12 -0000 This parameter lets us choose between the 'legacy' notification system (sendmail to some email addresses) and the 'new' notification system (pub-sub based system with targets and matchers). 'auto' (default) will use the 'legacy' system if a mail address is provided and the 'new' system if not. This is allows users to opt-in/opt-out from the new notification system, which might be a bit chatty by default. Signed-off-by: Lukas Wagner --- PVE/VZDump.pm | 95 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 58 insertions(+), 37 deletions(-) diff --git a/PVE/VZDump.pm b/PVE/VZDump.pm index b0574d41..4185ed62 100644 --- a/PVE/VZDump.pm +++ b/PVE/VZDump.pm @@ -452,12 +452,8 @@ sub send_notification { my $opts = $self->{opts}; my $mailto = $opts->{mailto}; my $cmdline = $self->{cmdline}; - # Old-style notification policy. This parameter will influce - # if an ad-hoc notification target/matcher will be created. - my $policy = $opts->{"notification-policy"} // - $opts->{mailnotification} // - 'always'; - + my $policy = $opts->{mailnotification} // 'always'; + my $mode = $opts->{"notification-mode"} // 'auto'; sanitize_task_list($tasklist); my $error_count = count_failed_tasks($tasklist); @@ -499,44 +495,69 @@ sub send_notification { }; my $fields = { + # TODO: There is no straight-forward way yet to get the + # backup job id here... (I think pvescheduler would need + # to pass that to the vzdump call?) type => "vzdump", hostname => $hostname, }; - my $notification_config = PVE::Notify::read_config(); - - my $legacy_sendmail = $policy eq "always" || ($policy eq "failure" && $failed); - - if ($mailto && scalar(@$mailto) && $legacy_sendmail) { - # <, >, @ are not allowed in endpoint names, but that is only - # verified once the config is serialized. That means that - # we can rely on that fact that no other endpoint with this name exists. - my $endpoint_name = ""; - $notification_config->add_sendmail_endpoint( - $endpoint_name, - $mailto, - undef, - undef, - "vzdump backup tool"); - - my $endpoints = [$endpoint_name]; + my $severity = $failed ? "error" : "info"; + my $email_configured = $mailto && scalar(@$mailto); + + if (($mode eq 'auto' && $email_configured) || $mode eq 'legacy-sendmail') { + if ($email_configured && ($policy eq "always" || ($policy eq "failure" && $failed))) { + # Start out with an empty config. Might still contain + # built-ins, so we need to disable/remove them. + my $notification_config = Proxmox::RS::Notify->parse_config('', ''); + + # Remove built-in matchers, since we only want to send an + # email to the specified recipients and nobody else. + for my $matcher (@{$notification_config->get_matchers()}) { + $notification_config->delete_matcher($matcher->{name}); + } - $notification_config->add_matcher( - "", - $endpoints, + # <, >, @ are not allowed in endpoint names, but that is only + # verified once the config is serialized. That means that + # we can rely on that fact that no other endpoint with this name exists. + my $endpoint_name = "<" . join(",", @$mailto) . ">"; + $notification_config->add_sendmail_endpoint( + $endpoint_name, + $mailto, + undef, + undef, + "vzdump backup tool" + ); + + my $endpoints = [$endpoint_name]; + + # Add a matcher that matches all notifications, set our + # newly created target as a target. + $notification_config->add_matcher( + "", + $endpoints, + ); + + PVE::Notify::notify( + $severity, + $subject_template, + $body_template, + $notification_props, + $fields, + $notification_config + ); + } + } else { + # We use the 'new' system, or we are set to 'auto' and + # no email addresses were configured. + PVE::Notify::notify( + $severity, + $subject_template, + $body_template, + $notification_props, + $fields, ); } - - my $severity = $failed ? "error" : "info"; - - PVE::Notify::notify( - $severity, - $subject_template, - $body_template, - $notification_props, - $fields, - $notification_config - ); }; sub new { -- 2.39.2