将慢日志转存到数据库

Posted 友哥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将慢日志转存到数据库相关的知识,希望对你有一定的参考价值。

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]

 

以上是关于将慢日志转存到数据库的主要内容,如果未能解决你的问题,请参考以下文章

是否可以将 SQL 注释添加到使用 ORM 构建的查询中?

argparse 代码片段只打印部分日志

常用python日期日志获取内容循环的代码片段

elk日志收集

Oracle数据库从RMAN备份集片段还原指定单个归档日志进行日志挖掘分析

用ElasticSearch,LogStash,Kibana搭建实时日志收集系统