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

女爷i 2024-04-17 23:49 33阅读 0赞

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

  1. #!/usr/bin/env python
  2. #
  3. import MySQLdb,MySQLdb.cursors
  4. import sys,time
  5. from datetime import datetime
  6. innodb_lock_output_file = '/tmp/innodb_lock_output.log'
  7. # socket_dir = '/var/lib/mysql/mysql.sock'
  8. time_step = 1
  9. db_host = '127.0.0.1'
  10. db_port = 23306
  11. db_user = 'zabbix'
  12. db_pass = 'l8ka65'
  13. f = open(innodb_lock_output_file,'a')
  14. current_time_stamp = int(time.time()) - time_step
  15. current_time = time.ctime()
  16. result = ''
  17. # print sys.argv
  18. if len(sys.argv) <> 2:
  19. print "Usage: %s current_lock | current_running" % sys.argv[0]
  20. exit()
  21. db = MySQLdb.connect(host=db_host, user=db_user,
  22. passwd=db_pass, charset='utf8',
  23. port = db_port
  24. # unix_socket=socket_dir
  25. )
  26. conn = db.cursor(MySQLdb.cursors.DictCursor)
  27. db.select_db('information_schema')
  28. now_time_sql = 'select now() as now_time;'
  29. conn.execute(now_time_sql)
  30. current_time = conn.fetchall()[0]['now_time']
  31. result += str(current_time)
  32. result += '\n'
  33. lock_sql = '''
  34. SELECT * FROM INNODB_TRX where TIMESTAMPDIFF(SECOND, trx_started, now()) > 1 ORDER BY trx_started LIMIT 1
  35. '''
  36. 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 '''
  37. if sys.argv[1] == 'current_lock':
  38. conn.execute(lock_sql)
  39. query_result = conn.fetchall()
  40. locks = conn.rowcount
  41. if locks > 0:
  42. cur_time = datetime.now()
  43. print (cur_time - query_result[0]['trx_started']).seconds
  44. else:
  45. print 0
  46. # print result
  47. for item in query_result:
  48. for each in item:
  49. # print each
  50. result += str(each)
  51. result += '\t'
  52. result += ':==>>>>\t'
  53. result += str(item[each])
  54. result += '\n'
  55. result += '\n'
  56. result += '\n'
  57. # print result
  58. if locks > 0:
  59. f.write(result)
  60. elif sys.argv[1] == 'current_running':
  61. conn.execute(running_sql)
  62. query_result = conn.fetchall()
  63. thread_count = conn.rowcount
  64. if thread_count > 0 :
  65. f.write(result)
  66. for item in conn.fetchall():
  67. f.write(str(item) + '\n')
  68. f.write('\n\n\n\n')
  69. print query_result[0]['time']
  70. else:
  71. print 0
  72. else:
  73. print "Usage: %s current_lock | current_running" % sys.argv[0]
  74. conn.close()
  75. db.close()
  76. f.close()

执行脚本

  1. # python innodb_lock_monitor.py current_running
  2. # python innodb_lock_monitor.py current_lock

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

  1. ]# tail -300 /tmp/innodb_lock_output.log
  2. blocking_trx_state :==>>>> RUNNING
  3. 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
  4. where who_cart.rec_id=1469638027

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26506993/viewspace-2215359/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/26506993/viewspace-2215359/

发表评论

表情:
评论列表 (有 0 条评论,33人围观)

还没有评论,来说两句吧...

相关阅读