• python写的分析mysql binlog日志工具


    因为数据库增删改突然暴增,需要查询是那些表的操作特别频繁,写了一个用来分析bin-log的小工具,找出增删改查的表,并按照操作次数降序排列,以下是代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    #for mysql5.5 binlog
    import os,sys
    #python binlog.py binglog-0001 '2013-07-01 00:00:00' '2013-07-02 00:00:00'
    def log_w(type,text):
        logfile = "%s.txt" % (type,text)
        #now = time.strftime("%Y-%m-%d %H:%M:%S")
        tt = str(text) + " "
        f = open(logfile,'a+')
        f.write(tt)
        f.close()
    logname = sys.argv[1]
    start_time = sys.argv[2]
    end_time = sys.argv[3]
    comn = "/usr/bin/mysqlbinlog --start-datetime='%s' --stop-datetime='%s' %s" % (start_time,end_time,logname)
    aa=os.popen(comn).readlines()
    mylist=[]
    for a in aa:
        if ('UPDATE' in a):
                update = ' '.join(a.split()[:2])
                mylist.append(update)
        if ('INSERT INTO' in a):
                update = ' '.join(a.split()[:3]).replace("INTO ","")
                mylist.append(update)
        if ('DELETE from' in a):
                update = ' '.join(a.split()[:3]).replace("from ","")
                mylist.append(update)
    mylist.sort()
    bb = list(set(mylist))
    bb.sort()
    cc = []
    for item in bb:
            cc.append([mylist.count(item),(item)])
    cc.sort()
    cc.reverse()
    for i in cc:
            print str(i[0])+' '+i[1]

    spacer.gifspacer.gif执行结果如下:165939295.jpg

    本文出自 “王伟” 博客,请务必保留此出处http://wangwei007.blog.51cto.com/68019/1306940

  • 相关阅读:
    Centos7.0 安装Oralce 11g数据库
    python学习:基础数据类型
    Centos7.0 安装MySQL数据库
    Centos7.0 安装MariaDB数据库
    微服务的详情
    Class -- 10 -- Method类常用方法解析
    遍历list的三种方式
    使用@Autowired注解警告Field injection is not recommended
    java中的两种排序工具Arrays和Collections的使用
    java的reflection和introspector
  • 原文地址:https://www.cnblogs.com/wajika/p/6235639.html
Copyright © 2020-2023  润新知