From 4cb3b2cf9baaf069b1fa505ba6c4fe5f7f3659ec Mon Sep 17 00:00:00 2001 From: Lukas Wagner Date: Thu, 3 Aug 2023 14:16:50 +0200 Subject: [PATCH] manager: send notifications via new notification module ... 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.5