Python监控MySQL Active Session并通过钉

2019-10-29  本文已影响0人  左轮Lee

之前有一篇监控慢查询并企业微信推送的文章 https://www.jianshu.com/p/1545c1e061b3,但是由于锁等待时间不计入慢查询,并且希望能及时发现长时间执行的sql,于是重新撸了一段Python代码来监控实时session。

使用步骤:
1.配置待监控的db信息 settings.py

DBLIST_INFO = [{'host': '192.168.211.1', 'port': 3306, 'username': 'root', 'password': 'root123'},
               {'host': '192.168.211.1', 'port': 3307, 'username': 'root', 'password': 'root123'}]

2.设置自己的钉钉机器人 check_thread.py

    webhook = "https://oapi.dingtalk.com/robot/send?access_token=钉钉机器人"

3.运行脚本

# 监控执行时间大于8s的session,即 select * from information_schema.`processlist` where time>8;
python check_thread.py -t 8 

4.监控报警示例

报警示例.png

github地址:https://github.com/ZhuhongLee/monitor_long_active_threads

上一篇下一篇

猜你喜欢

热点阅读