最近寫了一個用python監控tomcat日記文件的功能
實現的功能:
監控日記文件中實時過來的記錄,統計每分鍾各個接口調用次數,統計結果插入oracle
#!/usr/bin/python # -*- coding: UTF-8 -*- import time import os import signal import subprocess import re import cx_Oracle def monitorLog(logFile,oldDayTime): logUrl='10.0.22.**' #連接數據庫 connstr='username/password@10.0.22.**:**/**' db=cx_Oracle.connect(connstr) cursor = db.cursor() #結束時間 startTime ='' startMinute='' #讀取日記文件尾部日記 popen = subprocess.Popen('tail -f ' + logFile, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) pid = popen.pid print('Popen.pid:' + str(pid)) recommDict={} while True: line = popen.stdout.readline().strip() if line: #正則表達式得到推薦接口名字 matchObj = re.search(r'(?<=recomm=)(.*?)(?=&)', line, re.M | re.I) if matchObj: recommName=matchObj.group() if recommName not in recommDict: recommDict.setdefault(recommName,1) else: value=recommDict[recommName] value+=1 recommDict[recommName]=value #正則表達式獲取分鍾 matchTime = re.search(r'(?<=201[1-9]:)(.*?)(?= +)', line, re.M | re.I) if matchTime: thisTime=str(matchTime.group()) thisMinute=thisTime.split(":")[1] if startMinute is '': startMinute = thisMinute startTime = thisTime if startMinute!= thisMinute: for key in recommDict.keys(): value=str(recommDict[key]) logTime=str(oldDayTime)+" "+startTime sql = """INSERT INTO MINITOR_TOMCATLOGS(LOGURL,BEGINTIME,TIMEINTERVAL,PORTNAME,CALLNUM,UPDATETIME)VALUES ('"""+logUrl+"""','"""+logTime+"""','minute','"""+key+"""',"""+value+""",sysdate)""" cursor.execute(sql) db.commit() #清空recommDict recommDict.clear() startMinute ='' #獲取今天的時間 toDayTime=time.strftime('%Y-%m-%d', time.localtime()) if toDayTime!=oldDayTime: if len(recommDict)>0: for key in recommDict.keys(): value=str(recommDict[key]) logTime=str(oldDayTime)+" "+startTime sql = """INSERT INTO MINITOR_TOMCATLOGS(LOGURL,BEGINTIME,TIMEINTERVAL,PORTNAME,CALLNUM,UPDATETIME)VALUES ('"""+logUrl+"""','"""+logTime+"""','minute','"""+key+"""',"""+value+""",sysdate)""" cursor.execute(sql) db.commit() recommDict.clear() db.close() popen.kill() break nowDate=time.strftime("%Y-%m-%d", time.localtime()) tomcatLog_dir="/opt/apache-tomcat-7.0.54/logs/" currLogFile=tomcatLog_dir+"localhost_access_log."+nowDate+".txt" monitorLog(currLogFile,nowDate) if __name__ == '__main__': nowDate=time.strftime("%Y-%m-%d", time.localtime()) tomcatLog_dir="/opt/apache-tomcat-7.0.54/logs/" currLogFile=tomcatLog_dir+"localhost_access_log."+nowDate+".txt" monitorLog(currLogFile,nowDate)