Skip to content

Commit

Permalink
Limit size of emails
Browse files Browse the repository at this point in the history
  • Loading branch information
Chronial committed Jul 20, 2017
1 parent f6a2589 commit 9addf61
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions snapraid-runner.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ short = true
subject = [SnapRAID] Status Report:
from =
to =
; maximum email size in KiB
maxsize = 500

[smtp]
host =
Expand Down
18 changes: 16 additions & 2 deletions snapraid-runner.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf8 -*-
from __future__ import division

import argparse
import ConfigParser
import logging
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 9addf61

Please sign in to comment.