public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Lukas Wagner <l.wagner@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH v6 pve-ha-manager 01/30] manager: send notifications via new notification module
Date: Thu,  3 Aug 2023 14:16:50 +0200	[thread overview]
Message-ID: <20230803121719.519207-2-l.wagner@proxmox.com> (raw)
In-Reply-To: <20230803121719.519207-1-l.wagner@proxmox.com>

... 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           |  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 = <<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.39.2





  reply	other threads:[~2023-08-03 12:17 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-03 12:16 [pve-devel] [PATCH v6 many 00/30] fix #4156: introduce new notification system Lukas Wagner
2023-08-03 12:16 ` Lukas Wagner [this message]
2023-08-03 15:39   ` [pve-devel] applied: [PATCH v6 pve-ha-manager 01/30] manager: send notifications via new notification module Thomas Lamprecht
2023-08-03 12:16 ` [pve-devel] [PATCH v6 pve-manager 02/30] d/control: add dependency to `libpve-notify-perl` Lukas Wagner
2023-08-03 12:16 ` [pve-devel] [PATCH v6 pve-manager 03/30] vzdump: send notifications via new notification module Lukas Wagner
2023-08-03 12:16 ` [pve-devel] [PATCH v6 pve-manager 04/30] test: rename mail_test.pl to vzdump_notification_test.pl Lukas Wagner
2023-08-03 12:16 ` [pve-devel] [PATCH v6 pve-manager 05/30] api: apt: send notification via new notification module Lukas Wagner
2023-08-03 12:16 ` [pve-devel] [PATCH v6 pve-manager 06/30] api: replication: send notifications " Lukas Wagner
2023-08-03 12:16 ` [pve-devel] [PATCH v6 pve-manager 07/30] api: prepare api handler module for notification config Lukas Wagner
2023-08-03 12:16 ` [pve-devel] [PATCH v6 pve-manager 08/30] api: notification: add api routes for groups Lukas Wagner
2023-08-03 12:16 ` [pve-devel] [PATCH v6 pve-manager 09/30] api: notification: add api routes for sendmail endpoints Lukas Wagner
2023-08-03 12:16 ` [pve-devel] [PATCH v6 pve-manager 10/30] api: notification: add api routes for gotify endpoints Lukas Wagner
2023-08-03 12:17 ` [pve-devel] [PATCH v6 pve-manager 11/30] api: notification: add api routes for filters Lukas Wagner
2023-08-03 12:17 ` [pve-devel] [PATCH v6 pve-manager 12/30] api: notification: allow fetching notification targets Lukas Wagner
2023-08-03 12:17 ` [pve-devel] [PATCH v6 pve-manager 13/30] api: notification: allow to test targets Lukas Wagner
2023-08-03 12:17 ` [pve-devel] [PATCH v6 pve-manager 14/30] api: notification: disallow removing targets if they are used Lukas Wagner
2023-08-03 12:17 ` [pve-devel] [PATCH v6 pve-manager 15/30] ui: backup: allow to select notification target for jobs Lukas Wagner
2023-08-03 12:17 ` [pve-devel] [PATCH v6 pve-manager 16/30] ui: backup: adapt backup job details to new notification params Lukas Wagner
2023-08-03 12:17 ` [pve-devel] [PATCH v6 pve-manager 17/30] ui: backup: allow to set notification-target for one-off backups Lukas Wagner
2023-08-03 12:17 ` [pve-devel] [PATCH v6 pve-manager 18/30] ui: allow to configure notification event -> target mapping Lukas Wagner
2023-08-03 12:17 ` [pve-devel] [PATCH v6 pve-manager 19/30] ui: add notification target configuration panel Lukas Wagner
2023-08-03 12:17 ` [pve-devel] [PATCH v6 pve-manager 20/30] ui: perm path: add ACL paths for notifications, usb and pci mappings Lukas Wagner
2023-08-03 12:17 ` [pve-devel] [PATCH v6 pve-manager 21/30] ui: perm path: increase width of the perm path selector combobox Lukas Wagner
2023-08-03 12:17 ` [pve-devel] [PATCH v6 pve-manager 22/30] ui: dc: remove notify key from datacenter option view Lukas Wagner
2023-08-03 12:17 ` [pve-devel] [PATCH v6 pve-manager 23/30] vzdump: use <name> as a convention for virtual endpoints/groups Lukas Wagner
2023-08-03 12:17 ` [pve-devel] [PATCH v6 pve-manager 24/30] api: notification: make the 'mail-to-root' target visible to any user Lukas Wagner
2023-08-03 12:17 ` [pve-devel] [PATCH v6 proxmox-widget-toolkit 25/30] notification: add gui for sendmail notification endpoints Lukas Wagner
2023-08-03 12:17 ` [pve-devel] [PATCH v6 proxmox-widget-toolkit 26/30] notification: add gui for gotify " Lukas Wagner
2023-08-03 12:17 ` [pve-devel] [PATCH v6 proxmox-widget-toolkit 27/30] notification: add gui for notification groups Lukas Wagner
2023-08-03 12:17 ` [pve-devel] [PATCH v6 proxmox-widget-toolkit 28/30] notification: allow to select filter for notification targets Lukas Wagner
2023-08-03 12:17 ` [pve-devel] [PATCH v6 proxmox-widget-toolkit 29/30] notification: add ui for managing notification filters Lukas Wagner
2023-08-03 12:17 ` [pve-devel] [PATCH v6 pve-docs 30/30] add documentation for the new notification system Lukas Wagner
2023-08-11 11:22 ` [pve-devel] [PATCH v6 many 00/30] fix #4156: introduce " Dominik Csapak
2023-08-16 10:14 ` [pve-devel] applied-series: " Wolfgang Bumiller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230803121719.519207-2-l.wagner@proxmox.com \
    --to=l.wagner@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal