-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemailer.py
177 lines (156 loc) · 8.5 KB
/
emailer.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/python
import argparse
import datetime
import pymongo
import smtplib
import sys
import time
from email.mime.text import MIMEText
last_warning_check = datetime.datetime.utcnow() # when we last checked for warnings
last_email_time = last_warning_check # when we last sent messages
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--checktime', help='the number of seconds to wait after polling the database', type=int, default=300)
parser.add_argument('-a', '--alerttime', help='the minimum number of seconds for which inactivity from a uPMU is considered abnormal', type=int, default=1800)
parser.add_argument('-e', '--emailtime', help='the minimum number of seconds between emails', type=int, default=900)
parser.add_argument('-d', '--tolerablewarningdensity', help='the minimum density of warnings that causes an email alert', type=float, default=0.05)
parser.add_argument('senderaddr', help='the email address from which messages will be sent')
parser.add_argument('receiveraddrs', help='the email addresses to which messages will be sent', nargs='+')
args = parser.parse_args()
CHECKTIME = args.checktime
ALERTTIME = args.alerttime
EMAILTIME = args.emailtime
DENSITY_THRESHOLD = args.tolerablewarningdensity
SENDERADDR = args.senderaddr
RECEIVERADDRS = args.receiveraddrs
if len(sys.argv) < 3:
print 'Usage: ./emailer.py <sender email address> <receiver email addresses>'
exit()
alert = False # True if a uPMU has not send messages in ALERTTIME seconds
aliases = {}
try:
with open('serial_aliases.ini', 'r') as f:
for line in f:
pair = line.rstrip().split('=')
aliases[pair[0]] = pair[1]
except:
print 'WARNING: Could not read serial_aliases.ini'
client = pymongo.MongoClient()
latest_time = client.upmu_database.latest_time
warnings = client.upmu_database.warnings
warnings_summary = client.upmu_database.warnings_summary
inactive_serials = set()
events = {} # maps each serial number to a list of EventMessages
def add_event(serialNumber, event):
if serialNumber not in events:
events[serialNumber] = []
events[serialNumber].append(event)
def seconds_until_next_email():
""" Returns the number of seconds that must elapse until the next email can be sent. """
return EMAILTIME - (datetime.datetime.utcnow() - last_email_time).total_seconds()
def send_messages():
global last_email_time, alert
if len(events) == 0:
return # nothing to send
skeleton = """Hello,
This email is to inform you that events have ocurred regarding some uPMUs. The details are given below, listed by the serial number of each device and its alias, if known. All times listed below are in UTC.
{0}
This is an automated message. You should not reply to it."""
lines = [] # A list of lines in the message
for serialNum in events:
alias = aliases.get(serialNum, 'no alias found')
lines.append('Events regarding uPMU with serial number {0} ({1}):'.format(serialNum, alias))
for event in events[serialNum]:
lines.append(' {0}'.format(event))
lines.append('') # A blank line to separate serial numbers
txt = MIMEText(skeleton.format('\n'.join(lines)))
del txt['Subject']
if alert:
txt['Subject'] = 'ALERT: one or more uPMUs are inactive'
else:
txt['Subject'] = 'Automated uPMU Notification'
del txt['From']
txt['From'] = SENDERADDR
del txt['To']
txt['To'] = ', '.join(RECEIVERADDRS)
created = False
try:
emailsender = smtplib.SMTP('localhost')
created = True
refused = emailsender.sendmail(SENDERADDR, RECEIVERADDRS, txt.as_string())
events.clear() # So we don't get an entry in the next send for this serial number unless there was actually an event
emailsender.quit()
created = False
print 'Successfully sent email'
alert = False
for recipient in refused:
print 'WARNING: recipient {0} was refused by the server and did not receive the email'.format(recipient)
except smtplib.SMTPSenderRefused:
print 'WARNING: email not sent because sender ({0}) was refused'.format(SENDERADDR)
except smtplib.SMTPRecipientsRefused:
print 'WARNING: email not sent because all recipients were refused'
except BaseException as be:
print 'WARNING: email not sent'
print 'Details:', be
finally:
if created:
emailsender.quit()
last_email_time = datetime.datetime.utcnow()
class EventMessage(object):
warning = False
def __init__(self, description, event_time):
self.description = description
self.event_time = event_time
def __str__(self):
if self.description == 'active':
return 'NOTE: no messages had been received from the uPMU for at least {0} seconds, but device resumed to send messages at {1}'.format(ALERTTIME, self.event_time)
elif self.description == 'inactive':
return 'NOTE: no messages have been received for at least {0} seconds; last message received at {1}'.format(ALERTTIME, self.event_time)
return 'The event {0} ocurred at {1}.'.format(self.description, self.event_time)
class WarningMessage(EventMessage):
warning = True
def __init__(self, description, gen_time, start_time, end_time = None, prev_time = None):
EventMessage.__init__(self, description, gen_time)
self.start_time = start_time
self.end_time = end_time
self.prev_time = prev_time
def __str__(self):
if self.description == 'duplicate':
return 'WARNING: duplicate record for {0} (message generated at {1})'.format(self.start_time, self.event_time)
elif self.description == 'missing':
return 'WARNING: missing record(s): no data from {0} to {1} (message generated at {2})'.format(self.start_time, self.end_time, self.event_time)
elif self.description == 'missing file':
return 'WARNING: missing record(s): no data from {0} to {1}: no CSV file written (message generated at {2})'.format(self.start_time, self.end_time, self.event_time)
elif self.description == 'misplaced':
return 'WARNING: misplaced records(s) left uncorrected due to CSV boundary: new CSV file contains records from {0} to {1}, but would normally start at {2} (message generated at {3})'.format(self.start_time, self.end_time, self.prev_time, self.event_time)
while True:
# Add messages to events queue
for document in latest_time.find(): # Check for inactivity
serialNumber = document['serial_number']
lastReceived = document['time_received']
if (datetime.datetime.utcnow() - lastReceived).total_seconds() < ALERTTIME:
if serialNumber in inactive_serials: # if it's been fixed, mark it as active
inactive_serials.remove(serialNumber)
add_event(serialNumber, EventMessage('active', lastReceived))
elif serialNumber not in inactive_serials: # only add an event if hasn't been marked inactive it so we don't send too much spam
inactive_serials.add(serialNumber)
add_event(serialNumber, EventMessage('inactive', lastReceived))
alert = True
warning_check = datetime.datetime.utcnow() # The time of this warning check
for document in warnings_summary.find({'time': {'$gt': last_warning_check}}): # Check for warnings for missing/duplicate entries since the last time we checked
if document['written']:
# Check if the density of warnings is high enough to warrant an alert
density = document['num_warnings'] / (document['next_csv_start'] - document['csv_start']).total_seconds()
if density > DENSITY_THRESHOLD:
for doc in warnings.find({'serial_number': document['serial_number'], 'start_time': {'$gte': document['csv_start'], '$lt': document['next_csv_start']}}).sort('warning_time', pymongo.ASCENDING):
add_event(doc['serial_number'], WarningMessage(doc['warning_type'], doc['warning_time'], doc['start_time'], doc.get('end_time', None), doc.get('prev_time', None)))
else:
add_event(document['serial_number'], WarningMessage('missing file', document['time'], document['csv_start'], document['next_csv_start'] - datetime.timedelta(0, 1), None))
last_warning_check = warning_check
# Wait before checking again
seconds = seconds_until_next_email()
if seconds <= CHECKTIME:
time.sleep(max(seconds, 0))
send_messages()
time.sleep(min(CHECKTIME, CHECKTIME - seconds))
else:
time.sleep(CHECKTIME)