将有效慢日志转存到数据库v2
import re
import sys
import getopt
import MySQLdb
from subprocess import call
import os 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 def main(argv): try:
opts, args =getopt.getopt(argv,"hs:",["slowFileName="])
except getopt.GetoptError:
print 'processFile.py -s <slowFileName>'
sys.exit(1)
for opt, arg in opts:
if opt == '-s':
slowFileName= arg
print "slowFileName is %s" %(slowFileName) slowFileNameEventDetail="%s.EventDetail" %(slowFileName)
generateEventDetailCmd="/usr/local/bin/pt-query-digest --filter '$event->{Rows_examined} >=1000 && print Dumper $event' --noreport %s > %s " %(slowFileName , slowFileNameEventDetail)
print generateEventDetailCmd
os.system(generateEventDetailCmd)
slowFile = open(slowFileNameEventDetail)
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] if __name__ == "__main__":
main(sys.argv[1:])
将有效慢日志转存到数据库v2的更多相关文章
- ASP.NET实现弹出框真分页将复选框选择的数据存到数据库中(四)
这是第四步点击保存将信息存入数据库中. 这个就简单了利用ajax将JSON字符串传到后台然后这里有个知识点就是将DataTable直接存入数据库中.代码如下: 一.界面获取数据JS代码: //保存订单 ...
- 也用 Log4Net 之将日志记录到数据库的后台实现 (二)
也用 Log4Net 之将日志记录到数据库的后台实现 (二) 大家下午好,昨天讲了配置,今天我们讲讲后台实现,在完成了后台实现后,我们才能真正意义上的解决把自定义属性字段值录入到数据库中. 在开写之 ...
- 也用 Log4Net 之将日志记录到数据库的配置 (一)
也用 Log4Net 之将日志记录到数据库的配置 (一) 前段时间我一直想做一个通用一点的日志记录系统,可以便于不同的业务组调用进行日志记录及分析.本来打算着自己下手写一个,后面发现各业务组可能会需 ...
- SQL Server 2008无日志文件附加数据库
1.新建一个同名数据库. 2.停止数据库服务,覆盖新建的数据库主文件(小技巧:最好放在同一个磁盘里面,把新建的数据库主文件删掉或移开,再把要恢复的数据库主文件剪切过去,这样就可以节省时间.) 3.启动 ...
- SQL Server 2005无日志文件附加数据库
公司网站运营两年多了,日志文件超级大,在重装系统的时候,为了省事,就没有备份日志文件,而且是没有分离就把日志文件给删掉了(下次一定要记得先分离再删日志文件).结果造成数据库怎么都附加不上.出现错误. ...
- 飘逸的python - zlib压缩存到数据库
当每天有大量的数据存到kv数据库中去,且value数据很大,于是想压缩后再存进去. 之前提到了 gzip压缩,为什么不直接用gzip呢. 其实更确切的说gzip是一种文件格式,它压缩成gzip文件,而 ...
- java中的中文参数存到数据库乱码问题
关于java中的中文参数乱码问题,遇见过很多,若开发工具的字符集环境和数据库的字符集环境都一样,存到数据库中还是乱码的话,可以通过以下方法解决: 用数据库客户端检查每个字段的字符集和字符集校对和这个表 ...
- 关于使用Log4Net将日志插入oracle数据库中
1.关于配置文件. <?xml version="1.0" encoding="utf-8" ?> <configuration> &l ...
- 利用LogParser将IIS日志插入到数据库
利用LogParser将IIS日志插入到数据库 上面的博文是定制一个计划任务来将log日志定时的导入数据库 下面这篇博文是用cmd指令将日志导入到一张sql表中,是一次性操作 Log P ...
随机推荐
- 在android开发中如何使用JavaMail程序
javaMail,是提供给开发者处理电子邮件相关的编程接口.它是Sun发布的用来处理email的API.它可以方便地执行一些常用的邮件传输.我们可以基于JavaMail开发出类似于Microsoft ...
- 如何将Twitter的内容导入到SAP CRM和C4C
Twitter的内容导入SAP CRM Interaction Center呼叫中心 具体步骤查看我的博客Twitter(also Facebook) is official integrated i ...
- QT OpenGL中文教程在QT4版本后的错误代码更改(一)
由于教程中说的已经够可以了,这里就不对代码进行分析了,有兴趣可以自己去看看.这个教程来源于原来的NeHeOpenGL中文教程 (http://www.yakergong.net/nehe/) ,但其有 ...
- user(),current_user()函数的区别
user() 表示当前的登录用户 current_user() 表示对应于mysql.user表里对应的账号.
- Codeforces Round #347 (Div.2)_A. Complicated GCD
题目链接:http://codeforces.com/contest/664/problem/A A. Complicated GCD time limit per test 1 second mem ...
- Visual Studio 2013 ReportViewer Control
最近需要给学生讲报表,.NET的自然就是选择RDLC了. 因为学生比赛是用Visual Studio 2013,所以我在自己的笔记本上安装了Visual Studio 2013(平常是用2010),安 ...
- ubuntu 报错E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unav E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process us
1.配置xshell,查看虚拟机中ubuntu中网络ip ifconfig 报错 Command 'ifconfig' not found, but can be installed with: su ...
- js实现弹窗一个ip在24小时只弹出一次的代码
function cookieGO(name) { var today = new Date(); var expires = new Date(); expires.setTime(today.ge ...
- C# foreach语句
一.C# foreach语句 foreach语句能够对实现Ienumerable接口的容器进行遍历,并提供一个枚举器来实现Ienumerable接口.foreach语句为数组或对象集合中的各个元素执行 ...
- web之HTTP协议
1.web引用程序 web(world wide web)也叫万维网,是一种基于超文本和HTTP的.全球性的.动态交互的.跨平台的分布式图形信息系统.是建立在Internet上的一种网络服务,为浏览者 ...