前言
由于單位尚沒(méi)有完善的監(jiān)控制度,且項(xiàng)目也較少,使用開(kāi)源的監(jiān)控組件有點(diǎn)小題大做,因此研究了基于python的錯(cuò)誤日志監(jiān)控,功能如下:
- 監(jiān)聽(tīng)error日志文件增量數(shù)據(jù)(每分鐘的增量數(shù)據(jù))
- 提供白名單功能,白名單內(nèi)的異常不發(fā)送通知
- 有異常通過(guò)郵件發(fā)送通知
具體實(shí)現(xiàn)
前置條件
- 日志需要進(jìn)行級(jí)別劃分,錯(cuò)誤日志需要與info日志分開(kāi),防止日志過(guò)大導(dǎo)致宿主機(jī)爆棧
- 郵箱開(kāi)通對(duì)應(yīng)服務(wù),用于告警郵件發(fā)送
- python3環(huán)境
畫(huà)一下流程圖

image.png
廢話(huà)不多說(shuō)直接上代碼代碼風(fēng)格是按照java來(lái)的 -_-
#!/usr/bin/env python3
#!coding:utf-8
import subprocess
import datetime
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
import os
import string
#告警接收人
toUser = ['zhiaiyahong@yhwch.com']
#異常白名單,白名單內(nèi)的異常不通知,基于字符串包含進(jìn)行過(guò)濾
excludeClassErrorList = ['TripleDesCryptoHandler']
mySender='xxx@qq.com' # 發(fā)件人郵箱賬號(hào) 改為本人賬號(hào)
myPass = 'xxxx' # 發(fā)件人郵箱密碼(當(dāng)時(shí)申請(qǐng)smtp給的口令)
def grep(filename, arg):
process = subprocess.Popen(['grep', '-n', arg, filename], stdout=subprocess.PIPE)
stdout, stderr = process.communicate()
return stdout
def sendMail(toUser,content,titles):
msg=MIMEText(content,'html','utf-8')
msg['From']=formataddr(["異常日志監(jiān)控告警",mySender])
msg['To']=",".join(toUser)
msg['Subject']=titles
server=smtplib.SMTP_SSL("smtp.qq.com", 465)
server.login(mySender, myPass)
server.sendmail(mySender,toUser,msg.as_string())
server.quit()# 關(guān)閉連接
print("郵件發(fā)送完畢")
def excludeClassError(errorItem):
for x in excludeClassErrorList:
if x in errorItem:
return True
return False
nowDate = datetime.datetime.now()
nowSecond = (nowDate+datetime.timedelta(minutes=-1)).strftime("%Y-%m-%d %H:%M")
errorFileName = nowDate.strftime("%Y-%m-%d")
errorFileName = "/opt/logs/error-"+errorFileName+".log"
if not os.path.isfile(errorFileName):
print(errorFileName,"文件不存在,無(wú)錯(cuò)誤日志,萬(wàn)事大吉")
else:
printout = grep(errorFileName,nowSecond)
printout = printout.decode()
errorList = printout.split('\n')
errorInfo = ""
for x in errorList:
if(excludeClassError(x)):
print("忽略異常",x)
else:
errorInfo = errorInfo + x
if(errorInfo == ''):
print(nowSecond,"無(wú)錯(cuò)誤日志萬(wàn)事大吉")
else:
print(errorInfo)
content = "時(shí)間:"+nowSecond+"<br>異常簡(jiǎn)略:"+errorInfo+"<br>詳細(xì)請(qǐng)登錄服務(wù)器查看日志,目錄:"+errorFileName
titles = "【大事】大批空指針來(lái)襲,祝發(fā)際線高升"
sendMail(toUser,content,titles)