監(jiān)控數(shù)據(jù)庫是否可寫,如果你的監(jiān)控腳本邏輯是,寫入數(shù)據(jù)庫成功后顯示成功,反之顯示不成功然后報警。那么難題來了,數(shù)據(jù)庫真的無法寫入了,你的監(jiān)控腳本的寫入命令也會被MySQL hang住,一直卡在那里,直到天荒地老,根本無法實現(xiàn)報警。那換個思路,如果設(shè)置個超時時間,是不是更好。
讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:國際域名空間、網(wǎng)站空間、營銷軟件、網(wǎng)站建設(shè)、盤龍網(wǎng)站維護、網(wǎng)站推廣。
#!/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 數(shù)據(jù)庫異常: %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 數(shù)據(jù)庫無法寫入" 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 = "數(shù)據(jù)庫監(jiān)控" 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" >中國區(qū)zabbix數(shù)據(jù)庫監(jiān)控</th> </tr> <td width="100px" nowrap>告警區(qū)域</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數(shù)據(jù)庫監(jiān)控</td> <tr> </tr> <td width="100px" nowrap>告警級別</td> <td bgcolor=red >嚴重</td> <tr> </tr> <td width="100px" nowrap>告警狀態(tài)</td> <td bgcolor=red >PROBLEM</td> <tr> </tr> <td width="100px" nowrap>詳細內(nèi)容</td> <td >""" + mail_result + """</td> <tr> <td width="100px" nowrap>發(fā)生時間</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 "郵件發(fā)送成功!" except Exception, e: print "失敗:"+str(e) ### if __name__ == '__main__': #創(chuàng)建監(jiān)控數(shù)據(jù)庫連接,與是否可寫,的監(jiān)控表,下面是創(chuàng)建語句 #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)
當(dāng)前名稱:用python監(jiān)控mysql數(shù)據(jù)庫是否可寫
URL地址:http://jinyejixie.com/article24/ghoije.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、品牌網(wǎng)站建設(shè)、網(wǎng)站維護、軟件開發(fā)、服務(wù)器托管、網(wǎng)站設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)