ImportReImportMysqldbhost='10.76.45.7'Port=3306User='Test'Password='Test'DbName='Test'defLoadslowlogtodb (mysqlconn,keyvaluelist): Insertsql="INSERT INTO Slowlog (Rows_examined,rows_sent,thread_id,arg,cmd,db,fingerprint,host,ip,timestamp,user) VALUES ( %s,%s,%s,%s,%s,%s,%s,%s,%s,from_unixtime (%s),%s)"%(keyvaluelist['rows_examined'],keyvaluelist['rows_sent'],keyvaluelist['thread_id'],keyvaluelist['Arg'],keyvaluelist['cmd'],keyvaluelist['DB'],keyvaluelist['fingerprint'],keyvaluelist['Host'],keyvaluelist['IP'],keyvaluelist['timestamp'],keyvaluelist['User']) #Print Insertsql Try: Cursor=mysqlconn.cursor () cursor.execute (Insertsql) mysqlconn.commit ()exceptMysqldb.error, E:Print "Error%s:%s"% (E.args[0], e.args[1]) Passdefgetconnection ():Try: Conn=connection=mysqldb.connect (Host=host, Port=port, User=user, Passwd=password, db=dbname,connect_timeout=10) returnConnexceptMysqldb.error, E:Print "Error%s:%s"% (E.args[0], e.args[1]) Pass returnNoneslowfile= Open ('2330.slow.detail') Keyvaluelist={}conn=getconnection () forLineinchSlowfile:ifNone! = Re.match ("^\ $VAR 1 = {$", line):Print "begin"keyvaluelist={} elifNone! = Re.match ("};", line):Print "End"Loadslowlogtodb (conn,keyvaluelist)Else: #Print Linelinestrip=Line.strip () Linelen=Len (Linestrip)ifLinestrip.endswith (','): Linewitoutlastcomma=linestrip[0:-1] Else: Linewitoutlastcomma=Linestrip#Print LinestripKeyvaluepair=re.split ('=', Linewitoutlastcomma)ifLen (KeyValuePair) ==1: keyvaluelist["Arg"]="undefined" Continue #Print Keyvaluepair[0] #Print Keyvaluepair[1]KEYVALUELIST[KEYVALUEPAIR[0]]=KEYVALUEPAIR[1]
Dump slow logs to the database