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