From 9addf61e8e67cf9590ea1541680858bb7c39fdb0 Mon Sep 17 00:00:00 2001 From: Chronial Date: Thu, 20 Jul 2017 23:47:09 +0200 Subject: [PATCH] Limit size of emails --- README.md | 11 +++++++++++ snapraid-runner.conf.example | 2 ++ snapraid-runner.py | 18 ++++++++++++++++-- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 426e0f6..ab41fd9 100644 --- a/README.md +++ b/README.md @@ -22,3 +22,14 @@ scheduler. * Can create a size-limited rotated logfile. * Can send notification emails after each run or only for failures. * Can run `scrub` after `sync` + +## Changelog +###v0.3 (20 Jul 2017) +* Limit size of sent emails + +###v0.2 (27 Apr 2015) +* Fix compatibility with Snapraid 8.0 +* Allow disabling of scrub from command line + +###v0.1 (16 Feb 2014) +* Initial release diff --git a/snapraid-runner.conf.example b/snapraid-runner.conf.example index 0cc615f..a2ed8ce 100644 --- a/snapraid-runner.conf.example +++ b/snapraid-runner.conf.example @@ -20,6 +20,8 @@ short = true subject = [SnapRAID] Status Report: from = to = +; maximum email size in KiB +maxsize = 500 [smtp] host = diff --git a/snapraid-runner.py b/snapraid-runner.py index 6a37ec8..41a688e 100644 --- a/snapraid-runner.py +++ b/snapraid-runner.py @@ -1,4 +1,6 @@ # -*- coding: utf8 -*- +from __future__ import division + import argparse import ConfigParser import logging @@ -79,7 +81,19 @@ def send_email(success): body = "SnapRAID job completed successfully:\n\n\n" else: body = "Error during SnapRAID job:\n\n\n" - body += email_log.getvalue() + + 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") @@ -126,7 +140,7 @@ def load_config(args): int_options = [ ("snapraid", "deletethreshold"), ("logging", "maxsize"), - ("scrub", "percentage"), ("scrub", "older-than") + ("scrub", "percentage"), ("scrub", "older-than"), ("email", "maxsize"), ] for section, option in int_options: try: