Python writes a squid access log analysis applet

Source: Internet
Author: User
Tags ip number
In this two-week group, we wanted to learn python, so we created an environment and atmosphere for everyone to learn.

Yesterday in the group, posted a demand, is to count squid access log IP access number and URL of the number of visits and sorting, many students have generally achieved the corresponding functions, I put my simple implementation of the post, welcome to shoot Bricks:

The log format is as follows:

Copy the Code code as follows:


%ts.%0 3tu%6tr%{x-forwarded-for}>h%ss/%03hs% H "%{cookie}>h

Copy the Code code as follows:


1372776321.285 0 100.64.19.225 tcp_hit/200 8560 GET http://img1.jb51.net/games/0908/19/1549401_3_80x100.jpg-none/- Image/jpeg "http://www.jb51.net/" "mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; trident/4.0; Qqdownload 734;. net4.0c;. NET CLR 2.0.50727) "pcsuv=0;%20pcuvdata=lastaccesstime=1372776317582;%20u4ad=33480hn;%20c=14arynt;%20uf= 1372776310453

Copy the Code code as follows:


#!/usr/bin/python
#-*-Coding:utf-8-*-
Import Sys
From Optparse import Optionparser
'''
Just a test of log files, statistics Office access.log IP number
'''
Try
f = open ('/data/proclog/log/squid/access.log ')
Except Ioerror,e:
Print "Can ' t open the file:%s"% (e)

def log_report (field):
'''
Return the field of the access log
'''
if field = = "IP":
return [Line.split () [2] for line in F]
if field = = "url":
return [Line.split () [6] for line in F]
def log_count (field):
'''
Return a dict of like {field:number}
'''
Fields2 = {}
Fields = log_report (field)
For field_tmp in fields:
If field_tmp in Fields2:
FIELDS2[FIELD_TMP] + = 1
Else
FIELDS2[FIELD_TMP] = 1
Return FIELDS2
def log_sort (Field,number = ten, reverse = True):
'''
Print the sorted fields to output
'''
For V in Sorted (log_count (field). Iteritems (), key = Lambda x:x[1], reverse = reverse) [0:int (number)]:
Print V[1],v[0]
if __name__ = = "__main__":
Parser =optionparser (usage= "%prog [-i|-u] [-N num |-r]", Version = "1.0")
Parser.add_option ('-n ', '--number ', dest= "number", type=int,default=10,help= "print top line of the ouput")
Parser.add_option ('-I ', '--ip ', dest= "IP", action = "Store_true", help= "Print IP information of access log")
Parser.add_option ('-u ', '--url ', dest= "url", action = "Store_true", help= "Print URL information of access log")
Parser.add_option ('-R ', '--reverse ', action = "Store_true", dest= "reverse", help= "reverse output")
(Options,args) = Parser.parse_args ()

If Len (SYS.ARGV) < 2:
Parser.print_help ()
If Options.ip and Options.url:
Parser.error ('-I and-u can not is execute at the same time ')
If Options.ip:
Log_sort ("IP", Options.number, True and Options.reverse or False)
If Options.url:
Log_sort ("url", Options.number, True and Options.reverse or False)

F.close ()

The effect is as follows:

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.