分析ip流量的python脚本
日志文件格式如下
172.16.225.66 10.71.2.162 1612409 172.16.15.252 10.71.11.31 1210446 172.16.15.252 10.71.11.59 1197976 172.16.15.252 10.71.11.30 1166167 172.16.15.252 10.71.11.66 1149429 172.16.15.252 10.71.11.65 1124686 172.16.15.252 10.71.11.46 1110412
使用anychart图标插件
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>AnyChart Sample</title> <script type="text/javascript" language="javascript" src="./AnyChart.js"></script> </head> <body> <script type="text/javascript" language="javascript"> //<![CDATA[ var chart = new AnyChart('./AnyChart.swf'); chart.width = 800; chart.height = 500; chart.setXMLFile('./buildxml/'); chart.write(); //]]> </script> </body> </html>
数据由这个地址提供
chart.setXMLFile('./buildxml/');
这个地址由Django生成 (写的很糙,新手)
#!/usr/bin/python # -*- coding:utf-8 -*- import glob import datetime from django.http import HttpResponse #build dynamic data def build_xml(request): file_list = glob.glob('/vm_share/Bps*') file_list.sort() result = {} for id,fn in enumerate(file_list): for line in open(fn): src_ip, dest_ip, flow = line.split() result.setdefault(src_ip, [0] * len(file_list))[id] += int(flow) / 1000 result.setdefault(dest_ip, [0] * len(file_list))[id] += int(flow) / 1000 del_list = [] for key in result: if max(result[key]) <= 5000: del_list.append(key) for key in del_list: del result[key] #build xml format xml_start = r''' <?xml version="1.0" encoding="UTF-8"?> <anychart> <settings> <animation enabled="True" /> </settings> <charts> <chart> <data_plot_settings default_series_type="Line"> <line_series> <tooltip_settings enabled="true"> <format> IP:{%SeriesName} 流量:{%Value}{numDecimals:0} </format> </tooltip_settings> </line_series> </data_plot_settings> <data> ''' xml_end = r''' </data> <chart_settings> <title> <text>IP流量分析</text> </title> <axes> <y_axis> <title> <text>KB/s</text> </title> </y_axis> <x_axis> <labels align="Outside" /> <title> <text>时间</text> </title> </x_axis> </axes> </chart_settings> </chart> </charts> </anychart> ''' start_time = datetime.datetime(2012,07,26,20,04,00) time_interval = datetime.timedelta(minutes=5) time_axis = [] for i in range(len(file_list)): time_axis.append(start_time.strftime('%H:%M')) start_time += time_interval xml = '' for ip in result: i = 0 xml_data = '<series name="%s" >' %ip xml += xml_data for point in result[ip]: xml_data = '<point name="%s" y="%s" />' %(time_axis[i], point) xml += xml_data i += 1 xml_data = '</series>' xml += xml_data return HttpResponse(xml_start + xml + xml_end)
这是展示