用python监控mysql数据库是否可写-创新互联

监控数据库是否可写,如果你的监控脚本逻辑是,写入数据库成功后显示成功,反之显示不成功然后报警。那么难题来了,数据库真的无法写入了,你的监控脚本的写入命令也会被mysql hang住,一直卡在那里,直到天荒地老,根本无法实现报警。那换个思路,如果设置个超时时间,是不是更好。

为桐城等地区用户提供了全套网页设计制作服务,及桐城网站建设行业解决方案。主营业务为成都网站设计、成都做网站、桐城网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!#!/usr/bin/env python # -*-coding:utf8-*- import MySQLdb import re import smtplib import json from email.mime.text  import MIMEText import sys import time import multiprocessing reload(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 数据库异常: %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 数据库无法写入"     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 = "数据库监控"     TO = "xxxx@ptthink.com"     FROM = "ooooo@ptmind.com"     msg = MIMEText("""         <html>             <head>             </head>             <body>                 <table width="800" border="1" cellspacing="0" cellpadding="4">                     <tr>                        <th bgcolor="#00FFFF" height="1"  colspan="4" align="center"  >中国区zabbix数据库监控</th>                     </tr>                          <td  width="100px"    nowrap>告警区域</td>                          <td  >中国</td>                     <tr>                     </tr>                          <td  width="100px"    nowrap>主机名称</td>                          <td  >xxx.xxx.xxx.xxx</td>                     <tr>                     </tr>                          <td  width="100px"    nowrap>告警项目</td>                          <td  >zabbix数据库监控</td>                     <tr>                     </tr>                          <td  width="100px"    nowrap>告警级别</td>                          <td  bgcolor=red >严重</td>                     <tr>                     </tr>                          <td  width="100px"    nowrap>告警状态</td>                          <td  bgcolor=red >PROBLEM</td>                     <tr>                     </tr>                          <td  width="100px"    nowrap>详细内容</td>                          <td  >""" + mail_result + """</td>                     <tr>                          <td  width="100px"    nowrap>发生时间</td>                          <td  >""" + now_time + """</td>                     </tr>                 </table>             </body>         </html>""","html","utf-8")     msg['Subject'] = SUBJECT     msg['From']=FROM     msg['To']=TO         try:         server = smtplib.SMTP('localhost')         server.sendmail(FROM, TO, msg.as_string())         server.quit()                 print "邮件发送成功!"     except Exception, e:                 print "失败:"+str(e)          ###      if __name__ == '__main__':         #创建监控数据库连接,与是否可写,的监控表,下面是创建语句     #sql_user_info = """     #CREATE TABLE IF NOT EXISTS db_check_table (     #itemid INT(20),     #applicationid INT(20),     #hostid INT(20),     #name  VARCHAR(255),     #du_name  VARCHAR(255),     #item_name   VARCHAR(255)     #)     #"""     insert_sql = """insert into db_check_table values ('10211','13564','456789','test-172.5.6.7','cpu','cpu ldie')"""        try:         query_with_timeout(insert_sql)         except Exception,e:         mail_result = str(e)                 if mail_result != "successful" :             se_mail(mail_result)

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

当前题目:用python监控mysql数据库是否可写-创新互联
本文地址:https://www.cdcxhl.com/article16/cojddg.html

成都网站建设公司_创新互联,为您提供网页设计公司域名注册品牌网站制作服务器托管自适应网站定制开发

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

外贸网站建设