]> git.proxmox.com Git - pve-ha-manager.git/commitdiff
manager: send notifications via new notification module
authorLukas Wagner <l.wagner@proxmox.com>
Thu, 3 Aug 2023 12:16:50 +0000 (14:16 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Thu, 3 Aug 2023 15:34:52 +0000 (17:34 +0200)
... 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 <l.wagner@proxmox.com>
debian/control
src/PVE/HA/Env.pm
src/PVE/HA/Env/PVE2.pm
src/PVE/HA/NodeStatus.pm
src/PVE/HA/Sim/Env.pm

index 5bff56a0ea0bad62d6fbea62b7914ece1e660641..ffa9c1c444f56a5a0aa843bbed7656f6d7c78474 100644 (file)
@@ -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),
index 16603ec78ec4ef85a39ac37640e9cd1529d58741..b7060a43e5bc025a1587ef587fc8169184dbfe41 100644 (file)
@@ -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
index f6ebfebc208bafbf5cd99d06d09948afb33ae497..ea9e6e426f2cb4cb1a3e6ee083c0bf7216ed4e37 100644 (file)
@@ -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 = {};
index ee5be8e1eec40031b4b7c0ab5310238484c084c7..b264a367429b22c80757473ab02d02b9466b010c 100644 (file)
@@ -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
+    );
 };
 
 
index c6ea73c141258e74c1233148c2a80bc8561eaf6f..d3aea8d1c8f5a48b24b69277b7b7eeb465753634 100644 (file)
@@ -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);