-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsend_error_email.py
59 lines (50 loc) · 1.28 KB
/
send_error_email.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import accounting
import sys
from datetime import datetime
from collections import deque
def get_name(filter):
filter_name = filter.__name__
name = ""
if 'Osg' in filter_name:
name = "OSPool"
if "Chtc" in filter_name:
name = "CHTC"
if "Gpu" in filter_name:
name = f"{name} GPU"
return name
def get_subject(args):
datestr = datetime.fromtimestamp(args.start_ts).strftime("%Y-%m-%d")
subject_str = f"Error sending {get_name(args.filter)} {args.report_period.capitalize()} Usage Report {datestr}"
return subject_str
def log_tail(logfile, n=10):
if logfile is None:
return "No log file found."
lines = deque(maxlen=n)
try:
with open(logfile, "r") as f:
for line in f:
lines.append(line.rstrip())
except Exception:
return f"Error reading log file {logfile}."
else:
return "\n".join(lines)
if __name__ == "__main__":
args = accounting.parse_args(sys.argv[1:])
subject = get_subject(args)
html = f"""
<html>
<head>
</head>
<body>
Last 10 lines of log
<p style="font-family: monospace; white-space: pre">
{log_tail(args.log_file)}
</p>
</body>
</html>
"""
accounting.send_email(
subject=subject,
html=html,
**vars(args),
)