#!/usr/bin/env python# Coding:utf-8import mysqldbimport requests, jsonimport Timeurl = "Http://api.sendcloud.net/apiv2 /mail/send "key = {}params = {} #目标用户的邮箱to_email = {' Root ': [" [email protected] ",], ' Tianyi ': [" [Email protecte] D] "," [email protected] ",]} #对发送邮件的格式的配置def sendmail (mail_list, SQL): For number in range (len (mail_list)): params[' apiuser ' = "xxx" params[' apiKey '] = "0mxb0ag" params[' from '] = "[email protected]" Para ms[' fromname ' = "Bill" params[' subject '] = "warning" params[' html '] = "Your SQL Server is dangerous:" + SQL Params[' to '] = Mail_list[number] r = requests.post (URL, files={}, data=params) print R.text t Ime.sleep (1) if __name__ = = "__main__": While True: #配置数据库, the root user can see all user threads conn = mysqldb.connect (host= ' 127.0.0.1 ', user= ' root ', passwd= ' x ') cur = conn.cursor () recount = Cur.execute (' show processlist; ') For I in Cur.fetchall (): Print I # Determines the operation of the user number = 0 #i [4] corresponds to COMMAND,I[5] is time, when the SQL statement is in the state of execution, and is more than 1 seconds If i[4] = = ' Query ' and i[5] >= 1:while number < i[5]: # Send mail SendMail (to_email[i[1]], str (i[7)) Number = number + 1 # If this is more than 5 seconds, kill the process If number = = 4:id = str (i[0]) Print ID sql = ' kill ' + ID + '; ' Cur.execute (SQL) break Time.sleep (1) cur.close () Conn.close () Time.sleep (1) #配置说明 # Modify the connection information for the MySQL server: conn = MySQLdb.connect (host= ' 127.0.0.1 ', user= ' root ', passwd= ' x ') # Owned users and their corresponding email addresses: To_email = {' Root ': ["[email protected]",], ' Tianyi ': ["[Email protected]", "[email Protected] ",]} #使用的相关的sendcloud API configuration: SendMail ()
Reference: https://dev.mysql.com/doc/refman/5.7/en/show-processlist.html
Http://www.linuxidc.com/Linux/2016-02/128558.htm
MySQL Monitoring alarm Tool