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 E3127F1CB for ; Thu, 20 Jul 2023 16:34:02 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id D09AE14BD9 for ; Thu, 20 Jul 2023 16:33:17 +0200 (CEST) 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 ; Thu, 20 Jul 2023 16:33:13 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 40A2F420C4 for ; Thu, 20 Jul 2023 16:33:13 +0200 (CEST) From: Lukas Wagner To: pve-devel@lists.proxmox.com Date: Thu, 20 Jul 2023 16:32:08 +0200 Message-Id: <20230720143236.652292-42-l.wagner@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230720143236.652292-1-l.wagner@proxmox.com> References: <20230720143236.652292-1-l.wagner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.082 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 v4 pve-ha-manager 41/69] manager: send notifications via new notification module 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: Thu, 20 Jul 2023 14:34:02 -0000 ... instead of using sendmail directly. If the new 'notify.target-fencing' parameter from datacenter config is set, we use it as a target for notifications. If it is not set, we send the notification to the default target (mail-to-root). There is also a new 'notify.fencing' paramter which controls if notifications should be sent at all. If it is not set, we default to the old behavior, which is to send. Also add dependency to the `libpve-notify-perl` package to d/control. Signed-off-by: Lukas Wagner --- debian/control | 2 ++ src/PVE/HA/Env.pm | 6 ++--- src/PVE/HA/Env/PVE2.pm | 21 +++++++++------- src/PVE/HA/NodeStatus.pm | 52 ++++++++++++++++++++++++---------------- src/PVE/HA/Sim/Env.pm | 10 ++++++-- 5 files changed, 57 insertions(+), 34 deletions(-) diff --git a/debian/control b/debian/control index 5bff56a..ffa9c1c 100644 --- a/debian/control +++ b/debian/control @@ -8,6 +8,7 @@ Build-Depends: debhelper-compat (= 13), libpve-access-control, libpve-cluster-perl, libpve-common-perl, + libpve-notify-perl, libpve-rs-perl (>= 0.7.3), lintian, pve-cluster, @@ -21,6 +22,7 @@ Architecture: any Depends: libjson-perl, libpve-cluster-perl, libpve-common-perl, + libpve-notify-perl, libpve-rs-perl (>= 0.7.3), pve-cluster (>= 3.0-17), pve-container (>= 5.0.1), diff --git a/src/PVE/HA/Env.pm b/src/PVE/HA/Env.pm index 16603ec..b7060a4 100644 --- a/src/PVE/HA/Env.pm +++ b/src/PVE/HA/Env.pm @@ -144,10 +144,10 @@ sub log { return $self->{plug}->log($level, @args); } -sub sendmail { - my ($self, $subject, $text) = @_; +sub send_notification { + my ($self, $subject, $text, $properties) = @_; - return $self->{plug}->sendmail($subject, $text); + return $self->{plug}->send_notification($subject, $text, $properties); } # acquire a cluster wide manager lock diff --git a/src/PVE/HA/Env/PVE2.pm b/src/PVE/HA/Env/PVE2.pm index f6ebfeb..ea9e6e4 100644 --- a/src/PVE/HA/Env/PVE2.pm +++ b/src/PVE/HA/Env/PVE2.pm @@ -13,6 +13,7 @@ use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file use PVE::DataCenterConfig; use PVE::INotify; use PVE::RPCEnvironment; +use PVE::Notify; use PVE::HA::Tools ':exit_codes'; use PVE::HA::Env; @@ -219,16 +220,20 @@ sub log { syslog($level, $msg); } -sub sendmail { - my ($self, $subject, $text) = @_; +sub send_notification { + my ($self, $subject, $text, $properties) = @_; - # Leave it to postfix to append the correct hostname - my $mailfrom = 'root'; - # /root/.forward makes pvemailforward redirect the - # mail to the address configured in the datacenter - my $mailto = 'root'; + eval { + my $dc_config = PVE::Cluster::cfs_read_file('datacenter.cfg'); + my $target = $dc_config->{notify}->{'target-fencing'} // PVE::Notify::default_target(); + my $notify = $dc_config->{notify}->{fencing} // 'always'; + + if ($notify eq 'always') { + PVE::Notify::error($target, $subject, $text, $properties); + } + }; - PVE::Tools::sendmail($mailto, $subject, $text, undef, $mailfrom); + $self->log("warning", "could not notify: $@") if $@; } my $last_lock_status_hash = {}; diff --git a/src/PVE/HA/NodeStatus.pm b/src/PVE/HA/NodeStatus.pm index ee5be8e..b264a36 100644 --- a/src/PVE/HA/NodeStatus.pm +++ b/src/PVE/HA/NodeStatus.pm @@ -188,35 +188,45 @@ sub update { } } -# assembles a commont text for fence emails -my $send_fence_state_email = sub { - my ($self, $subject_prefix, $subject, $node) = @_; - - my $haenv = $self->{haenv}; - - my $mail_text = <{haenv}; my $status = $haenv->read_manager_status(); - my $data = { manager_status => $status, node_status => $self->{status} }; - - $mail_text .= to_json($data, { pretty => 1, canonical => 1}); - $haenv->sendmail($mail_subject, $mail_text); + my $notification_properties = { + "status-data" => { + manager_status => $status, + node_status => $self->{status} + }, + "node" => $node, + "subject-prefix" => $subject_prefix, + "subject" => $subject, + }; + + $haenv->send_notification( + $subject_template, + $body_template, + $notification_properties + ); }; diff --git a/src/PVE/HA/Sim/Env.pm b/src/PVE/HA/Sim/Env.pm index c6ea73c..d3aea8d 100644 --- a/src/PVE/HA/Sim/Env.pm +++ b/src/PVE/HA/Sim/Env.pm @@ -288,8 +288,14 @@ sub log { printf("%-5s %5d %12s: $msg\n", $level, $time, "$self->{nodename}/$self->{log_id}"); } -sub sendmail { - my ($self, $subject, $text) = @_; +sub send_notification { + my ($self, $subject, $text, $properties) = @_; + + # The template for the subject is "{{subject-prefix}}: {{subject}}" + # We have to perform poor-man's template rendering to pass the test cases. + + $subject = $subject =~ s/\{\{subject-prefix}}/$properties->{"subject-prefix"}/r; + $subject = $subject =~ s/\{\{subject}}/$properties->{"subject"}/r; # only log subject, do not spam the logs $self->log('email', $subject); -- 2.39.2