From 0c6d4b511df412d274bcc3e0060de63dc91c224a Mon Sep 17 00:00:00 2001 From: Nathan Flynn Date: Sat, 7 Mar 2020 15:25:47 +0000 Subject: [PATCH 1/4] added aprise notifications --- snapraid-runner.py | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/snapraid-runner.py b/snapraid-runner.py index 71d87c4..90e5cf6 100644 --- a/snapraid-runner.py +++ b/snapraid-runner.py @@ -125,10 +125,41 @@ def send_email(success): server.quit() +def send_notification(success): + import apprise + logging.info("sending msg") + # Create an Apprise instance + apobj = apprise.Apprise() + + # Create an Config instance + apprise_config = apprise.AppriseConfig() + + apprise_config_file = config["notification"]["config"] + # Add a configuration source: + apprise_config.add(apprise_config_file) + # Make sure to add our config into our apprise object + apobj.add(apprise_config) + + if success: + message_title = "SnapRAID job completed successfully:\n\n\n" + else: + message_title = "Error during SnapRAID job:\n\n\n" + + message_body = email_log.getvalue() + + # Then notify these services any time you desire. The below would + # notify all of the services that have not been bound to any specific + # tag. + apobj.notify( + body=message_body, + title=message_title, + ) + def finish(is_success): if ("error", "success")[is_success] in config["email"]["sendon"]: try: send_email(is_success) + send_notification(is_success) except Exception: logging.exception("Failed to send email") if is_success: @@ -142,7 +173,7 @@ def load_config(args): global config parser = configparser.RawConfigParser() parser.read(args.conf) - sections = ["snapraid", "logging", "email", "smtp", "scrub"] + sections = ["snapraid", "logging", "email", "smtp", "scrub", "notification"] config = dict((x, defaultdict(lambda: "")) for x in sections) for section in parser.sections(): for (k, v) in parser.items(section): @@ -162,7 +193,8 @@ def load_config(args): config["smtp"]["tls"] = (config["smtp"]["tls"].lower() == "true") 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["snapraid"]["touch"] = ( + config["snapraid"]["touch"].lower() == "true") if args.scrub is not None: config["scrub"]["enabled"] = args.scrub @@ -304,3 +336,4 @@ def run(): main() + From d33ebf93f33079d84f0a903b06e890b67ee86347 Mon Sep 17 00:00:00 2001 From: Nathan Flynn Date: Sat, 7 Mar 2020 15:42:19 +0000 Subject: [PATCH 2/4] remove email method as replaced by apprise --- .gitignore | 3 ++ snapraid-runner.conf.example | 27 ++++++-------- snapraid-runner.py | 68 ++++-------------------------------- 3 files changed, 20 insertions(+), 78 deletions(-) diff --git a/.gitignore b/.gitignore index c0fde79..bfb6d59 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ +*.yml +*.yaml +*.log snapraid-runner.conf diff --git a/snapraid-runner.conf.example b/snapraid-runner.conf.example index ed64810..9f62fd4 100644 --- a/snapraid-runner.conf.example +++ b/snapraid-runner.conf.example @@ -14,27 +14,22 @@ file = snapraid.log ; maximum logfile size in KiB, leave empty for infinite maxsize = 5000 -[email] -; when to send an email, comma-separated list of [success, error] +[notification] +enabled = true + +; when to send a notificariton on, comma-separated list of [success, error] sendon = success,error -; set to false to get full programm output via email + +; set to false to get full programm output short = true -subject = [SnapRAID] Status Report: -from = -to = + +; Python Apprise config. +; https://github.com/caronc/apprise/wiki/config_yaml +config = apprise.yml + ; maximum email size in KiB maxsize = 500 -[smtp] -host = -; leave empty for default port -port = -; set to "true" to activate -ssl = false -tls = false -user = -password = - [scrub] ; set to true to run scrub after sync enabled = false diff --git a/snapraid-runner.py b/snapraid-runner.py index 90e5cf6..d7dd028 100644 --- a/snapraid-runner.py +++ b/snapraid-runner.py @@ -73,58 +73,6 @@ def snapraid_command(command, args={}, *, allow_statuscodes=[]): else: raise subprocess.CalledProcessError(ret, "snapraid " + command) - -def send_email(success): - import smtplib - from email.mime.text import MIMEText - from email import charset - - if len(config["smtp"]["host"]) == 0: - logging.error("Failed to send email because smtp host is not set") - return - - # use quoted-printable instead of the default base64 - charset.add_charset("utf-8", charset.SHORTEST, charset.QP) - if success: - body = "SnapRAID job completed successfully:\n\n\n" - else: - body = "Error during SnapRAID job:\n\n\n" - - log = email_log.getvalue() - maxsize = config['email'].get('maxsize', 500) * 1024 - if maxsize and len(log) > maxsize: - cut_lines = log.count("\n", maxsize // 2, -maxsize // 2) - log = ( - "NOTE: Log was too big for email and was shortened\n\n" + - log[:maxsize // 2] + - "[...]\n\n\n --- LOG WAS TOO BIG - {} LINES REMOVED --\n\n\n[...]".format( - cut_lines) + - log[-maxsize // 2:]) - body += log - - msg = MIMEText(body, "plain", "utf-8") - msg["Subject"] = config["email"]["subject"] + \ - (" SUCCESS" if success else " ERROR") - msg["From"] = config["email"]["from"] - msg["To"] = config["email"]["to"] - smtp = {"host": config["smtp"]["host"]} - if config["smtp"]["port"]: - smtp["port"] = config["smtp"]["port"] - if config["smtp"]["ssl"]: - server = smtplib.SMTP_SSL(**smtp) - else: - server = smtplib.SMTP(**smtp) - if config["smtp"]["tls"]: - server.starttls() - if config["smtp"]["user"]: - server.login(config["smtp"]["user"], config["smtp"]["password"]) - server.sendmail( - config["email"]["from"], - [config["email"]["to"]], - msg.as_string()) - server.quit() - - def send_notification(success): import apprise logging.info("sending msg") @@ -156,12 +104,11 @@ def send_notification(success): ) def finish(is_success): - if ("error", "success")[is_success] in config["email"]["sendon"]: + if ("error", "success")[is_success] in config["notification"]["sendon"]: try: - send_email(is_success) send_notification(is_success) except Exception: - logging.exception("Failed to send email") + logging.exception("Failed to send notification") if is_success: logging.info("Run finished successfully") else: @@ -173,7 +120,7 @@ def load_config(args): global config parser = configparser.RawConfigParser() parser.read(args.conf) - sections = ["snapraid", "logging", "email", "smtp", "scrub", "notification"] + sections = ["snapraid", "logging", "scrub", "notification"] config = dict((x, defaultdict(lambda: "")) for x in sections) for section in parser.sections(): for (k, v) in parser.items(section): @@ -181,7 +128,7 @@ def load_config(args): int_options = [ ("snapraid", "deletethreshold"), ("logging", "maxsize"), - ("scrub", "percentage"), ("scrub", "older-than"), ("email", "maxsize"), + ("scrub", "percentage"), ("scrub", "older-than"), ] for section, option in int_options: try: @@ -189,10 +136,7 @@ def load_config(args): except ValueError: config[section][option] = 0 - config["smtp"]["ssl"] = (config["smtp"]["ssl"].lower() == "true") - config["smtp"]["tls"] = (config["smtp"]["tls"].lower() == "true") config["scrub"]["enabled"] = (config["scrub"]["enabled"].lower() == "true") - config["email"]["short"] = (config["email"]["short"].lower() == "true") config["snapraid"]["touch"] = ( config["snapraid"]["touch"].lower() == "true") @@ -222,12 +166,12 @@ def setup_logger(): file_logger.setFormatter(log_format) root_logger.addHandler(file_logger) - if config["email"]["sendon"]: + if config["notification"]["sendon"]: global email_log email_log = StringIO() email_logger = logging.StreamHandler(email_log) email_logger.setFormatter(log_format) - if config["email"]["short"]: + if config["notification"]["short"]: # Don't send programm stdout in email email_logger.setLevel(logging.INFO) root_logger.addHandler(email_logger) From dcc5f3b23347effe7fa185ad049a54f4cd85c837 Mon Sep 17 00:00:00 2001 From: Nathan Flynn Date: Tue, 12 May 2020 07:39:46 +0000 Subject: [PATCH 3/4] formatting tweaks --- snapraid-runner.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/snapraid-runner.py b/snapraid-runner.py index d7dd028..cefc40c 100644 --- a/snapraid-runner.py +++ b/snapraid-runner.py @@ -22,7 +22,7 @@ # Global variables config = None -email_log = None +notification_log = None def tee_log(infile, out_lines, log_level): @@ -93,7 +93,7 @@ def send_notification(success): else: message_title = "Error during SnapRAID job:\n\n\n" - message_body = email_log.getvalue() + message_body = notification_log.getvalue() # Then notify these services any time you desire. The below would # notify all of the services that have not been bound to any specific @@ -167,14 +167,14 @@ def setup_logger(): root_logger.addHandler(file_logger) if config["notification"]["sendon"]: - global email_log - email_log = StringIO() - email_logger = logging.StreamHandler(email_log) - email_logger.setFormatter(log_format) + global notification_log + notification_log = StringIO() + notification_logger = logging.StreamHandler(notification_log) + notification_logger.setFormatter(log_format) if config["notification"]["short"]: # Don't send programm stdout in email - email_logger.setLevel(logging.INFO) - root_logger.addHandler(email_logger) + notification_logger.setLevel(logging.INFO) + root_logger.addHandler(notification_logger) def main(): From e3d4bd5c69141167e8bd502dff7c9f0997a2f048 Mon Sep 17 00:00:00 2001 From: Nathan Flynn Date: Tue, 12 May 2020 07:41:08 +0000 Subject: [PATCH 4/4] added apprise example yml --- apprise.yml.example | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 apprise.yml.example diff --git a/apprise.yml.example b/apprise.yml.example new file mode 100644 index 0000000..57649e4 --- /dev/null +++ b/apprise.yml.example @@ -0,0 +1,3 @@ +urls: + - pbul://MY-KEY +