可以配置在Zabbix里面,作為監(jiān)控的模版

#!/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()
執(zhí)行腳本
# 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
文章名稱:MySQL5.6大查詢和大事務(wù)監(jiān)控腳本(Python2)-創(chuàng)新互聯(lián)
網(wǎng)頁地址:http://chinadenli.net/article12/cejpgc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、網(wǎng)站設(shè)計公司、電子商務(wù)、網(wǎng)站制作、網(wǎng)站設(shè)計、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)