Add gotify notification

This commit is contained in:
tdehaeze committed 2022-07-26 20:51:48 +02:00
1 parent 60b8b6731c
commit df764ca47c
2 files changed
+31 -1

No files matched your search

+9
View File
@@ -25,6 +25,15 @@ to =
; maximum email size in KiB ; maximum email size in KiB
maxsize = 500 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] [smtp]
host = host =
; leave empty for default port ; leave empty for default port
+22 -1
View File
@@ -9,6 +9,7 @@ import sys
import threading import threading
import time import time
import traceback import traceback
import requests
from collections import Counter, defaultdict from collections import Counter, defaultdict
from io import StringIO from io import StringIO
@@ -64,6 +65,21 @@ def snapraid_command(command, args={}, *, allow_statuscodes=[]):
else: else:
raise subprocess.CalledProcessError(ret, "snapraid " + command) 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): def send_email(success):
import smtplib import smtplib
@@ -122,6 +138,11 @@ def finish(is_success):
send_email(is_success) send_email(is_success)
except Exception: except Exception:
logging.exception("Failed to send email") 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: if is_success:
logging.info("Run finished successfully") logging.info("Run finished successfully")
else: else:
@@ -133,7 +154,7 @@ def load_config(args):
global config global config
parser = configparser.RawConfigParser() parser = configparser.RawConfigParser()
parser.read(args.conf) 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) config = dict((x, defaultdict(lambda: "")) for x in sections)
for section in parser.sections(): for section in parser.sections():
for (k, v) in parser.items(section): for (k, v) in parser.items(section):