时间:2021-05-22
本文实例讲述了Python实现获取nginx服务器ip及流量统计信息功能。分享给大家供大家参考,具体如下:
#!/usr/bin/python#coding=utf8log_file = "/usr/local/nginx/logs/access.log"with open(log_file) as f: contexts = f.readlines()# define ip dict###ip = {} # key为ip信息,value为ip数量(若重复则只增加数量)flow = {} # key为ip信息,value为流量总和sum = 0for line in contexts: # count row size of flow size = line.split()[9] # print ip ip_attr = line.split()[0] # count total size of flow sum = int(size) + sum if ip_attr in ip.keys(): # if ip repeated,如果ip重复就将ip数量加一,而流量继续叠加 # count of ip plus 1 ip[ip_attr] = ip[ip_attr] + 1 # size of flow plus size flow[ip_attr] = flow[ip_attr] + int(size) else: # if ip not repeated # define initial values of count of ip and size of flow ip[ip_attr] = 1 flow[ip_attr] = int(size)print(ip)print(flow)print(sum/1024/1024)现在有nginx的访问日志:
[root@weblogic ~]# cat access.log 192.168.223.1 - - [18/Jul/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 Gecko) Chrome/59.0.3071.115 Safari/537.36" "-"192.168.223.136 "192.168.223.1" - - [17/Jul/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; Trident/7.0; rv:11.0) like Gecko" "192.168.223.1"192.168.223.1 - - [18/Jul/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 Gecko) Chrome/59.0.3071.115 Safari/537.36" "-"192.168.223.136 "192.168.223.1" - - [18/Jul/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 Gecko) Chrome/59.0.3071.115 Safari/537.36" "192.168.223.1"192.168.223.1 - - [18/Jul/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 Gecko) Chrome/59.0.3071.115 Safari/537.36" "-"192.168.223.136 "192.168.223.1" - - [18/Jul/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 Gecko) Chrome/59.0.3071.115 Safari/537.36" "192.168.223.1"192.168.223.1 - - [18/Jul/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 Gecko) Chrome/59.0.3071.115 Safari/537.36" "-"192.168.223.136 "192.168.223.1" - - [18/Jul/2017:10:45:07 +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 Gecko) Chrome/59.0.3071.115 Safari/537.36" "192.168.223.1"192.168.223.1 - - [18/Jul/2017:10:51:25 +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 Gecko) Chrome/59.0.3071.115 Safari/537.36" "-"192.168.223.136 "192.168.223.1" - - [18/Jul/2017:10:51:25 +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 Gecko) Chrome/59.0.3071.115 Safari/537.36" "192.168.223.1"利用python将nginx的ip进行统计
思路:将文件内容一行一行都出来,然后进行字符串strip().split(),得到一个列表,列表的第一个元素就是ip的内容
初始化一个空字典,用key表示ip内容,value表示该ip的个数,如果ip重复了,则将value进行增加:
[root@weblogic ~]# cat nginx.py #!/usr/bin/python#coding=utf8log_file = "/root/access.log"ip = {}with open(log_file) as f: for i in f.readlines(): print i.strip().split()[0] ip_attr = i.strip().split()[0] if ip_attr in ip.keys(): # 如果ip存在于字典中,则将该ip的value也就是个数进行增加 ip[ip_attr] = ip[ip_attr] + 1 else: ip[ip_attr] = 1print ip获取执行结果:
[root@weblogic ~]# python nginx.py 192.168.223.1192.168.223.136192.168.223.1192.168.223.136192.168.223.1192.168.223.136192.168.223.1192.168.223.136192.168.223.1192.168.223.136{'192.168.223.1': 5, '192.168.223.136': 5}更多关于Python相关内容可查看本站专题:《Python日志操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了python实现的分析并统计nginx日志数据功能。分享给大家供大家参考,具体如下:利用python脚本分析nginx日志内容,默认统计ip、访问
本文介绍了通过Nginx反向代理实现IP访问分流的示例代码,分享给大家。具体如下:通过Nginx做反向代理来实现分流,以减轻服务器的负载和压力是比较常见的一种服
在Nginx服务器上进行一些常规设置,来限制其并发数及会话空间等。nginx限制ip并发数,也是说限制同一个ip同时连接服务器的数量1,添加limit_zone
使用Python语句,读取Linux远端服务器上的文件打印到控制台的代码实现:下载包:paramikoimportparamiko#服务器信息,主机名(IP地址
提到分发请求,相信大多数人首先会想到Nginx,Nginx作为一种多功能服务器,不仅提供了反向代理隐藏主机ip的能力,还拥有简单的缓存加速功能。当然Nginx最