標籤:用python監控mysql資料庫是否可寫
監控資料庫是否可寫,如果你的監控指令碼邏輯是,寫入資料庫成功後顯示成功,反之顯示不成功然後警示。那麼難題來了,資料庫真的無法寫入了,你的監控指令碼的寫入命令也會被mysql hang住,一直卡在那裡,直到天荒地老,根本無法實現警示。那換個思路,如果設定個逾時時間,是不是更好。
#!/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 資料庫異常: %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 = "[email protected]" FROM = "[email protected]" 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" style="font-size:15px">中國區zabbix資料庫監控</th> </tr> <td width="100px" style="font-size:15px" nowrap>警示地區</td> <td style="font-size:15px">中國</td> <tr> </tr> <td width="100px" style="font-size:15px" nowrap>主機名稱</td> <td style="font-size:15px">xxx.xxx.xxx.xxx</td> <tr> </tr> <td width="100px" style="font-size:15px" nowrap>警示項目</td> <td style="font-size:15px">zabbix資料庫監控</td> <tr> </tr> <td width="100px" style="font-size:15px" nowrap>警示層級</td> <td bgcolor=red style="font-size:15px">嚴重</td> <tr> </tr> <td width="100px" style="font-size:15px" nowrap>警示狀態</td> <td bgcolor=red style="font-size:15px">PROBLEM</td> <tr> </tr> <td width="100px" style="font-size:15px" nowrap>詳細內容</td> <td style="font-size:15px">""" + mail_result + """</td> <tr> <td width="100px" style="font-size:15px" nowrap>發生時間</td> <td style="font-size:15px">""" + 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)
本文出自 “杜雲龍” 部落格,請務必保留此出處http://duyunlong.blog.51cto.com/1054716/1921655
用python監控mysql資料庫是否可寫