-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.py
157 lines (82 loc) · 2.77 KB
/
demo.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
import sys
sys.path.append('.')
import django
from django.core.management import setup_environ
import settings
setup_environ(settings)
from dalas.maillog.models import *
from parser import DalasParser
import time
class EventManager():
"""
Just small class to manage 4 typical Postfix Actions, and them track
"""
def __init__(self):
self.QueueList = { }
self.QueueRawData = { }
self.Queuestatus = { }
self.actor_cmd = None
self.cmd = {'nqmgr':self.Nqmgr,'qmgr':self.Qmgr,'smtp':self.Smtp,'cleanup':self.CleanUp,'pickup':self.PickUp}
def HandleEvent(self,data):
queue = Queue.objects.filter(ident=data['unique'])
if not queue:
queue = Queue.objects.create(ident=data['unique'])
queue.save()
else:
queue = queue[0]
event = Event.objects.create()
event.label = data['label']
event.raw_month = data['month']
event.raw_day = data['day']
event.raw_time = data['time']
event.raw_hostname =data['hostname']
event.raw_actor = data['process']
event.raw_actor_cmd = data['childprocess']
event.raw_pid = data['pid']
event.raw_line = str(data)
event.queue.add(queue)
event.save()
#Check if has actor... and execute...
if data and self.cmd.has_key(data['childprocess']) :
self.cmd[data['childprocess']](data,queue)
else:
#FIX-ME: Create handle to exceptions:
#need regex to cover
return False
def Qmgr(self,data,queue):
print "QMGR", data
def Nqmgr(self,data,queue):
print data
def Smtp(self,data,queue):
# FIX-ME:
recipients = Recipient.objects.create()
recipients.label ='send to: %s' % data['output']['to']
recipients.email_to =data['output']['to']
recipients.orig_to =data['output']['orig_to']
recipients.relay_info = data['output']['relay']
recipients.delays = data['output']['delays']
recipients.dsn = data['output']['dsn']
recipients.status = data['output']['status']
recipients.queue = queue
recipients.save()
def CleanUp(self,data,queue):
#Create Unique MSG ID
msg = Msg.objects.filter(msgid__msgid=data['unique'])
if not msg:
print data
msg = Msg.objects.create()
msg.save()
else:
msg = msg[0]
print msg
#'email_to','orig_to','delays'
#{'queueId': '2244E61201', 'hostname': 'debian-base', 'childprocess': 'cleanup', 'time': '16:00:08', 'process': 'postfix', 'output': {'message-id': '[email protected]'}, 'unique': '2244E61201', 'pid': '21339', 'day': '22', 'month': 'Jul'}
def PickUp(self,data,queue):
print data
a = DalasParser('/tmp/simple.log')
#fix-ME
#if msglist > 1000, refuse input...
manager = EventManager()
for l in a.parse():
manager.HandleEvent(l)
print manager.QueueList