我要计算通过域访问的带宽,需要知道有多少次域被击中。我能够计算带宽,但不知道如何计算在日志中出现的域。任何想法都会很有帮助。非常感谢你的帮助。你知道吗
代码:
import os
import re
from collections import defaultdict
import string
merged_logs = []
line=[]
dict = defaultdict(int)
bandwidth = 0
path = ["/var/logs/"]
for i in path:
for filename in os.listdir(i):
with open(os.path.join(i, filename), 'r') as filedata:
merged_logs += filedata.readlines()
for line in merged_logs:
line_split = line.split(" ")
start = "CONNECT "
end = " -"
domain_str = line[line.find(start)+len(start):line.find(end)]
if domain_str.find("/")>0:
domain_split = domain_str.split("/")
domain = domain_split[0]
if len(line_split)==10:
bandwidth = line_split[3]
if len(line_split)==11:
bandwidth = line_split[4]
else:
domain = domain_str
if len(line_split)==10:
bandwidth = line_split[3]
if len(line_split)==11:
bandwidth = line_split[4]
if domain not in dict:
dict[domain] = int(bandwidth)
else:
dict[domain] += int(bandwidth)
for key, value in dict.items():
print key, (value * 2 )/(1024 * 1024)
/var/logs下的示例日志文件包含以下行:
1569935790.563 1010 192.168.10.3 TCP_TUNNEL/200 1001803 CONNECT www.google.com:443 - HIER_DIRECT/www.google.com - 192.168.100.3
1569935790.563 1010 192.168.10.3 TCP_TUNNEL/200 1001085 CONNECT www.google.com:443 - HIER_DIRECT/www.google.com - 192.168.100.3
1569935790.563 1010 192.168.10.3 TCP_TUNNEL/200 1000182 CONNECT www.google.com:443 - HIER_DIRECT/www.google.com - 192.168.100.3
1569935790.563 1010 192.168.10.3 TCP_TUNNEL/200 1006183 CONNECT www.xyz.com/index.php - HIER_DIRECT/www.xyz.com - 192.168.100.3
1569935790.563 1010 192.168.10.3 TCP_TUNNEL/200 1091083 CONNECT www.xyz.com/index.php - HIER_DIRECT/www.xyz.com - 192.168.100.3
1569935790.563 1010 192.168.10.3 TCP_TUNNEL/200 2091803 CONNECT www.xyz.com/index.php - HIER_DIRECT/www.xyz.com - 192.168.100.3
1569935790.563 1010 192.168.10.3 TCP_TUNNEL/200 2091083 CONNECT www.xyz.com/index.php - HIER_DIRECT/www.xyz.com - 192.168.100.3
59375 192.168.10.3 TAG_NONE/503 10 CONNECT www.google.com - HIER_NONE/- - 192.168.100.3
输出格式应为:
Domain Bandwidth (MB) Hit (Count)
www.xyz.com 11 4
www.google.com 5 3
我在示例日志上运行了上述代码,以获得以下输出:
相关问题 更多 >
编程相关推荐