zoukankan      html  css  js  c++  java
  • 将慢日志转存到数据库

    import re
    import MySQLdb
    
    host='10.76.45.7'
    port=3306
    user='test'
    password='test'
    dbName='test'
    
    def loadSlowLogtoDb(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()
        except MySQLdb.Error, e:
                    print "Error %s: %s" % (e.args[0], e.args[1])
                    pass
    
    def getConnection():
        try:
            conn=connection=MySQLdb.connect(host=host, port=port, user=user, passwd=password, db=dbName,connect_timeout=10)
            return conn
        except MySQLdb.Error, e:
                print "Error %s: %s" % (e.args[0], e.args[1])
            pass
        return None
    
    slowFile = open('2330.slow.detail')
    keyValueList={}
    conn=getConnection()
    for line in slowFile:
        if None != re.match("^$VAR1 = {$",line):
            print "begin"
            keyValueList={}
        elif None != re.match("};",line):
            print "end"
            loadSlowLogtoDb(conn,keyValueList)
        else:
            #print line
            lineStrip=line.strip()
            lineLen=len(lineStrip)
            if lineStrip.endswith(','):
                lineWitoutLastComma=lineStrip[0:-1]
            else:
                lineWitoutLastComma=lineStrip
            #print lineStrip
            keyValuePair=re.split(' => ',lineWitoutLastComma)
            if len(keyValuePair)==1:
                keyValueList["arg"]="undefined"
                continue
            #print keyValuePair[0]
            #print keyValuePair[1]
            keyValueList[keyValuePair[0]]=keyValuePair[1]
    MySQL限时解答,24小时内友哥专业解答
    http://www.yougemysqldba.com
    如有进一步需要请联系微信onesoft007
    微博账号@友哥一指
  • 相关阅读:
    背包系列 hdu3449 有依赖背包
    背包系列 hdu 3535 分组背包
    屏蔽scrollview的滚动
    高精度算法代码
    输入法出现时,中间固定,底部上移的代码
    排序之分治排序
    排序之双向冒泡排序
    Miller Rabin 大素数测试
    来聊聊WWDC 苹果大会上的那些黑科技
    不想成为好leader的程序猿不是好攻城狮
  • 原文地址:https://www.cnblogs.com/youge-OneSQL/p/7532169.html
Copyright © 2011-2022 走看看