If the monitoring database is writable, if your monitoring script logic is successful after writing to the database, the display is unsuccessful and then the alarm is displayed. So the problem comes, the database really can not write, your monitoring script write command will also be the MySQL hang, stuck there, until the end of the day, can not realize the alarm. Then change the idea, if set a timeout time, is not better.
#!/usr/bin/env python# -*-coding:utf8-*-import mysqldbimport reimport smtplibimport jsonfrom email.mime.text import MIMETextimport sysimport timeimport Multiprocessingreload (SYS) sys.setdefaultencoding (' UTF8 ') def mysql_select (sql, pipe): try: conn = mysqldb.connect (host= ' xxx.xxx.xxx.xxx ', user= ' xxxx ', passwd= ' xxxx ', db= ' xxxx ', port=xxxx,charset= ' UTF8 ', connect_timeout=10 cursor = conn.cursor () cursor.execute (SQL) result = cursor.fetchall () cursor.close () Conn.commit () conn.close () pipe.send (' SuccessfuL ') except Exception,e: pipe.send ("zabbix database exception: %s" % e) def query_with_timeout (SQL): pipe_out, pipe_in = multiprocessing. Pipe (False) subproc = multiprocessing. Process (target=mysql_select,args= (sql, pipe_in)) subproc.start () subproc.join (timeout=3) if pipe_out.poll ():        EX_C = PIPE_OUT.RECV () else: ex_c = "zabbix database cannot be written" subproc.terminate () #raise exception (" Query %r ran for >%r " % (sql, 5) raise exception (Ex_c) ### def se_mail (mail_result): now_time = time.strftime ('%y-%m-%d %h:%m:%s ', Time.localtime (Time.time ())) sys.setdefaultencoding (' utf-8 ') SUBJECT = "database Monitoring" TO = "[email protected]" FROM = "[email Protected] " msg = mimetext (" ""
This article is from the "Du Yunlong" blog, make sure to keep this source http://duyunlong.blog.51cto.com/1054716/1921655
Use Python to monitor whether MySQL database is writable