当前位置:Gxlcms > 数据库问题 > mysql监控报警工具

mysql监控报警工具

时间:2021-07-01 10:21:17 帮助过:20人阅读

  1. #!/usr/bin/env python
  2. # coding:utf-8
  3. import MySQLdb
  4. import requests, json
  5. import time
  6. url = "http://api.sendcloud.net/apiv2/mail/send"
  7. key = {}
  8. params = {}
  9. #目标用户的邮箱
  10. to_email = {‘root‘: ["xxxx@xxx.com ", ], ‘tianyi‘: ["x@qq.com", "x@sina.com", ]}
  11. #对发送邮件的格式的配置
  12. def sendmail(mail_list, sql):
  13. for number in range(len(mail_list)):
  14. params[‘apiUser‘] = "xxx"
  15. params[‘apiKey‘] = "0Mxb0ag"
  16. params[‘from‘] = "x@foxmail.com"
  17. params[‘fromName‘] = "bill"
  18. params[‘subject‘] = "warning"
  19. params[‘html‘] = "your sqlserver is dangerous: " + sql
  20. params[‘to‘] = mail_list[number]
  21. r = requests.post(url, files={}, data=params)
  22. print r.text
  23. time.sleep(1)
  24. if __name__ == "__main__":
  25. while True:
  26. #配置数据库,root用户可以看见所有用户的线程
  27. conn = MySQLdb.connect(host=‘127.0.0.1‘, user=‘root‘, passwd=‘x‘)
  28. cur = conn.cursor()
  29. reCount = cur.execute(‘show processlist;‘)
  30. for i in cur.fetchall():
  31. print i
  32. # 判断对用户的操作
  33. number = 0
  34. #i[4]对应的是Command,i[5]是Time,当sql语句处于执行的状态,并且时间大于1秒的时候
  35. if i[4] == ‘Query‘ and i[5] >= 1:
  36. while number < i[5]:
  37. # 发送邮件
  38. sendmail(to_email[i[1]], str(i[7]))
  39. number = number + 1
  40. # 如果此时大于5秒就杀死进程
  41. if number == 4:
  42. id = str(i[0])
  43. print id
  44. sql = ‘kill ‘ + id + ‘;‘
  45. cur.execute(sql)
  46. break
  47. time.sleep(1)
  48. cur.close()
  49. conn.close()
  50. time.sleep(1)
  51. #配置说明
  52. #修改所处mysql服务器的连接信息: conn = MySQLdb.connect(host=‘127.0.0.1‘, user=‘root‘, passwd=‘x‘)
  53. #所拥有用户及其对应的邮箱地址:to_email = {‘root‘: ["x@xx.com ", ], ‘tianyi‘: ["x@qq.com", "x@sina.com", ]}
  54. #使用的相关的sendcloud api配置:sendmail()

 

 

参考资料:https://dev.mysql.com/doc/refman/5.7/en/show-processlist.html

     http://www.linuxidc.com/Linux/2016-02/128558.htm

mysql监控报警工具

标签:data   highlight   sendmail   sub   url   imp   bin   usr   inux   

人气教程排行