ruosu0710
2017-03-15 11:20:30 +08:00
目前在用的.
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import subprocess
import time
import datetime
from wxnotice import *
#日志文件是按天产生,则通过在程序中判断文件的产生日期与当前时间,更换监控的日志文件
def catch(logfile,server):
print '监控的日志文件是%s' %logfile
send_notice('od8J4s4pbnj2UTwASk-tKr8hKbEU','异常监控任务启动')
stoptime=datetime.datetime.combine(datetime.date.today(), datetime.time.max)
stoptime = time.strftime('%Y-%m-%d %H:%M:%S',stoptime.timetuple())
print stoptime
popen = subprocess.Popen('tail -f ' + logfile, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
pid = popen.pid
print('Popen.pid:' + str(pid))
while True:
thistime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))#当前时间
#print thistime
if thistime >= stoptime:
# 终止子进程
popen.kill()
print '杀死 subprocess'
break
line = popen.stdout.readline().strip()
# 判断内容是否有异常
if line.find('Exception')>0:
line=line+popen.stdout.readline().strip()#
line=line+popen.stdout.readline().strip()#
#line=line+popen.stdout.readline().strip()#
#line=line+popen.stdout.readline().strip()#
#line=line+popen.stdout.readline().strip()#多读一行.获取更多的异常信息
openid_array=['od8J4s4pbnj2UTwASk-tKr8hKbEU','od8J4s8xWPDUtrJTXL-bQ9FYias4']
for openid in openid_array:
content='['+server+']'+line
content=content.replace('\"','')
send_notice(openid,content)#发送微信模板通知给管理员
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import sys
from exception import *
filename= '/data/app/tomcat'+sys.argv[1]+'/logs/catalina.out'
print filename
catch(filename,'TOMCAT'+sys.argv[1])