python分析nginx日誌

來源:互聯網
上載者:User

標籤:python

問題:分析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 sysreload(sys)sys.setdefaultencoding( "utf-8" )import IPimport oscnt=10fd = 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分割的時候需要考慮自己日誌的格式,根據格式切割。

本文出自 “share,open source” 部落格,請務必保留此出處http://liqilong2010.blog.51cto.com/3029053/1882398

python分析nginx日誌

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.