forked from netfarm/archiver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PyLogAnalyzer.py
executable file
·512 lines (431 loc) · 14.6 KB
/
PyLogAnalyzer.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#!/usr/bin/env python
# -*- Mode: Python; tab-width: 4 -*-
#
# Netfarm Mail Archiver - release 2
#
# Copyright (C) 2007 Gianluigi Tiesi <sherpya@netfarm.it>
# Copyright (C) 2007 NetFarm S.r.l. [http://www.netfarm.it]
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
# ======================================================================
## @file PyLogAnalyzer.py
## Netfarm Mail Archiver [loganalyzer]
## TODO
## - handle db disconnection
## - close fd when damonize
from sys import stdin, stdout
from os import chdir, fork
from traceback import format_exc
from types import StringType
from rfc822 import parseaddr
from mx.DateTime.Parser import DateTimeFromString
from psycopg2 import connect
import re
DBDSN = 'host=localhost dbname=mail user=archiver password=archiver'
re_line = re.compile(r'(\w+\s+\d+\s+\d+:\d+:\d+) (.*?) (.*?)\[(\d*?)\]: (.*)')
re_msg = re.compile(r'(\w+=.*?), ')
re_pstat = re.compile(r'(\w+) \((.*)\)')
defskiplist = [ '127.0.0.1', 'localhost' ]
E_ALWAYS = 0
E_INFO = 1
E_WARN = 2
E_ERR = 3
E_TRACE = 4
global logfd
loglevel = E_TRACE
### Queries
q_postfix_msgid = """
insert into mail_log_in (
message_id,
r_date,
ref
) values (
'%(message_id)s',
'%(date)s',
'%(ref)s'
);"""
q_postfix_update = """
update mail_log_in set
mailfrom = '%(mailfrom)s',
mail_size = %(mail_size)d,
nrcpts = %(nrcpts)d
where id in (select id
from mail_log_in
where ref = '%(ref)s'
order by r_date desc
limit 1);
"""
q_postfix_del = """
delete from mail_log_in
where id
in (select id from mail_log_in where ref = '%(ref)s'
order by id desc limit 1);
"""
q_sendmail_in = """
insert into mail_log_in (
mailfrom,
message_id,
r_date,
mail_size,
nrcpts,
ref
) values (
'%(mailfrom)s',
'%(message_id)s',
'%(date)s',
%(mail_size)d,
%(nrcpts)d,
'%(ref)s'
);
"""
q_mail_id = """
select id from mail_log_in
where ref = '%(ref)s'
order by r_date desc
limit 1;
"""
q_out = """
insert into mail_log_out (
mail_id,
d_date,
dsn,
relay,
delay,
status,
status_desc,
mailto
) values (
%(mail_id)d,
'%(date)s',
'%(dsn)s',
'%(relay)s',
%(delay)d,
'%(status)s',
'%(status_desc)s',
'%(mailto)s'
);
"""
PyLog = None
def log(severity, text):
if severity <= loglevel:
logfd.write(text + '\n')
logfd.flush()
def sqlquote(text):
text = text.replace("\\", "\\\\")
text = text.replace("'", "\\'")
return text
def quotedict(info):
for key in info.keys():
if type(info[key]) == StringType:
info[key] = sqlquote(info[key])
class PyLogAnalyzer:
def __init__(self, filename, skiplist=defskiplist):
self.log = log
self.skiplist = skiplist
try:
self.dbConn = connect(DBDSN)
self.dbCursor = self.dbConn.cursor()
except:
raise Exception, 'Cannot connect to DB'
try:
if filename == '-':
self.fd = stdin
else:
self.fd = open(filename, 'r')
except:
raise Exception, 'Cannot open log file'
def __del__(self):
try:
self.dbCursor.close()
self.dbConn.close()
except:
pass
try:
self.fd.close()
except:
pass
self.log(E_TRACE, 'Exit')
def query(self, query, info, fetch=False):
quotedict(info)
qs = query % info
try:
self.dbCursor.execute(qs)
self.dbConn.commit()
except KeyboardInterrupt:
raise KeyboardInterrupt
except:
log(E_ERR, '-----------\n[DB Query Error]')
log(E_ERR, format_exc().strip())
log(E_ERR, '\n[Query]\n' + qs)
log(E_ERR, '-----------')
try:
self.dbConn.rollback()
except:
pass
return False
if fetch:
return self.dbCursor.fetchone()
else:
return True
def mainLoop(self):
self.log(E_ALWAYS, '[PyLogAnalyzer] Starting...')
while 1:
try:
## Read a line
line = self.fd.readline().strip()
## No data, exit
if not len(line):
log(E_ALWAYS, 'No more data, exiting')
break
## Parse the line
res = re_line.match(line)
## No match, continue
if res is None:
log(E_TRACE, 'No match on ' + line)
continue
## split fields
datestr, host, name, pid, line = res.groups()
line = line.split(': ', 1)
if len(line) != 2: continue
ref, msg = line
## Parse date
try:
date = DateTimeFromString(datestr).Format()
except:
log(E_ERR, 'Error parsing date, ' + datestr)
continue
## Get process/subprocess
proc = name.split('/', 1)
if len(proc) == 1:
## Single name
process = subprocess = proc[0]
elif len(proc) == 2:
## process/subprocess
process, subprocess = proc
else:
## How many slashes?
log(E_ERR, 'Too many slashes in process name ' + name)
continue
## Pick the needed parse method
hname = '_'.join([process, subprocess]).replace('-', '_')
handler = getattr(self, hname, None)
if handler is None: continue
## Map fields
info = dict(date=date,
host=host,
pid=pid,
ref=ref,
msg=msg,
process=process,
subprocess=subprocess)
## Parse/split all values
data = { 'dsn': '0.0.0' } # postfix and some lines on sendmail
parts = re_msg.split(msg)
if len(parts) == 0: continue
for part in parts:
part = part.strip()
if part.find('=') == -1: continue
key, value = part.split('=', 1)
data[key] = value
info.update(data)
if not handler(info.copy()):
log(E_ERR, 'Query Failed - Force Respawn (syslog-ng)')
break
except (KeyboardInterrupt, IOError):
break
except:
log(E_ERR, '-----------\n[Runtime Error]')
log(E_ERR, format_exc().strip())
log(E_ERR, '-----------')
## Gather message_id and put the entry in the database
def postfix_cleanup(self, info):
""" Collects message_id and inserts the record with the placeholder """
if info['msg'].startswith('reject:'): return True
if info.has_key('resent-message-id'): return True
if not info.has_key('message-id'):
self.log(E_ERR, 'postfix/cleanup got no message_id %s: %s' % (info['ref'], info['msg']))
return True
info['message_id'] = info['message-id']
return self.query(q_postfix_msgid, info)
## qmgr log have from or queue deletions
def postfix_qmgr(self, info):
""" Collects from, size and nrcpt and updates record of postfix/cleanup """
if not info.has_key('from'): return True # removed
try:
info['mail_size'] = long(info['size'])
except:
info['mail_size'] = 0
try:
info['nrcpts'] = long(info['nrcpt'].split(' ', 1)[0])
except:
info['nrcpts'] = 0
## Parse mailfrom using rfc822 module
try:
info['mailfrom'] = parseaddr(info['from'])[1]
except:
log(E_ERR, 'Error while parsing mailfrom address, ' + info['from'])
return True
return self.query(q_postfix_update, info)
def postfix_smtp(self, info):
""" Picks mail_id from the record of postfix/cleanup and fills mail_log_out entry """
if not info.has_key('to'): return True # no need
## Skip 'connect to' - FIXME find a better way postfix in etch 11, sarge 9, lenny 10 ??
if len(info['ref']) not in (9, 10, 11): return True
## Retrieve ref's mail_id
mail_id = self.query(q_mail_id, info, fetch=True)
if mail_id is None: return True
info['mail_id'] = mail_id[0]
if info.has_key('relay'):
info['relay'] = info['relay'].split('[')[0]
else:
info['relay'] = 'none'
if info['relay'] in self.skiplist:
return self.query(q_postfix_del, info)
try:
info['delay'] = long(info['delay'])
except:
info['delay'] = 0
## Parse mailto using rfc822 module
try:
info['mailto'] = parseaddr(info['to'])[1] # FIXME multiple recipients as in sendmail?
except:
log(E_ERR, 'Error while parsing mailto address, ' + info['to'])
return True
## Parse status
res = re_pstat.match(info['status'])
if res:
info['status'], info['status_desc'] = res.groups()
else:
info['status'], info['status_desc'] = 'unknown', info['status']
return self.query(q_out, info)
postfix_lmtp = postfix_smtp
def sendmail_sendmail(self, info):
""" Collects two stages of sendmail log """
if info.has_key('from') and info.has_key('msgid'):
## Collects from, message_id and nrctps
if info['from'] == '<>': return True # no return path
## Parse mailfrom using rfc822 module
try:
info['mailfrom'] = parseaddr(info['from'])[1]
except:
log(E_ERR, 'Error while parsing mailfrom address, ' + info['from'])
return True
try:
info['mail_size'] = long(info['size'])
except:
info['mail_size'] = 0
try:
info['nrcpts'] = long(info['nrcpts'])
except:
info['nrcpts'] = 0
## Now message_id as required dict key
info['message_id'] = info['msgid']
return self.query(q_sendmail_in, info)
elif info.has_key('to'):
## Collects to, delay, relay, dns, status and status_desc
## First check if we have a corresponding mail_id
mail_id = self.query(q_mail_id, info, fetch=True)
if mail_id is None: return True
info['mail_id'] = mail_id[0]
## Sendmail messages are not easy to parse
status = info['stat']
if status.startswith('Sent '):
status, statusdesc = 'sent', status.split('Sent ', 1).pop()
elif status.startswith('Deferred: '):
status, statusdesc = 'deferred', status.split('Deferred: ', 1).pop()
else:
status, statusdesc = 'unknown', status
info['status'] = status
## remove ( and )
if (statusdesc[0] == '(') and (statusdesc[-1] == ')'):
statusdesc = statusdesc[1:-1]
info['status_desc'] = statusdesc
if info.has_key('relay'):
info['relay'] = info['relay'].split(' ', 1)[0]
else:
info['relay'] = 'none'
## Conformance with postfix
if info['relay'][-1] == '.': info['relay'] = info['relay'][:-1]
if info['relay'] in self.skiplist: return True
recipients = info['to'].split(',')
info['recipients'] = []
for recipient in recipients:
try:
info['recipients'].append(parseaddr(recipient)[1])
except:
log(E_ERR, 'Error while parsing mailto address, ' + info['to'])
if len(info['recipients']) == 0:
log(E_ERR, 'No recipients collected - skipping')
return True
## Parse delay
try:
hms = info['delay'].split(':')
seconds = int(hms[2])
seconds = seconds + (int(hms[1]) * 60)
if hms[0].find('+') == -1:
d = 0
h = int(hms[0])
else:
d, h = hms[0].split('+')
d = int(d)
h = int(h)
seconds = seconds + (h * 60 * 60)
seconds = seconds + (d * 24 * 60 * 60)
info['delay'] = long(seconds)
except:
info['delay'] = 0
res = True
for recipient in info['recipients']:
info['mailto'] = recipient
res = res and self.query(q_out, info)
return res
else:
return True # ignored
sm_mta_sm_mta = sendmail_sendmail
def sigtermHandler(signum, frame):
log(E_ALWAYS, 'SiGTERM received')
if __name__ == '__main__':
global logfd
from sys import argv, exit as sys_exit
from signal import signal, SIGTERM
if '-l' in argv:
skiplist = []
argv.remove('-l')
else:
skiplist = defskiplist
if '-d' in argv:
daemonize = True
logfd = open('/var/log/pylog.log', 'a')
argv.remove('-d')
else:
logfd = stdout
daemonize = False
if '-ng' in argv:
logfd = open('/var/log/pylog.log', 'a')
argv.remove('-ng')
if len(argv) != 2:
print 'Usage %s [-d] [-ng] [-l] logfile|fifo' % argv[0]
sys_exit()
if daemonize and (argv[0] == '-'):
print '%s incompatible options -d with stdin'
sys_exit()
signal(SIGTERM, sigtermHandler)
if daemonize:
pid = fork()
if pid: sys_exit(0)
chdir('/')
try:
PyLog = PyLogAnalyzer(argv[1], skiplist)
PyLog.mainLoop()
except:
log(E_ERR, '-----------\n[Startup Error]')
log(E_ERR, format_exc().strip())
log(E_ERR, '-----------')