diff --git a/snapraid-runner.conf.example b/snapraid-runner.conf.example index bc8fca4..27f0d15 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 @@ -42,3 +51,7 @@ enabled = false plan = 12 ; minimum block age (in days) for scrubbing. Only used with percentage plans older-than = 10 + +[sync] +; set to true to force sync files with zero size +force-zero = false diff --git a/snapraid-runner.py b/snapraid-runner.py index 56b8e9e..755450c 100755 --- a/snapraid-runner.py +++ b/snapraid-runner.py @@ -9,6 +9,7 @@ import threading import time import traceback +import requests from collections import Counter, defaultdict from io import StringIO @@ -41,7 +42,9 @@ def snapraid_command(command, args={}, *, allow_statuscodes=[]): arguments = ["--conf", config["snapraid"]["config"], "--quiet"] for (k, v) in args.items(): - arguments.extend(["--" + k, str(v)]) + arguments.append("--" + k) + if v != '': + arguments.append(str(v)) p = subprocess.Popen( [config["snapraid"]["executable"], command] + arguments, stdout=subprocess.PIPE, @@ -64,6 +67,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 +140,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 +156,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", "sync"] config = dict((x, defaultdict(lambda: "")) for x in sections) for section in parser.sections(): for (k, v) in parser.items(section): @@ -154,6 +177,7 @@ def load_config(args): config["scrub"]["enabled"] = (config["scrub"]["enabled"].lower() == "true") config["email"]["short"] = (config["email"]["short"].lower() == "true") config["snapraid"]["touch"] = (config["snapraid"]["touch"].lower() == "true") + config["sync"]["force-zero"] = (config["sync"]["force-zero"].lower() == "true") # Migration if config["scrub"]["percentage"]: @@ -281,8 +305,11 @@ def run(): logging.info("No changes detected, no sync required") else: logging.info("Running sync...") + sync_args: dict[str, str] = {} + if config["sync"]["force-zero"]: + sync_args["force-zero"] = "" try: - snapraid_command("sync") + snapraid_command("sync", sync_args) except subprocess.CalledProcessError as e: logging.error(e) finish(False)