我的一位客户正在使用大量带宽,这似乎与 IMAP 有关,而与他们的实际网站无关。
有一个日志文件 /var/log/maillog,它记录每个 IMAP 连接以及发送/读取到客户端的字节数:
我正在尝试编写一个小型 shell 脚本来计算每个用户的带宽,以便我们可以找出它是哪个电子邮件帐户。
Oct 1 10:43:00 web1 dovecot: IMAP([email protected]): Disconnected: Logged out bytes=363/4407
Oct 1 10:43:00 web1 dovecot: IMAP([email protected]): Disconnected: Logged out bytes=63/492
Oct 1 10:43:00 web1 dovecot: IMAP([email protected]): Disconnected: Logged out bytes=386/15378
Oct 1 10:43:00 web1 dovecot: IMAP([email protected]): Disconnected: Logged out bytes=63/509
Oct 1 10:43:20 web1 dovecot: IMAP([email protected]): Disconnected: Logged out bytes=345/3044
Oct 1 10:43:20 web1 dovecot: IMAP([email protected]): Disconnected: Logged out bytes=63/492
Oct 1 10:43:20 web1 dovecot: IMAP([email protected]): Disconnected: Logged out bytes=784/40163
Oct 1 10:43:20 web1 dovecot: IMAP([email protected]): Disconnected: Logged out bytes=63/622
日志格式为:
# IMAP logout format string:
# %i - total number of bytes read from client
# %o - total number of bytes sent to client
#imap_logout_format = bytes=%i/%o
因此它应该只添加最后两个字段并包含电子邮件地址:
我找到了以下脚本,但我没有使用 directadmin,但应该能够想出类似的东西,然而我无法让它正常工作。
http://forum.directadmin.com/showthread.php?t=36588
有人有主意吗?
提前致谢!
答案1
这个脚本可能有用。嗯,使用您提供的示例输出对我来说是有效的。
#!/usr/bin/python
import os
import sys
import re
logline = re.compile(".+?dovecot: IMAP\((.+?)\): Disconnected: Logged out bytes=(\d+)/(\d+)")
matched = 0
totallines = 0
results = {}
if __name__ == "__main__":
if len(sys.argv) < 2:
print "Need logfile to continue"
logfile = open(sys.argv[1])
for line in logfile.readlines():
totallines += 1
log = logline.match(line)
if log:
matched += 1
user, inb, outb = log.group(1), int(log.group(2)), int(log.group(3))
if user in results:
results[user][0] += inb
results[user][1] += outb
else:
results[user] = [inb, outb]
else:
continue
print "Matched {0} of {1} lines".format(matched, totallines)
for k in results.keys():
r = results[k]
print "{0}: {1}/{2} Total: {3}".format(k, r[0], r[1], r[0]+r[1])