#usr/BIN/EVN python
#-*-Coding:utf-8-*-
Import OS, sys
Import re
Import MySQLdb
db = MySQLdb.connect (host= ' 192.168.10.94 ', user= ' Inserter ', passwd= ' * * * *, db= ' gydata ')
Cursor=db.cursor ()
LOG_FILE=SYS.ARGV[1]
Print Log_file
For line in open (log_file):
Elements=line.split (")
Cip=elements[0]
RPTTIME=ELEMENTS[1]
Rpttimelength=len (Rpttime)
Rpttime=rpttime[1:rpttimelength]
CORE=ELEMENTS[4]
If Core.startswith ('/c.gif? '):
Rpttime_elements=re.split (' \w+ ', rpttime)
if (rpttime_elements[1]== ' Jan '):
Month= ' 01 '
Elif (rpttime_elements[1]== ' Feb '):
Month= ' 02 '
Elif (rpttime_elements[1]== ' Mar '):
Month= ' 03 '
Elif (rpttime_elements[1]== ' APR '):
Month= ' 04 '
Elif (rpttime_elements[1]== ' may '):
Month= ' 05 '
elif (rpttime_elements[1]== ' June '):
Month= ' 06 '
Elif (rpttime_elements[1]== ' Jul '):
Month= ' 07 '
Elif (rpttime_elements[1]== '):
Month= ' 08 '
Elif (rpttime_elements[1]== ' Sep '):
Month= ' 09 '
Elif (rpttime_elements[1]== ' Oct '):
Month= ' 10 '
Elif (rpttime_elements[1]== ' Nov '):
Month= ' 11 '
Elif (rpttime_elements[1]== ' Dec '):
Month= ' 12 '
rpttime=rpttime_elements[2]+ '-' +month+ '-' +rpttime_elements[0]+ ' +rpttime_elements[3]+ ': ' +rpttime_elements[4]+ ' : ' +rpttime_elements[5]
Core_content=core.split ('/c.gif? ') [1]
keyvalue={}
Items=core_content.split (' & ')
For item in items:
Key=item.split (' = ') [0]
Value=item.split (' = ') [1]
Keyvalue[key]=value
Tr=keyvalue.get (' tr ')
if (Tr==none):
Tr= "
Else
Keyvalue.pop (' tr ')
Subtr=keyvalue.get (' SUBTR ')
if (Subtr==none):
Subtr= "
Else
Keyvalue.pop (' SUBTR ')
Uuid=keyvalue.get (' uuid ')
if (Uuid==none):
Uuid= "
Else
Keyvalue.pop (' uuid ')
Cbios=keyvalue.get (' Cbios ')
if (Cbios==none):
Cbios= "
Else
Keyvalue.pop (' Cbios ')
Cmac=keyvalue.get (' Cmac ')
if (Cmac==none):
Cmac= "
Else
Keyvalue.pop (' Cmac ')
Seq=keyvalue.get (' seq ')
if (Seq==none):
Seq=-1
Else
Keyvalue.pop (' seq ')
Chk=keyvalue.get (' chk ')
if (Chk==none):
Chk= "
Else
Keyvalue.pop (' chk ')
Salt=keyvalue.get (' salt ')
if (Salt==none):
Salt= "
Else
Keyvalue.pop (' salt ')
Try
Cursor.execute ("INSERT into Value_header (UUID, Cbios, CMAC, SEQ, TR, SUBTR, CHK, SALT, Rpttime, CIP) VALUES ('%s ', '%s ', '%s ') ,%s, '%s ', '%s ', '%s ', '%s ', '%s ', '%s ') "% (UUID,CBIOS,CMAC,SEQ,TR,SUBTR,CHK,SALT,RPTTIME,CIP))
Except
Continue
For k in Keyvalue.keys ():
if (k<> '):
Try
#print "INSERT into values_detail_%s_%s (UUID, Rpttime, Ritem, RVALUE) VALUES ('%s ', '%s ', '%s ', '%s ')"% (Tr,subtr,uuid, RPTTIME,K,KEYVALUE[K])
Cursor.execute ("INSERT into value_detail_%s_%s (UUID, Rpttime, Ritem, RVALUE) VALUES ('%s ', '%s ', '%s ', '%s ')"% (TR,SUBTR, UUID,RPTTIME,K,KEYVALUE[K]))
Except
Continue
This processing efficiency is not high, can be
if (rpttime_elements[1]== ' Jan '):
Month= ' 01 '
Elif (rpttime_elements[1]== ' Feb '):
Month= ' 02 '
Elif (rpttime_elements[1]== ' Mar '):
Month= ' 03 '
Elif (rpttime_elements[1]== ' APR '):
Month= ' 04 '
Elif (rpttime_elements[1]== ' may '):
Month= ' 05 '
elif (rpttime_elements[1]== ' June '):
Month= ' 06 '
Elif (rpttime_elements[1]== ' Jul '):
Month= ' 07 '
Elif (rpttime_elements[1]== '):
Month= ' 08 '
Elif (rpttime_elements[1]== ' Sep '):
Month= ' 09 '
Elif (rpttime_elements[1]== ' Oct '):
Month= ' 10 '
Elif (rpttime_elements[1]== ' Nov '):
Month= ' 11 '
Elif (rpttime_elements[1]== ' Dec '):
Month= ' 12 '
This section is replaced by a hash table.
Python handles Nginx log storage