firs version trafic mail
This commit is contained in:
parent
3063e78dd9
commit
ae88cc4911
|
@ -494,7 +494,7 @@ class PostfixMailscannerTraffic(ServiceMonitor):
|
||||||
model = 'mailboxes.Mailbox'
|
model = 'mailboxes.Mailbox'
|
||||||
resource = ServiceMonitor.TRAFFIC
|
resource = ServiceMonitor.TRAFFIC
|
||||||
verbose_name = _("Postfix-Mailscanner traffic")
|
verbose_name = _("Postfix-Mailscanner traffic")
|
||||||
script_executable = '/usr/bin/python'
|
script_executable = '/usr/bin/python3'
|
||||||
monthly_sum_old_values = True
|
monthly_sum_old_values = True
|
||||||
doc_settings = (settings,
|
doc_settings = (settings,
|
||||||
('MAILBOXES_MAIL_LOG_PATH',)
|
('MAILBOXES_MAIL_LOG_PATH',)
|
||||||
|
@ -540,90 +540,68 @@ class PostfixMailscannerTraffic(ServiceMonitor):
|
||||||
return ini_date < int(date) < end_date
|
return ini_date < int(date) < end_date
|
||||||
|
|
||||||
users = {{}}
|
users = {{}}
|
||||||
delivers = {{}}
|
sends = {{}}
|
||||||
reverse = {{}}
|
|
||||||
|
|
||||||
def prepare(object_id, mailbox, ini_date):
|
def prepare(object_id, mailbox, ini_date):
|
||||||
global users
|
global users
|
||||||
global delivers
|
global sends
|
||||||
global reverse
|
|
||||||
ini_date = to_local_timezone(ini_date)
|
ini_date = to_local_timezone(ini_date)
|
||||||
ini_date = int(ini_date.strftime('%Y%m%d%H%M%S'))
|
ini_date = int(ini_date.strftime('%Y%m%d%H%M%S'))
|
||||||
users[mailbox] = (ini_date, object_id)
|
users[mailbox] = (ini_date, object_id)
|
||||||
delivers[mailbox] = set()
|
sends[mailbox] = {{}}
|
||||||
reverse[mailbox] = set()
|
|
||||||
|
def monitor(users, sends, maillogs):
|
||||||
|
grupos = []
|
||||||
|
sasl_username_pattern = re.compile(r'sasl_username=([a-zA-Z0-9\.\-_]+)')
|
||||||
|
size_pattern = re.compile(r'size=(\d+),')
|
||||||
|
|
||||||
def monitor(users, delivers, reverse, maillogs):
|
|
||||||
targets = {{}}
|
|
||||||
counter = {{}}
|
|
||||||
user_regex = re.compile(r'\(Authenticated sender: ([^ ]+)\)')
|
|
||||||
for maillog in maillogs:
|
for maillog in maillogs:
|
||||||
try:
|
try:
|
||||||
with open(maillog, 'r') as maillog:
|
with open(maillog, 'r') as maillog:
|
||||||
for line in maillog.readlines():
|
for line in maillog.readlines():
|
||||||
# Only search for Authenticated sendings
|
# Only search for Authenticated sendings
|
||||||
if '(Authenticated sender: ' in line:
|
if 'sasl_username=' in line:
|
||||||
username = user_regex.search(line).groups()[0]
|
# si el usuario es uno de los elegidos y el rango de tiempo es correcto
|
||||||
try:
|
# recoge el id de grupo
|
||||||
sender = users[username]
|
match = sasl_username_pattern.search(line)
|
||||||
except KeyError:
|
if not match:
|
||||||
continue
|
continue
|
||||||
else:
|
|
||||||
month, day, time, __, proc, id = line.split()[:6]
|
username = match.groups(1)[0]
|
||||||
if inside_period(month, day, time, sender[0]):
|
if username not in users.keys():
|
||||||
# Add new email
|
continue
|
||||||
delivers[id[:-1]] = username
|
|
||||||
# Look for a MailScanner requeue ID
|
month, day, time, __, __, req_id = line.split()[:6]
|
||||||
elif ' Requeue: ' in line:
|
if inside_period(month, day, time, users[username][0]):
|
||||||
id, __, req_id = line.split()[6:9]
|
group = req_id[:-1]
|
||||||
id = id.split('.')[0]
|
sends[username][group] = 0
|
||||||
try:
|
grupos.append(group)
|
||||||
username = delivers[id]
|
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
targets[req_id] = (username, 0)
|
|
||||||
reverse[username].add(req_id)
|
|
||||||
# Look for the mail size and count the number of recipients of each email
|
|
||||||
else:
|
else:
|
||||||
try:
|
# busca el size de envios donde se alla anadido el groupID anteriormente,
|
||||||
month, day, time, __, proc, req_id, __, msize = line.split()[:8]
|
# una vez encontrado borra el groupID
|
||||||
except ValueError:
|
for id in grupos:
|
||||||
# not interested in this line
|
if id in line:
|
||||||
continue
|
match = size_pattern.search(line)
|
||||||
if proc.startswith('postfix/'):
|
if not match:
|
||||||
req_id = req_id[:-1]
|
continue
|
||||||
if msize.startswith('size='):
|
for k, v in sends.items():
|
||||||
try:
|
if id in sends[k].keys():
|
||||||
target = targets[req_id]
|
sends[k][id] += int(match.groups(1)[0])
|
||||||
except KeyError:
|
grupos.remove(id)
|
||||||
pass
|
|
||||||
else:
|
|
||||||
targets[req_id] = (target[0], int(msize[5:-1]))
|
|
||||||
elif proc.startswith('postfix/smtp'):
|
|
||||||
try:
|
|
||||||
target = targets[req_id]
|
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
if inside_period(month, day, time, users[target[0]][0]):
|
|
||||||
try:
|
|
||||||
counter[req_id] += 1
|
|
||||||
except KeyError:
|
|
||||||
counter[req_id] = 1
|
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
sys.stderr.write(str(e)+'\\n')
|
sys.stderr.write(str(e)+'\\n')
|
||||||
|
|
||||||
for username, opts in users.iteritems():
|
# devolver la sumatoria de valores a orchestra (id_user, size)
|
||||||
size = 0
|
for username, opts in users.items():
|
||||||
for req_id in reverse[username]:
|
total_size = 0
|
||||||
size += targets[req_id][1] * counter.get(req_id, 0)
|
for size in sends[username].values():
|
||||||
print opts[1], size
|
total_size += size
|
||||||
|
print(f"{{opts[1]}} {{total_size}}")
|
||||||
""").format(**context)
|
""").format(**context)
|
||||||
)
|
)
|
||||||
|
|
||||||
def commit(self):
|
def commit(self):
|
||||||
self.append('monitor(users, delivers, reverse, maillogs)')
|
self.append('monitor(users, sends, maillogs)')
|
||||||
|
|
||||||
def monitor(self, mailbox):
|
def monitor(self, mailbox):
|
||||||
context = self.get_context(mailbox)
|
context = self.get_context(mailbox)
|
||||||
|
|
Loading…
Reference in New Issue