Use Python to monitor whether MySQL database is writable

Source: Internet
Author: User

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

Related Article

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.