[pve-devel] [PATCH v2 pve-ha-manager 42/42] manager: send notifications via new notification module
Lukas Wagner
l.wagner at proxmox.com
Wed May 24 15:56:49 CEST 2023
... instead of using sendmail directly
Signed-off-by: Lukas Wagner <l.wagner at proxmox.com>
---
src/PVE/HA/Env.pm | 6 ++---
src/PVE/HA/Env/PVE2.pm | 27 ++++++++++++++++++---
src/PVE/HA/NodeStatus.pm | 52 ++++++++++++++++++++++++----------------
src/PVE/HA/Sim/Env.pm | 10 ++++++--
4 files changed, 66 insertions(+), 29 deletions(-)
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..d818812 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,8 +220,8 @@ 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';
@@ -228,7 +229,27 @@ sub sendmail {
# mail to the address configured in the datacenter
my $mailto = 'root';
- PVE::Tools::sendmail($mailto, $subject, $text, undef, $mailfrom);
+ # Add ephemeral sendmail endpoint/channel for backwards compatibility
+ # TODO: Make notification channel configurable, then the
+ # temporary endpoint/channel should not be necessary any more.
+ my $notification_config = PVE::Notify::read_config();
+ $notification_config->add_sendmail_endpoint(
+ 'anonymous-ha-manager-sendmail',
+ [$mailto],
+ $mailfrom,
+ undef
+ );
+ my $channel = 'mail';
+
+ $notification_config->add_channel($channel, ['anonymous-ha-manager-sendmail']);
+
+ PVE::Notify::warning(
+ $channel,
+ $subject,
+ $text,
+ $properties,
+ $notification_config
+ );
}
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 = <<EOF
-The node '$node' failed and needs manual intervention.
+my $body_template = <<EOT;
+{{#verbatim}}
+The node '{{node}}' failed and needs manual intervention.
-The PVE HA manager tries to fence it and recover the
-configured HA resources to a healthy node if possible.
+The PVE HA manager tries to fence it and recover the configured HA resources to
+a healthy node if possible.
-Current fence status: $subject_prefix
-$subject
+Current fence status: {{subject-prefix}}
+{{subject}}
+{{/verbatim}}
+{{heading-2 "Overall Cluster status:"}}
+{{object status-data}}
+EOT
-Overall Cluster status:
------------------------
+my $subject_template = "{{subject-prefix}}: {{subject}}";
-EOF
-;
- my $mail_subject = $subject_prefix . ': ' . $subject;
+# assembles a commont text for fence emails
+my $send_fence_state_email = sub {
+ my ($self, $subject_prefix, $subject, $node) = @_;
+ my $haenv = $self->{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.30.2
More information about the pve-devel
mailing list