在公司里做的一個接口系統(tǒng)正压,主要是對接第三方的系統(tǒng)接口欣福,所以,這個系統(tǒng)里會和很多其他公司的項目交互焦履。隨之而來一個很蛋疼的問題拓劝,這么多公司的接口雏逾,不同公司接口的穩(wěn)定性差別很大,訪問量大的時候郑临,有的不怎么行的接口就各種出錯了栖博。
這個接口系統(tǒng)剛剛開發(fā)不久,整個系統(tǒng)中厢洞,處于比較邊緣的位置仇让,不像其他項目,有日志庫躺翻,還有短信告警丧叽,一旦出問題,很多情況下都是用戶反饋回來公你,所以踊淳,我的想法是,拿起python陕靠,為這個項目寫一個監(jiān)控迂尝。如果在調(diào)用某個第三方接口的過程中,大量出錯了剪芥,說明這個接口有有問題了雹舀,就可以更快的采取措施。
項目的也是有日志庫的粗俱,所有的info,error日志都是每隔一分鐘掃描入庫虚吟,日志庫是用的mysql寸认,表里有幾個特別重要的字段:
evel 日志級別
message 日志內(nèi)容
file_name Java代碼文件
log_time 日志時間
有日志庫,就不用自己去線上環(huán)境掃日志分析了串慰,直接從日志庫入手偏塞。由于日志庫在線上時每隔1分鐘掃,那我就去日志庫每隔2分鐘掃一次邦鲫,如果掃到有一定數(shù)量的error日志就報警灸叼,如果只有一兩條錯誤就可以無視了,也就是短時間爆發(fā)大量錯誤日志庆捺,就可以斷定系統(tǒng)有問題了古今。報警方式就用發(fā)送郵件,所以滔以,需要做下面幾件事情:
- 操作MySql捉腥。
- 發(fā)送郵件。
- 定時任務(wù)你画。
- 日志抵碟。
- 運行腳本桃漾。
明確了以上幾件事情,就可以動手了拟逮。
操作數(shù)據(jù)庫
使用MySQLdb這個驅(qū)動撬统,直接操作數(shù)據(jù)庫,主要就是查詢操作敦迄。
獲取數(shù)據(jù)庫的連接:
def get_con(): host = "127.0.0.1" port = 3306 logsdb = "logsdb" user = "root" password = "never tell you" con = MySQLdb.connect(host=host, user=user, passwd=password, db=logsdb, port=port, charset="utf8") return con
從日志庫里獲取數(shù)據(jù)恋追,獲取當(dāng)前時間之前2分鐘的數(shù)據(jù),首先颅崩,根據(jù)當(dāng)前時間進行計算一下時間
def calculate_time(): now = datetime.now() now_min = now.minute if now_min < 2: now_min += 60 - 2 else: now_min -= 2 return now.replace(minute=now_min).strftime("%Y-%m-%d %H:%M:%S")
然后几于,根據(jù)時間和日志級別去日志庫查詢數(shù)據(jù)
def get_data(): select_time = calculate_time() logger.info("select time:"+select_time) sql = "select file_name,message from logsdb.app_logs_record " "where log_time >"+"'"+select_time+"'" "and level="+"'ERROR'" "order by log_time desc" conn = get_con() cursor = conn.cursor() cursor.execute(sql) results = cursor.fetchall() cursor.close() conn.close() return results
發(fā)送郵件
使用python發(fā)送郵件比較簡單,使用標(biāo)準(zhǔn)庫smtplib就可以
這里使用163郵箱進行發(fā)送沿后,你可以使用其他郵箱或者企業(yè)郵箱都行沿彭,不過host和port要設(shè)置正確。
def send_email(content):
sender = "sender_monitor@163.com" receiver = ["rec01@163.com", "rec02@163.com"] host = 'smtp.163.com' port = 465 msg = MIMEText(content) msg['From'] = "sender_monitor@163.com" msg['To'] = "rec01@163.com,rec02@163.com" msg['Subject'] = "system error warning" try: smtp = smtplib.SMTP_SSL(host, port) smtp.login(sender, '123456') smtp.sendmail(sender, receiver, msg.as_string()) logger.info("send email success") except Exception, e: logger.error(e)
定時任務(wù)
使用一個單獨的線程尖滚,每2分鐘掃描一次喉刘,如果ERROR級別的日志條數(shù)超過5條,就發(fā)郵件通知漆弄。
def task(): while True: logger.info("monitor running") results = get_data() if results is not None and len(results) > 5: content = "recharge error:" logger.info("a lot of error,so send mail") for r in results: content += r[1]+'
' send_email(content) sleep(2*60)
日志
為這個小小的腳本配置一下日志log.py睦裳,讓日志可以輸出到文件和控制臺中。
所以廉邑,最后,這個監(jiān)控小程序就是這樣的app_monitor.py
運行腳本
腳本在服務(wù)器上運行蛛蒙,使用supervisor進行管理。
在服務(wù)器(centos6)上安裝supervisor渤愁,然后在/etc/supervisor.conf中加入一下配置
[program:app-monitor]command = python /root/monitor/app_monitor.pydirectory = /root/monitoruser = root
然后在終端中運行supervisord啟動supervisor牵祟。
在終端中運行supervisorctl,進入shell抖格,運行status查看腳本的運行狀態(tài)诺苹。
總結(jié)
這個小監(jiān)控思路很清晰,還可以繼續(xù)修改雹拄,比如:監(jiān)控特定的接口收奔,發(fā)送短信通知等等。
因為有日志庫滓玖,就少了去線上正式環(huán)境掃描日志的麻煩筹淫,所以,如果沒有日志庫,就要自己上線上環(huán)境掃描损姜,在正式線上環(huán)境一定要小心哇~