From df764ca47cf26d61bb986d112103b839a2b4a8e2 Mon Sep 17 00:00:00 2001 From: Thomas Dehaeze Date: Tue, 26 Jul 2022 20:51:48 +0200 Subject: [PATCH] Add gotify notification --- snapraid-runner.conf.example | 9 +++++++++ snapraid-runner.py | 23 ++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/snapraid-runner.conf.example b/snapraid-runner.conf.example index bc8fca4..f99f40b 100644 --- a/snapraid-runner.conf.example +++ b/snapraid-runner.conf.example @@ -25,6 +25,15 @@ to = ; maximum email size in KiB maxsize = 500 +[gotify] +; when to send a gotify notification, comma-separated list of [success, error] +sendon = success,error +; url of the gotify server without trailing slash +url = https://gotify.server.com +; Gotify token +token = + + [smtp] host = ; leave empty for default port diff --git a/snapraid-runner.py b/snapraid-runner.py index 56b8e9e..d08c05b 100755 --- a/snapraid-runner.py +++ b/snapraid-runner.py @@ -9,6 +9,7 @@ import sys import threading import time import traceback +import requests from collections import Counter, defaultdict from io import StringIO @@ -64,6 +65,21 @@ def snapraid_command(command, args={}, *, allow_statuscodes=[]): else: raise subprocess.CalledProcessError(ret, "snapraid " + command) +def send_gotify(success): + + if success: + requests.post("{}/message?token={}".format(config["gotify"]["url"], config["gotify"]["token"]), json={ + "message": "Success", + "priority": 2, + "title": "SnapRAID" + }) + else: + requests.post("{}/message?token={}".format(config["gotify"]["url"], config["gotify"]["token"]), json={ + "message": "Failed", + "priority": 2, + "title": "SnapRAID" + }) + def send_email(success): import smtplib @@ -122,6 +138,11 @@ def finish(is_success): send_email(is_success) except Exception: logging.exception("Failed to send email") + if ("error", "success")[is_success] in config["gotify"]["sendon"]: + try: + send_gotify(is_success) + except Exception: + logging.exception("Failed to send gotify notification") if is_success: logging.info("Run finished successfully") else: @@ -133,7 +154,7 @@ def load_config(args): global config parser = configparser.RawConfigParser() parser.read(args.conf) - sections = ["snapraid", "logging", "email", "smtp", "scrub"] + sections = ["snapraid", "logging", "email", "gotify", "smtp", "scrub"] config = dict((x, defaultdict(lambda: "")) for x in sections) for section in parser.sections(): for (k, v) in parser.items(section):