[pve-devel] [PATCH ha-manager v2 4/7] Env: add sendmail

Thomas Lamprecht t.lamprecht at proxmox.com
Wed Jun 15 17:59:00 CEST 2016


Can be used to sen mails to the admin on certain important events
Do not log sent mails in regressions tests but in the simulator

Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---

changes since previous version:
* dismiss senmail logs in regression test, they bring no real value there but
  clutter the output, imo

 src/PVE/HA/Env.pm         | 6 ++++++
 src/PVE/HA/Env/PVE2.pm    | 9 +++++++++
 src/PVE/HA/Sim/Env.pm     | 7 +++++++
 src/PVE/HA/Sim/TestEnv.pm | 6 ++++++
 4 files changed, 28 insertions(+)

diff --git a/src/PVE/HA/Env.pm b/src/PVE/HA/Env.pm
index c7537b1..55f6684 100644
--- a/src/PVE/HA/Env.pm
+++ b/src/PVE/HA/Env.pm
@@ -132,6 +132,12 @@ sub log {
     return $self->{plug}->log($level, @args);
 }
 
+sub sendmail {
+    my ($self, $subject, $text) = @_;
+
+    return $self->{plug}->sendmail($subject, $text);
+}
+
 # acquire a cluster wide manager lock
 sub get_ha_manager_lock {
     my ($self) = @_;
diff --git a/src/PVE/HA/Env/PVE2.pm b/src/PVE/HA/Env/PVE2.pm
index 37823ee..ef6485d 100644
--- a/src/PVE/HA/Env/PVE2.pm
+++ b/src/PVE/HA/Env/PVE2.pm
@@ -226,6 +226,15 @@ sub log {
     syslog($level, $msg);
 }
 
+sub sendmail {
+    my ($self, $subject, $text) = @_;
+
+    my $mailfrom = 'root@' . $self->nodename();
+    my $mailto = 'root at localhost';
+
+    PVE::Tools::sendmail($mailto, $subject, $text, undef, $mailfrom);
+}
+
 my $last_lock_status = {};
 
 sub get_pve_lock {
diff --git a/src/PVE/HA/Sim/Env.pm b/src/PVE/HA/Sim/Env.pm
index 3715eff..cd1574c 100644
--- a/src/PVE/HA/Sim/Env.pm
+++ b/src/PVE/HA/Sim/Env.pm
@@ -224,6 +224,13 @@ sub log {
     printf("%-5s %5d %12s: $msg\n", $level, $time, "$self->{nodename}/$self->{log_id}");
 }
 
+sub sendmail {
+    my ($self, $subject, $text) = @_;
+
+    # only log subject, do not spam the logs
+    $self->log('email', $subject);
+}
+
 sub get_time {
     my ($self) = @_;
 
diff --git a/src/PVE/HA/Sim/TestEnv.pm b/src/PVE/HA/Sim/TestEnv.pm
index 93aeede..fe08be3 100644
--- a/src/PVE/HA/Sim/TestEnv.pm
+++ b/src/PVE/HA/Sim/TestEnv.pm
@@ -130,4 +130,10 @@ sub get_max_workers {
     return 0;
 }
 
+sub sendmail {
+    my ($self, $subject, $text) = @_;
+
+    # dismiss mails for regression tests
+}
+
 1;
-- 
2.1.4




More information about the pve-devel mailing list