]> git.proxmox.com Git - pve-manager.git/commitdiff
Send an email when a replication job fails.
authorWolfgang Link <w.link@proxmox.com>
Thu, 7 Dec 2017 11:07:00 +0000 (12:07 +0100)
committerWolfgang Bumiller <w.bumiller@proxmox.com>
Wed, 13 Dec 2017 13:51:07 +0000 (14:51 +0100)
A email notification will be send for each job when the job fails.
This message will only send when an error occurs and the fail count is on 1.

Reviewed-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
Acked-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
PVE/API2/Replication.pm
PVE/CLI/pvesr.pm
bin/init.d/pvesr.service

index 38449892f12d24ca10ecc4425d14a2d016d4a732..8c049363f74e938b338379614f18e8bb4daf7edc 100644 (file)
@@ -72,7 +72,7 @@ sub run_single_job {
 
 # passing $now and $verbose is useful for regression testing
 sub run_jobs {
-    my ($now, $logfunc, $verbose) = @_;
+    my ($now, $logfunc, $verbose, $mail) = @_;
 
     my $iteration = $now // time();
 
@@ -83,7 +83,21 @@ sub run_jobs {
 
        while (my $jobcfg = PVE::ReplicationState::get_next_job($iteration, $start_time)) {
            my $guest_class = $lookup_guest_class->($jobcfg->{vmtype});
-           PVE::Replication::run_replication($guest_class, $jobcfg, $iteration, $start_time, $logfunc, 1, $verbose);
+
+           eval {
+               PVE::Replication::run_replication($guest_class, $jobcfg, $iteration, $start_time, $logfunc, $verbose);
+           };
+           if (my $err = $@) {
+               warn "$jobcfg->{id}: got unexpected replication job error - $err";
+               my $state = PVE::ReplicationState::read_state();
+               my $jobstate = PVE::ReplicationState::extract_job_state($state, $jobcfg);
+               eval {
+                   PVE::Tools::sendmail('root', "Replication Job: $jobcfg->{id} failed", $err)
+                       if $jobstate->{fail_count} == 1 && $mail;
+               };
+               warn ": $@" if $@;
+           };
+
            $start_time = $now // time();
        }
     };
index 7da94404b6b11c01369c4e9fe5ed4e46e9f5f35f..cb79e2bf9e925e6f3dbe604568e102d01f8eed20 100644 (file)
@@ -221,12 +221,21 @@ __PACKAGE__->register_method ({
                default => 0,
                optional => 1,
            },
+           mail => {
+               description => "Send an email notification in case of a failure.",
+               type => 'boolean',
+               default => 0,
+               optional => 1,
+           },
        },
     },
     returns => { type => 'null' },
     code => sub {
        my ($param) = @_;
 
+       die "Mail and id are mutually exclusive!\n"
+           if $param->{id} && $param->{mail};
+
        my $logfunc;
 
        if ($param->{verbose}) {
@@ -242,7 +251,7 @@ __PACKAGE__->register_method ({
 
        } else {
 
-           PVE::API2::Replication::run_jobs(undef, $logfunc);
+           PVE::API2::Replication::run_jobs(undef, $logfunc, 0, $param->{mail});
        }
 
        return undef;
index 5706d4265b92cfc12d1f36f2e4372ba5fa9d635e..e0c082afc7ec44002243ae8caae62f9c40f3d43c 100644 (file)
@@ -4,4 +4,4 @@ ConditionPathExists=/usr/bin/pvesr
 
 [Service]
 Type=oneshot
-ExecStart=/usr/bin/pvesr run
+ExecStart=/usr/bin/pvesr run --mail 1