介绍
这篇文章将为大家详细讲解有关Python如何实现获取nginx服务器ip及流量统计信息功能,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
具体如下:
# !/usr/bin/Python #=utf8编码 时间=log_file “/usr/地方/nginx/日志/access.log" with 开放(log_file), as f: 时间=contexts 才能;f.readlines () #,define ip  dict # # # ip =,{},,, #,关键为ip信息,值为ip数量(若重复则只增加数量) flow =,{},, #,关键为ip信息,值为流量总和 sum =0 for line 上下文拷贝: #,才能count row size of 流 时间=size 才能;line.split () [9] #,才能print ip 时间=ip_attr 才能;line.split () [0] #,才能count total size of 流 sum 才能=,int(大小),+,和 if 才能;ip_attr 拷贝ip.keys ():,, #, if ip 重复,如果ip重复就将ip数量加一,而流量继续叠加 #,才能count of ip plus 1 ,,,的ip (ip_attr),=, ip (ip_attr), + 1 #,才能size of flow plus 大小 ,,,流[ip_attr],=,流(ip_attr), +, int(大小) 其他的才能: #,才能if ip not 重复 #,才能define initial values of count of ip 以及size of 流 ,,,的ip (ip_attr),=, 1 ,,,流[ip_attr],=, int(大小) 打印(ip) print(流) 打印(金额/1024/1024)
现在有nginx的访问日志:
[root@weblogic ~] #, cat access.log 192.168.223.1 安康安康;[18/7月/2017:10:21:25 + 0800],“GET /favicon.ico HTTP/1.1“, 192.168.223.136:8080 404年,24,“http://192.168.223.136:8080 proxy_path/index.html",“Mozilla/5.0, (Windows NT 10.0;, Win64;, x64), AppleWebKit/537.36, (KHTML, like 壁虎),Chrome/59.0.3071.115 Safari/537.36“,“产生绯闻; 192.168.223.136 “192.168.223.1",安康;作用;[17日/7月/2017:17:06:44 + 0800],“GET /index.html HTTP/1.0“,“192.168.223.136", 304, 0,“产生绯闻;,“Mozilla/5.0, (Windows NT 10.0;, WOW64;,三叉戟/7.0;,房车:11.0),like Gecko",“192.168.223.1" 192.168.223.1 安康安康;[18/7月/2017:10:30:12 + 0800],“GET /proxy_path/index.html HTTP/1.1“, 192.168.223.136:8080 304年,0,“产生绯闻;,“Mozilla/5.0, (Windows NT 10.0;, Win64;, x64), AppleWebKit/537.36, (KHTML, like 壁虎),Chrome/59.0.3071.115 Safari/537.36“,“产生绯闻; 192.168.223.136 “192.168.223.1",安康;作用;[18/7月/2017:10:30:12 + 0800],“GET /index.html HTTP/1.0“,“192.168.223.137", 304, 0,“产生绯闻;,“Mozilla/5.0, (Windows NT 10.0;, Win64;, x64), AppleWebKit/537.36, (KHTML, like 壁虎),Chrome/59.0.3071.115 Safari/537.36“,“192.168.223.1" 192.168.223.1 安康安康;[18/7月/2017:10:38:38 + 0800],“GET /proxy_path/index.html HTTP/1.1“, 192.168.223.136:8080 304年,0,“产生绯闻;,“Mozilla/5.0, (Windows NT 10.0;, Win64;, x64), AppleWebKit/537.36, (KHTML, like 壁虎),Chrome/59.0.3071.115 Safari/537.36“,“产生绯闻; 192.168.223.136 “192.168.223.1",安康;作用;[18/7月/2017:10:38:38 + 0800],“GET /index.html HTTP/1.0“,“192.168.223.136:80", 304, 0,“产生绯闻;,“Mozilla/5.0, (Windows NT 10.0;, Win64;, x64), AppleWebKit/537.36, (KHTML, like 壁虎),Chrome/59.0.3071.115 Safari/537.36“,“192.168.223.1" 192.168.223.1 安康安康;[18/7月/2017:10:45:07 + 0800],“GET /proxy_path/index.html HTTP/1.1“, 192.168.223.136:8080 304年,0,“产生绯闻;,“Mozilla/5.0, (Windows NT 10.0;, Win64;, x64), AppleWebKit/537.36, (KHTML, like 壁虎),Chrome/59.0.3071.115 Safari/537.36“,“产生绯闻; null null null null null null null null null null null null null null null null null null null null null null null null null null nullPython如何实现获取nginx服务器ip及流量统计信息功能