監(jiān)控?cái)?shù)據(jù)庫是否可寫,如果你的監(jiān)控腳本邏輯是,寫入數(shù)據(jù)庫成功后顯示成功,反之顯示不成功然后報(bào)警。那么難題來了,數(shù)據(jù)庫真的無法寫入了,你的監(jiān)控腳本的寫入命令也會被mysql hang住,一直卡在那里,直到天荒地老,根本無法實(shí)現(xiàn)報(bào)警。那換個思路,如果設(shè)置個超時時間,是不是更好。

為桐城等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及桐城網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為
成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、桐城網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!#!/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>主機(jī)名稱</td>
<td >xxx.xxx.xxx.xxx</td>
<tr>
</tr>
<td width="100px" nowrap>告警項(xiàng)目</td>
<td >zabbix數(shù)據(jù)庫監(jiān)控</td>
<tr>
</tr>
<td width="100px" nowrap>告警級別</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ā)生時間</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ù)庫連接,與是否可寫,的監(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)
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
當(dāng)前題目:用python監(jiān)控mysql數(shù)據(jù)庫是否可寫-創(chuàng)新互聯(lián)
本文地址:http://aaarwkj.com/article16/cojddg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司、域名注冊、品牌網(wǎng)站制作、服務(wù)器托管、自適應(yīng)網(wǎng)站、定制開發(fā)
廣告
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源:
創(chuàng)新互聯(lián)