最近写了一个用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)

最新文章

  1. XML基础
  2. JS控制按钮不能连续被点击
  3. window对象中的常见方法
  4. Android Studio NDK 学习之接受Java传入的Int数组
  5. Effective C++ 条款45
  6. SQL注入专题
  7. visio篇章--1
  8. postgres create table default now
  9. php 用于绘图使用的颜色数组
  10. Animate.css 教程
  11. nrf51 SDK自带例程的解读
  12. 【Cloud Foundry】Could Foundry学习(二)——核心组件分析
  13. SELinux Policy Macros
  14. net.sz.framework 框架 登录服务器架构 单服2 万 TPS(QPS)
  15. Java异常处理-----非运行时异常(受检异常)
  16. 软件工程(GZSD2015) 第二次作业成绩
  17. 从 0 开始手写一个 Mybatis 框架,三步搞定!
  18. fabric 在阿里云Ubuntu部署 注意
  19. linux 同步IO: sync、fsync与fdatasync、sys_sync【转】
  20. 【转】frameset 框架集使用语法,常用语后台。

热门文章

  1. Handler引起的内存泄露
  2. TensorFlow 安装 Win10 Python+GPU
  3. 51nod 1031 骨牌覆盖
  4. Django展示第一个网页
  5. mount nfs 各版本之间的转换
  6. nginx的编译安装
  7. 【原创翻译】链接DLL至可执行文件---翻译自MSDN
  8. web.config中配置数据库连接的两种方式
  9. [题解] cogs 1669 神秘的咒语
  10. GC 机制