MySQL 5.6大查詢和大事務監控指令碼(Python 2)
可以配置在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
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26506993/viewspace-2215359/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- shell指令碼:監控MySQL服務是否正常指令碼MySql
- Shell指令碼監控MySQL主從狀態指令碼MySql
- 網路卡流量監控指令碼,python實現指令碼Python
- 【MySQL】MHA原始碼之監控檢查(一)MySql原始碼
- 【shell】磁碟監控指令碼指令碼
- shell指令碼監控啟動停止weblogic服務指令碼Web
- Sqlserver查詢alwayson同步情況指令碼(2)SQLServer指令碼
- Oracle查詢回滾大事務所需時間Oracle
- mysql大事務MySql
- 系統監控&JVM監控指標資料查詢JVM指標
- PostgreSQL之鎖監控指令碼SQL指令碼
- python查詢mysql中文亂碼問題PythonMySql
- 分享實用監控指令碼:使用Shell檢查程式是否存在指令碼
- 監控系統告警指令碼集合指令碼
- 使用Zabbix + Python對Mysql監控PythonMySql
- 基於Ping和Telnet/NC的監控指令碼案例分析指令碼
- 2 個簡單的 python 指令碼,連線 MySQL 和讀取 ExcelPython指令碼MySqlExcel
- Shell 系統資訊監控指令碼指令碼
- MySQL之連線查詢和子查詢MySql
- Grafana+Prometheus 監控 MySql服務GrafanaPrometheusMySql
- mySQL多表查詢與事務MySql
- 監控磁碟使用率的shell指令碼指令碼
- centos 監控web站點是否500 指令碼CentOSWeb指令碼
- PowerShell 指令碼來監控 CPU、記憶體和磁碟使用情況:指令碼記憶體
- 如何用bash shell 指令碼監控 Linux記憶體、磁碟和 CPU?指令碼Linux記憶體
- MYSQL和SQLServer效能監控指標MySqlServer指標
- MySQL 無法滿足查詢效能?北明天時選擇 TDengine 實現熱網監控和能源分析MySql
- MySQL——優化巢狀查詢和分頁查詢MySql優化巢狀
- MySQL全面瓦解11:子查詢和組合查詢MySql
- 手撕Vue-查詢指令和模板Vue
- Kubernetes監控實踐(2):可行監控方案之Prometheus和SensuPrometheus
- 關於前端指令碼異常監控的思考前端指令碼
- Sentry 監控 - Snuba 資料中臺架構(編寫和測試 Snuba 查詢)架構
- TiDB 查詢優化及調優系列(三)慢查詢診斷監控及排查TiDB優化
- MySQL監控工具MySql
- 理解索引(中):MySQL查詢過程和高階查詢索引MySql
- 微服務:指標和健康監控微服務指標
- Python中使用MySQL模糊查詢的方法PythonMySql