-
Notifications
You must be signed in to change notification settings - Fork 0
/
OutageParse.py
391 lines (309 loc) · 16.1 KB
/
OutageParse.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
from bs4 import BeautifulSoup
from Dashboard import connect_db
from flask import Config
import sys, re, datetime, sqlite3, os, imaplib, email, pdb, smtplib, logging
def FromEmails():
#Config, init variables
config = Config(os.getcwd())
config.from_pyfile('settings.cfg')
logger = logging.getLogger('ICONZ.HelpdeskDashboard.OutageParse')
logger.addHandler(config['HANDLER'])
def GetBody(msg):
if msg.is_multipart():
return(msg.get_payload(0)).get_payload(decode=True)
else:
return(msg.get_payload(decode=True))
def Passthrough(msg_list):
try:
smtp_conn = smtplib.SMTP(config['OUTAGES_SMTP_SERVER'],
config['OUTAGES_SMTP_PORT'])
for msg in msg_list:
smtp_conn.sendmail(config['OUTAGES_SMTP_FROM_ADDR'],
config['OUTAGES_SMTP_TO_ADDR'], msg.as_string())
logger.info('Succesfully passed through ' + str(len(msg_list)) + ' messages')
except Exception as e:
logger.error('Faliure to passthrough all messages.', exc_info = True)
def DBInsert(data):
#Could tidy this up so it's done as a batch operation. Probably unecessary
conn = connect_db()
cur = conn.cursor()
try:
cur.execute('select rowid from outage_texts where provider_ref_id match(?)',
[data['reference']])
db_outage = cur.fetchone()
db_outage_id = None
db_outage_text_id = None
if db_outage == None:
cur.execute('insert into outage_texts (provider_ref_id, equipment_list, full_text) values (?,?,?)',
[unicode(data['reference']), unicode(data['equipment']), unicode(data['full_text'])])
db_outage_text_id = cur.lastrowid
cur.execute('insert into outages (provider, num_affected, start_time, end_time, issues, text_id) values (?,?,?,?,?,?)',
[data['provider'], data['num_affected'], data['start_datetime'], data['end_datetime'], data['issues'], db_outage_text_id])
db_outage_id = cur.lastrowid
conn.commit()
logger.debug('Created new outage with id ' + str(db_outage_id))
else:
cur.execute('update outage_texts set equipment_list=(?), full_text=(?) where rowid==?',
[unicode(data['equipment']), unicode(data['full_text']), db_outage[0]])
db_outage_text_id = db_outage[0]
cur.execute('select id from outages where text_id==?', [db_outage_text_id])
db_outage_id = cur.fetchone()[0]
cur.execute('update outages set num_affected=(?), start_time=(?), end_time=(?), issues=(?) where id==?',
[data['num_affected'], data['start_datetime'], data['end_datetime'], data['issues'], db_outage_id])
conn.commit()
logger.debug('Updated outage with id ' + str(db_outage_id))
users = list()
for line in data['equipment'].split('\n'):
cur.execute('select id, name from dslams where name==?', [line])
dslam = cur.fetchone()
if dslam is not None:
cur.execute('select id, asid from dslusers where dslam_id==?', [dslam[0]])
users.append(cur.fetchall())
for db_block in users:
for user in db_block:
if user is not None:
cur.execute('select * from outages_dslusers_rel where (outages_id==?) and (dslusers_id==?)', [db_outage_id, user[0]])
if cur.fetchone() is None:
cur.execute('insert into outages_dslusers_rel (outages_id, dslusers_id) values (?,?)',
[db_outage_id, user[0]])
conn.commit()
#Clears connection so that program can continue in case of exception
except sqlite3.IntegrityError as e:
conn.close()
conn = connect_db()
raise(e)
def ChorusHTMLFat(data):
def FindContent(fieldname, soup, one = True):
if one:
out = [string for string in soup.find(
"td", text=re.compile(fieldname), style=re.compile('(;color:|; COLOR: )rgb\(0,72,97\);')
).parent.find(
"td", style=re.compile('(;color:|; COLOR: )rgb\(0,0,0\);')
).stripped_strings]
if len(out) < 1: return None
else: return out[0]
else:
return [string for string in soup.find(
"td", text=re.compile(fieldname), style=re.compile('(;color:|; COLOR: )rgb\(0,72,97\);')
).parent.find(
"td", style=re.compile('(;color:|; COLOR: )rgb\(0,0,0\);')
).stripped_strings]
soup = BeautifulSoup(data)
results = dict({'provider' : 'Chorus', 'reference' : FindContent('Ticket Number:', soup), 'issues' : False})
try:
start_date = FindContent('Date:', soup)
start_time = FindContent('Start Time:', soup)
if start_time is not None:
if start_time.endswith('(amended)'):
start_time = start_time[0:6]
else:
results['issues'] = True
start_time = '00:00'
results['start_datetime'] = datetime.datetime.strptime(start_date + ' ' +
start_time, '%d/%m/%Y %H:%M')
except (AttributeError, ValueError):
results['issues'] = True
results['start_datetime'] = None
try:
end_date = FindContent('Resolution Date:', soup)
end_time = FindContent('Resolution Time:', soup)
if end_date is None:
if end_time is None:
results['end_datetime'] = None
else:
results['end_datetime'] = datetime.datetime.combine(start_datetime.date,
datetime.time.strptime(end_time, '%H:%M'))
else:
results['end_datetime'] = datetime.datetime.strptime(end_date + ' ' +
end_time, '%d/%m/%Y %H:%M')
except (AttributeError, ValueError):
results['end_datetime'] = None
try:
results['num_affected'] = int(re.search(('(\d+)'), FindContent('Customer Impact:',
soup)).group(1))
except AttributeError:
results['issues'] = True
results['num_affected'] = 0
#Might be useful later if I can get ASID -> DSLAM mappings
equipment = FindContent('Equipment List:', soup, False)
results['equipment'] = ''
for string in equipment:
if re.match('[A-Z]{2,3}[-|_|/]([A-Z]{1,3}-)?DSLAM-\d{0,2}', string):
results['equipment'] += string + '\n'
else:
results['issues'] = True
results['full_text'] = ''
for string in soup.stripped_strings:
if re.match('^VF \w{1,3}$', string):
string = ''
results['full_text'] = results['full_text'] + string + '\n'
return results
def ChorusPlainText(f):
results = dict({'provider' : 'Chorus',
'reference' : re.search(('SED Ref.+:.+(\d{6})'), f).group(1),
'issues' : False})
start_date = re.search(('DATE.+:.+(\d{2}/\d{2}/\d{2})'), f)
start_time = re.search(('NZ Standard Time.+:.+(\d{2}:\d{2}:\d{2}.(AM|PM))'), f)
results['start_datetime'] = datetime.datetime.strptime(start_date.group(1) + ' ' +
start_time.group(1), '%d/%m/%y %I:%M:%S %p')
duration = re.search(('DURATION \(HR:MIN:SEC\).+:.+(\d{3}):(\d{2}):(\d{2})'), f)
results['end_datetime'] = results['start_datetime'] + datetime.timedelta(hours = int(duration.group(1)),
minutes = int(duration.group(2)), seconds = int(duration.group(3)))
try:
results['num_affected'] = int(re.search('Up to (\d+) DSL customers', f).group(1))
except AttributeError:
results['issues'] = True
results['num_affected'] = 0
logger.warning('ChorusPlainText could not extract num_affected')
equipment = re.findall(('([A-Z]{2,3}[-|_|/]([A-Z]{1,3}-)?DSLAM-\d{0,2})'), f)
results['equipment'] = ''
for regex_groups in equipment:
results['equipment'] += regex_groups[0] + '\n'
if 'DSLAM' not in results['equipment']:
results['issues'] = True
logger.warning('ChorusPlainText could not extract equipment_list')
results['full_text'] = str(f)
if results['issues'] == False:
logger.debug('ChorusPlainText parsed message without issue')
return results
def ChorusHTMLSkinny(outage):
soup = BeautifulSoup(outage)
results = dict({'provider' : 'Chorus', 'issues' : False})
text = ''
#Mandated by shitty HTML
stringgen = soup.stripped_strings
for string in stringgen:
if ('Field' not in string) and ('FIELD' not in string):
if 'Ticket Number:' in string:
stringgen.next()
results['reference'] = stringgen.next()
text += results['reference'] + '\n'
elif 'Location:' in string:
stringgen.next()
results['location'] = stringgen.next()
text += results['location'] + '\n'
elif 'Start Date:' in string:
stringgen.next()
results['start_datetime'] = stringgen.next()
text += results['start_datetime'] + '\n'
elif 'Start Time:' in string:
stringgen.next()
temp = stringgen.next()
results['start_datetime'] = results['start_datetime'] + ' ' + temp
text += temp + '\n'
elif 'Resolution Date:' in string:
stringgen.next()
results['end_datetime'] = stringgen.next()
text += results['end_datetime'] + '\n'
elif 'Resolution Time:' in string:
stringgen.next()
temp = stringgen.next()
results['end_datetime'] += ' ' + temp
text += temp + '\n'
elif 'Customer Impact:' in string:
stringgen.next()
temp = stringgen.next()
try:
results['num_affected'] = int(re.search('Up to (\d+) DSL customers', temp).group(1))
except AttributeError:
results['issues'] = True
results['num_affected'] = 0
logger.warning('ChorusHTMLSkinny could not extract num_affected')
text += temp + '\n'
elif 'Equipment List:' in string:
results['equipment'] = ''
stringgen.next()
temp = stringgen.next()
while 'Additional Information:' not in temp:
results['equipment'] += temp
text += temp + '\n'
temp = stringgen.next()
elif 'Cable Pair & Range:' in string:
results['equipment'] = ''
stringgen.next()
temp = stringgen.next()
while 'Additional Information:' not in temp:
results['equipment'] += temp
text += temp + '\n'
temp = stringgen.next()
else:
text += string + '\n'
if (results['start_datetime'] is not None) and (len(results['start_datetime']) == 16):
try:
results['start_datetime'] = datetime.datetime.strptime(results['start_datetime'], '%d/%m/%Y %H:%M')
except ValueError:
try:
results['start_datetime'] = datetime.datetime.strptime(results['start_datetime'], '%H:%M %d/%m/%Y')
except ValueError:
results['issues'] = True
results['start_datetime'] = None
logger.warning('ChorusHTMLSkinny could not extract start_datetime')
elif results['start_datetime'] is not None:
results['issues'] = True
results['start_datetime'] = None
logger.warning('ChorusHTMLSkinny could not extract start_datetime')
else:
results['issues'] = True
logger.error('Fallthrough condition in ChorusHTMLSkinny when parsing start_datetime')
if (results['end_datetime'] is not None) and (len(results['end_datetime']) == 16):
try:
results['end_datetime'] = datetime.datetime.strptime(results['end_datetime'], '%d/%m/%Y %H:%M')
except ValueError:
try:
results['end_datetime'] = datetime.datetime.strptime(results['end_datetime'], '%H:%M %d/%m/%Y')
except ValueError:
results['issues'] = True
results['end_datetime'] = None
logger.warning('ChorusHTMLSkinny could not extract end_datetime')
else:
results['end_datetime'] = None
logger.warning('ChorusHTMLSkinny could not extract end_datetime')
results['full_text'] = text
if results['issues'] == False:
logger.debug('ChorusHTMLSkinny parsed message without issue')
return results
#Actual script begins here.
passthrough_list = list()
#IMAP login
imap = imaplib.IMAP4(config['OUTAGES_POP_SERVER'])
imap.login(config['OUTAGES_POP_USER'], config['OUTAGES_POP_PASS'])
imap.select('INBOX')
ids = None
if config['DEBUG'] is True:
ids = imap.search(None, '(SUBJECT "")')[1][0].split()
else:
ids = imap.search(None, '(UNSEEN)')[1][0].split()
if len(ids) > 0:
logger.info('Collected ' + str(len(ids)) + 'messages from inbox')
#Fetch messages
for id in ids:
typ, msg_data = imap.fetch(id, '(RFC822)')
for response_part in msg_data:
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1])
data = None
#Choose parser
if (msg.has_key('from')) and (msg.has_key('subject')):
if (config['CHORUS_PLAIN_TEXT_FROM_ADDRS_RE'].search(msg['from'])) and (config['CHORUS_PLAIN_TEXT_RE'].match(msg['subject'])):
logger.debug('Message ' + msg['subject'] + 'passed to ChorusPlainText parser')
data = ChorusPlainText(GetBody(msg))
elif (config['CHORUS_HTML_FAT_FROM_ADDRS_RE'].search(msg['from'])) and (config['CHORUS_HTML_FAT_RE'].match(msg['subject'])):
logger.debug('Message ' + msg['subject'] + 'passed to ChorusHTMLFat parser')
data = ChorusHTMLFat(GetBody(msg))
elif (config['CHORUS_HTML_SKINNY_FROM_ADDRS_RE'].search(msg['from'])) and (config['CHORUS_HTML_SKINNY_RE'].match(msg['subject'])):
logger.debug('Message ' + msg['subject'] + 'passed to ChorusHTMLSkinny parser')
data = ChorusHTMLSkinny(GetBody(msg))
else:
passthrough_list.append(msg)
logger.warning('Could not select parser for message ' + msg['subject'])
else:
logger.error('Unable to select parser due to missing headers')
if data is not None:
try:
DBInsert(data)
except (sqlite3.IntegrityError, UnicodeDecodeError) as e:
logger.warning('Unable to insert data into db from message ' + msg['subject'], exc_info = True)
passthrough_list.append(msg)
Passthrough(passthrough_list)
if __name__ == '__main__':
FromEmails()