[pbs-devel] [RFC proxmox-backup 4/8] server: add sanity check job email notifications

Christian Ebner c.ebner at proxmox.com
Wed Dec 13 16:38:15 CET 2023


Defines the email templates and method to send success/failure
notification emails for the sanity check jobs.

Signed-off-by: Christian Ebner <c.ebner at proxmox.com>
---
 src/server/email_notifications.rs | 78 +++++++++++++++++++++++++++++++
 1 file changed, 78 insertions(+)

diff --git a/src/server/email_notifications.rs b/src/server/email_notifications.rs
index 43b55656..7672f30a 100644
--- a/src/server/email_notifications.rs
+++ b/src/server/email_notifications.rs
@@ -241,6 +241,36 @@ Please visit the web interface for further details:
 
 "###;
 
+const SANITY_CHECK_OK_TEMPLATE: &str = r###"
+
+Job ID:    {{jobname}}
+
+Sanity check successful.
+
+
+Please visit the web interface for further details:
+
+<https://{{fqdn}}:{{port}}/#pbsServerAdministration:tasks>
+
+"###;
+
+const SANITY_CHECK_ERR_TEMPLATE: &str = r###"
+
+Job ID:    {{jobname}}
+
+Sanity checks failed:
+
+{{#each errors}}
+  {{this~}}
+{{/each}}
+
+
+Please visit the web interface for further details:
+
+<https://{{fqdn}}:{{port}}/#pbsServerAdministration:tasks>
+
+"###;
+
 lazy_static::lazy_static! {
 
     static ref HANDLEBARS: Handlebars<'static> = {
@@ -272,6 +302,9 @@ lazy_static::lazy_static! {
 
             hb.register_template_string("certificate_renewal_err_template", ACME_CERTIFICATE_ERR_RENEWAL)?;
 
+            hb.register_template_string("sanity_check_ok_template", SANITY_CHECK_OK_TEMPLATE)?;
+            hb.register_template_string("sanity_check_err_template", SANITY_CHECK_ERR_TEMPLATE)?;
+
             Ok(())
         });
 
@@ -460,6 +493,48 @@ pub fn send_prune_status(
     Ok(())
 }
 
+pub fn send_sanity_check_status(
+    email: &str,
+    notify: Option<Notify>,
+    jobname: &str,
+    result: &Result<Vec<String>, Error>,
+) -> Result<(), Error> {
+    match notify {
+        None => { /* send notifications by default */ }
+        Some(notify) => {
+            if notify == Notify::Never || (result.is_ok() && notify == Notify::Error) {
+                return Ok(());
+            }
+        }
+    }
+
+    let (fqdn, port) = get_server_url();
+    let mut data = json!({
+        "jobname": jobname,
+        "fqdn": fqdn,
+        "port": port,
+    });
+
+    let (subject, text) = match result {
+        Ok(errors) if errors.is_empty() => (
+            format!("Sanity check successful"),
+            HANDLEBARS.render("sanity_check_ok_template", &data)?,
+        ),
+        Ok(errors) => {
+            data["errors"] = json!(errors);
+            (
+                format!("Sanity check failed"),
+                HANDLEBARS.render("sanity_check_err_template", &data)?,
+            )
+        }
+        Err(_) => return Ok(()),
+    };
+
+    send_job_status_mail(&email, &subject, &text)?;
+
+    Ok(())
+}
+
 pub fn send_sync_status(
     email: &str,
     notify: DatastoreNotify,
@@ -760,4 +835,7 @@ fn test_template_register() {
     assert!(HANDLEBARS.has_template("package_update_template"));
 
     assert!(HANDLEBARS.has_template("certificate_renewal_err_template"));
+
+    assert!(HANDLEBARS.has_template("sanity_check_ok_template"));
+    assert!(HANDLEBARS.has_template("sanity_check_err_template"));
 }
-- 
2.39.2





More information about the pbs-devel mailing list