5
0
mirror of git://git.proxmox.com/git/pve-ha-manager.git synced 2025-01-03 05:17:57 +03:00

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 <l.wagner@proxmox.com>
This commit is contained in:
Lukas Wagner 2023-08-03 14:16:50 +02:00 committed by Thomas Lamprecht
parent dfe080bab1
commit 4cb3b2cf9b
5 changed files with 57 additions and 34 deletions

2
debian/control vendored
View 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),

View 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

View 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';
PVE::Tools::sendmail($mailto, $subject, $text, undef, $mailfrom);
if ($notify eq 'always') {
PVE::Notify::error($target, $subject, $text, $properties);
}
};
$self->log("warning", "could not notify: $@") if $@;
}
my $last_lock_status_hash = {};

View File

@ -188,35 +188,45 @@ sub update {
}
}
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.
Current fence status: {{subject-prefix}}
{{subject}}
{{/verbatim}}
{{heading-2 "Overall Cluster status:"}}
{{object status-data}}
EOT
my $subject_template = "{{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 $mail_text = <<EOF
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.
Current fence status: $subject_prefix
$subject
Overall Cluster status:
-----------------------
EOF
;
my $mail_subject = $subject_prefix . ': ' . $subject;
my $status = $haenv->read_manager_status();
my $data = { manager_status => $status, node_status => $self->{status} };
$mail_text .= to_json($data, { pretty => 1, canonical => 1});
my $notification_properties = {
"status-data" => {
manager_status => $status,
node_status => $self->{status}
},
"node" => $node,
"subject-prefix" => $subject_prefix,
"subject" => $subject,
};
$haenv->sendmail($mail_subject, $mail_text);
$haenv->send_notification(
$subject_template,
$body_template,
$notification_properties
);
};

View 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);