码迷,mamicode.com
首页 > 编程语言 > 详细

Python 查找binlog文件

时间:2016-04-12 23:59:53      阅读:390      评论:0      收藏:0      [点我收藏+]

标签:

经常需要在 binlog 中查找一些日志信息,于是写了一个简单的脚本。对于非常巨大的 binlog 文件,该脚本可能会速度慢,毕竟还是用的 list,暂时没想到好办法。

详细看代码:

#/usr/bin/python

#2016-04-12

#search string in the binlogs

#usage:
#put this file into binlog-dir,exec as:
#"python test.py 111 123 update" or 
#"python test.py 111 123 update and insert" or 
#"python test.py 111 123 update or delete"
#the nums are the binlog-num.

import sys
import subprocess
 
def find_str(files):
    for file in files:
        comm = "mysqlbinlog {0}".format(file)
        lines = subprocess.Popen(comm, shell=True, stdout=subprocess.PIPE)
        lines = lines.stdout.readlines()
        for line in lines:
            line = line.lower()
            if len(sys.argv) == 4:
                if sys.argv[3] + ‘ ‘ in line:
                    yield line
            elif len(sys.argv) == 6 and sys.argv[4] == "and":
                if sys.argv[3] + ‘ ‘ and sys.argv[5] + ‘ ‘ in line:
                    yield line
            elif len(sys.argv) == 6 and sys.argv[4] == "or":
                if sys.argv[3] + ‘ ‘ or sys.argv[5] + ‘ ‘ in line:
                    yield line

    
if __name__ == "__main__":    
    start = sys.argv[1]
    end = sys.argv[2]
    files = ["updatelog.{0:06d}".format(i) for i in range(int(start), int(end)+1)]    

    f = find_str(files)
    for i in f:
        print(i)

 

Python 查找binlog文件

标签:

原文地址:http://www.cnblogs.com/bvac/p/5384756.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!