Python Real-time analytics user Login

Source: Internet
Author: User
Tags dateformat

Via Nginx Log

#!/usr/local/bin/python3#Coding:utf-8# ====================================================#Author:chang-email:[email protected]#Last modified:2017-5-8#Filename:nginxanalysis.py#description:real Time Analysis Nginx log,base time, OS, thread#Blog:http://www.cnblogs.com/changbo# ===================================================="""requirements: Read Nginx log files every 1 minutes Notice: Init script parameters during simulation log cutting CP Access.log Access2017xxxx.log && echo > Access.log && Amp echo ' 0 ' > Offset.txt"""Import TimeImportOSImportReImportPymysql fromThreadingImportThread#From django.db Import connectionKeypage= ['/sys/get_user.do','/hmcsapiv1.0.7/user/login']engdate= {'Jan':'1','Feb':'2','Mar':'3','APR':'4',' May':'5','June':'6','Jul':'7',' the':'8','Sept':'9',           'Oct':'Ten','Nov':' One','Dec':' A'}defDateFormat (nginxdate): Day= (Nginxdate.split ('[') [1]). Split ('/') [0] Month= engdate[(Nginxdate.split ('[') [1]). Split ('/') [1]] year= (Nginxdate.split ('[') [1]). Split ('/') [2]    returnYear +'-'+ Month +'-'+ Day#Write log offsetdefwriteoffset (number): With open ('Offset.txt','w+') as F3:f3.write (number) F3.flush ()#Get log offsetdefGetOffset (): With open ('Offset.txt') as F2:offset=F2.readline ()returnOffsetdb= Pymysql.connect ("xxxxxx","xxxx","xxxx","xxxx") Cursor=db.cursor ()#cleantable = ' TRUNCATE abnormal 'Listtime=[]listuser= []defanalysisdb (): whileTrue:time.sleep (60)        Try: Sql3='SELECT user,time from Userlogin'Cursor.execute (SQL3) Results=Cursor.fetchall () forRowinchresults:listuser.append (row[0]) listtime.append (row[1])            #count the number of users logged in within 1 minutesSQL1 ="SELECT Count (*) from Userlogin where time= '%s ' and user= '%s '"%(Listtime[0], listuser[0])#Delete the record if the condition is not metSQL2 ="DELETE from Userlogin where time= '%s ' and user= '%s '"%(Listtime[0], listuser[0])Print(Listtime[0], listuser[0]) cursor.execute (SQL3) cursor.execute (SQL1) Datad=Cursor.fetchone ()Print(Datad)ifDatad[0] < 3: Cursor.execute (SQL2) db.commit ()Print('-----Delete Success-------')            delListtime[0]delListuser[0]exceptException as E:time.sleep (60)            Print(e)defHandleline (logline): Susptmp= Logline.split (" ")    ifLen (susptmp) > 2:        ifSUSPTMP[6] = =Keypage[0]:delSusptmp[1:3]            delSusptmp[7:]            delSusptmp[2:6]            ifLen (susptmp) > 2: IP=Susptmp[0] time1= ((Susptmp[1].split (':', 1)) [1]) [0:5] Date= DateFormat ((Susptmp[1].split (':', 1) ) [0] _, _, User, _, PASSD, _= Re.split (r'[&= "]', susptmp[2])                #Print (IP + '---', time1 + '---', date + '---', user + '---', PASSD + '---')sql ="INSERT into Userlogin (IP, time, user, PASSD, date) VALUES ('%s ', '%s ', '%s ', '%s ', ' %s ')"% (IP, time1, user,'*****', date)Try: Cursor.execute (SQL) Db.commit ()Print('Insert success!')                exceptException as E:Print(e)#Online Analysis LogdefAnalysisLog (): With open ('Access.log') as F1: whileTrue:#Get offsetLastOffset =GetOffset ()#Jump the specify log linef1.seek (int (lastoffset))#Gets the line offsetwhere =F1.tell () Line=f1.readline () writeoffset (str (where))if  notLine:time.sleep (10) F1.seek (where)Else:                #processes the line and gets the offset of the row and writes to the fileHandleline (line) Nowoffset=F1.tell () writeoffset (str (nowoffset))if __name__=='__main__':    if  notOs.path.exists ('Offset.txt'): With open ("Offset.txt",'W') as F:f.write ('0') T1= Thread (target=analysislog) T2= Thread (target=analysisdb) T1.start () T2.start ()

end!

Python Real-time analytics user Login

Related Article

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.