MySQL Monitoring alarm Tool

Source: Internet
Author: User

#!/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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.