阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

使用Python模块分析Nginx日志

143次阅读
没有评论

共计 1489 个字符,预计需要花费 4 分钟才能阅读完成。

问题:分析 Nginx 日志并找出访问最多 10 个 IP 地址的来源以及次数

使用 Python 模块 IP

使用方法以及下载地址:https://pypi.python.org/pypi/17MonIP

相关 python 脚本:

#!/usr/bin/env python
#coding:utf8
#Auth:lad
#date:2016-12-05
#desc:parser the nginx’s log,the head of 10
import sys
reload(sys)
sys.setdefaultencoding(“utf-8”)
 
import IP
import os
 
cnt=10
 
fd = open(“/tmp/IP.txt”,”w”)
ipStr = os.popen(“cat /tmp/access.log|awk ‘{print $6}’|sort | uniq -c | sort -nr | head -10 “)
 
fd.write(ipStr.read())
fd.close()
 
fd1 = open(“/tmp/IP.txt”,”r”)
fd2 = open(“/tmp/nginx_IP.txt”,”w+”)
for i in range(1,cnt+1):
    d = fd1.readline()
    print >> fd2,” 访问次数:%8s 访问 IP:%16s IP 地址来源:%20s”%(d.split()[0],d.split()[1],IP.find(d.split()[1]))
fd1.close()
fd2.close()
print “OK”

具体使用 awk 分割的时候需要考虑自己日志的格式,根据格式切割。

执行结果:

使用 Python 模块分析 Nginx 日志

下面关于 Nginx 的文章您也可能喜欢,不妨参考下:

CentOS 7.2 下编译安装 PHP7.0.10+MySQL5.7.14+Nginx1.10.1  http://www.linuxidc.com/Linux/2016-09/134804.htm

搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3 下 Nginx 性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3 下配置 Nginx 加载 ngx_pagespeed 模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx 安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm

Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

Nginx 的 500,502,504 错误解决方法 http://www.linuxidc.com/Linux/2015-03/115507.htm

CentOS 7 编译安装 Nginx1.10.2 脚本启动失败解决思路 http://www.linuxidc.com/Linux/2017-01/139794.htm

Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-02/140361.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计1489字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中