iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >MySQL 5.6大查询和大事务监控脚本(Python 2)
  • 103
分享到

MySQL 5.6大查询和大事务监控脚本(Python 2)

2024-04-02 19:04:59 103人浏览 泡泡鱼
摘要

可以配置在Zabbix里面,作为监控的模版 #!/usr/bin/env python # import Mysqldb,mysqldb.cursors import s

可以配置在Zabbix里面,作为监控的模版


#!/usr/bin/env python
#
import Mysqldb,mysqldb.cursors
import sys,time
from datetime import datetime
innodb_lock_output_file = '/tmp/innodb_lock_output.log'
# Socket_dir = '/var/lib/mysql/mysql.sock'
time_step = 1
db_host = '127.0.0.1'
db_port = 23306
db_user = 'zabbix'
db_pass = 'l8ka65'
f = open(innodb_lock_output_file,'a')
current_time_stamp = int(time.time()) - time_step
current_time = time.ctime()
result = ''
# print sys.argv
if len(sys.argv) <> 2:
    print "Usage: %s current_lock | current_running" % sys.argv[0]
    exit()
db = MySQLdb.connect(host=db_host, user=db_user,
                     passwd=db_pass, charset='utf8',
                     port = db_port
                     # unix_socket=socket_dir
                     )
conn = db.cursor(MySQLdb.cursors.DictCursor)
db.select_db('infORMation_schema')
now_time_sql = 'select now() as now_time;'
conn.execute(now_time_sql)
current_time = conn.fetchall()[0]['now_time']
result += str(current_time)
result += '\n'
lock_sql = '''
    SELECT * FROM INNODB_TRX where TIMESTAMPDIFF(SECOND, trx_started, now()) > 1 ORDER BY trx_started LIMIT 1
    '''
running_sql = '''select user,host,db,time,State,info  from PROCESSLIST where TIME > 30 and  COMMAND  <> 'Sleep' and COMMAND <> 'Binlog Dump' and user <> 'system user' and lower(info) not like '%alter%table%' order by TIME DESC LIMIT 1 '''
if sys.argv[1] == 'current_lock':
    conn.execute(lock_sql)
    query_result = conn.fetchall()
    locks = conn.rowcount
    if locks > 0:
        cur_time = datetime.now()
        print (cur_time - query_result[0]['trx_started']).seconds
    else:
        print 0
    # print result
    for item in query_result:
        for each in item:
            # print each
            result +=  str(each)
            result += '\t'
            result += ':==>>>>\t'
            result += str(item[each])
            result += '\n'
        result += '\n'
    result += '\n'
    # print result
    if locks > 0:
        f.write(result)
elif sys.argv[1] == 'current_running':
    conn.execute(running_sql)
    query_result = conn.fetchall()
    thread_count = conn.rowcount 
    if thread_count > 0 :
        f.write(result)
        for item in conn.fetchall():
            f.write(str(item) + '\n')
        f.write('\n\n\n\n')
        print query_result[0]['time']
    else:
        print 0
else:
    print "Usage: %s current_lock | current_running" % sys.argv[0]
conn.close()
db.close()
f.close()


执行脚本

Python innodb_lock_monitor.py current_running
# python innodb_lock_monitor.py current_lock


慢查询语句会记录在文本文件中


]# tail -300 /tmp/innodb_lock_output.log
blocking_trx_state	:==>>>>	RUNNING
requesting_SQL	:==>>>>	delete who_cart,who_cart_ext from who_cart left join who_cart_ext on who_cart.rec_id = who_cart_ext.cart_id
         where  who_cart.rec_id=1469638027




您可能感兴趣的文档:

--结束END--

本文标题: MySQL 5.6大查询和大事务监控脚本(Python 2)

本文链接: https://www.lsjlt.com/news/48805.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作