mysql报警_mysql监控报警工具

#!/usr/bin/env python

# coding:utf-8

import MySQLdb

import requests, json

import time

url = "http://api.sendcloud.net/apiv2/mail/send"

key = {}

params = {}

#目标用户的邮箱

to_email = {'root': ["xxxx@xxx.com ", ], 'tianyi': ["x@qq.com", "x@sina.com", ]}

#对发送邮件的格式的配置

def sendmail(mail_list, sql):

for number in range(len(mail_list)):

params['apiUser'] = "xxx"

params['apiKey'] = "0Mxb0ag"

params['from'] = "x@foxmail.com"

params['fromName'] = "bill"

params['subject'] = "warning"

params['html'] = "your sqlserver is dangerous: " + sql

params['to'] = mail_list[number]

r = requests.post(url, files={}, data=params)

print r.text

time.sleep(1)

if __name__ == "__main__":

while True:

#配置数据库,root用户可以看见所有用户的线程

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

# 判断对用户的操作

number = 0

#i[4]对应的是Command,i[5]是Time,当sql语句处于执行的状态,并且时间大于1秒的时候

if i[4] == 'Query' and i[5] >= 1:

while number < i[5]:

# 发送邮件

sendmail(to_email[i[1]], str(i[7]))

number = number + 1

# 如果此时大于5秒就杀死进程

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)

#配置说明

#修改所处mysql服务器的连接信息: conn = MySQLdb.connect(host='127.0.0.1', user='root', passwd='x')

#所拥有用户及其对应的邮箱地址:to_email = {'root': ["x@xx.com ", ], 'tianyi': ["x@qq.com", "x@sina.com", ]}

#使用的相关的sendcloud api配置:sendmail()

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值