zoukankan      html  css  js  c++  java
  • python监控tomcat日记文件

    最近写了一个用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)
  • 相关阅读:
    隐式类型转换
    STL::allocator rebind
    Proxy Class(代理类)
    C++ 没有合适的默认构造函数(无参数构造函数)
    E
    C
    Multiplication Puzzle POJ
    Brackets POJ
    Halloween Costumes LightOJ
    ACwing 139. 回文子串的最大长度(二分+Hash)
  • 原文地址:https://www.cnblogs.com/aijianiula/p/5391532.html
Copyright © 2011-2022 走看看