-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoitor_log.py
More file actions
61 lines (49 loc) · 1.58 KB
/
moitor_log.py
File metadata and controls
61 lines (49 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
# -*-coding:utf-8-*-
# Filename: monitorLog.py
# Author: yy520it@gmail.com
# This is a real-time monitoring logs, the script for the specified keyword alarm
import subprocess
import sys
import urllib
import urllib2
import os
import signal
import time
#短信接口
def sendsms(mobile,content):
URL = 'SMS interface url'
content = '[%s] %s' % (time.strftime('%Y%m%d %H:%M:%S'),content)
data = {'m':mobile,'c':content}
body = urllib.urlencode(data)
request = urllib2.Request(URL,body)
urldata = urllib2.urlopen(request)
#日志文件目录和名称
logFile = '/usr/local/tomcat8/logs/name.log'
def monitorLog(logFile):
print '监控的日志文件 是%s' % logFile
popen = subprocess.Popen('tail -f ' + logFile, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
pid = popen.pid
print('Popen.pid:' + str(pid))
while True:
line = popen.stdout.readline().strip()
# 判断内容是否为空,避免在重启tomcat,杀掉日志
if len(line) == 0:
print 'tail进程死掉'
popen.kill()
print '杀掉进程,重新执行程序'
break
# 发送报警人
if 'org.hibernate.exception.LockTimeoutException' in line:
print('发现异常报警')
sendsms(15066666666,'级别:严重 LockTimeoutException错误')
# 当前时间
thistime = time.strftime('%H:%M')
if thistime == '23:59':
popen.kill()
sys.exit('清空僵尸进程')
print '等待180秒'
time.sleep(180)
monitorLog(logFile)
if __name__ == '__main__':
monitorLog(logFile)