收集Tomcat异常日志并发送邮件

脚本功能:

收集Tomcat异常日志并发送邮件,如果修改正则表达式,也可以用于收集其他日志文件的错误信息

运行环境:

Python2.7/2.4皆可运行

脚本使用方法:

可利用Crontab或者计划任务来指定时间运行,例如:

*/10 * * * * 脚本路径

脚本运行效果如下:

脚本内容如下:

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python
# coding=utf-8
#---------------------------------------------------------
# Name: Tomcat错误日志发送邮件脚本
# Purpose: 收集Tomcat异常日志并发送邮件
# Version: 1.0
# Author: LEO
# BLOG: http://www.linuxidc.com
# EMAIL: chanyipiaomiao@163.com
# Created: 2013-05-22
# Copyright: (c) LEO 2013
# Python: 2.7/2.4 皆可使用
#--------------------------------------------------------
from smtplib import SMTP
from email import MIMEText
from email import Header
from os.path import getsize
from sys import exit
from re import compile , IGNORECASE
#定义主机 帐号 密码 收件人 邮件主题
smtpserver = 'smtp.163.com'
sender = '帐号/发件人'
password = '密码'
receiver = ( '收件人1' , '收件人2' ,)
subject = u 'Web服务器Tomcat日志错误信息'
From = u 'xxx Web服务器'
To = u '服务器管理员'
#定义tomcat日志文件位置
tomcat_log = '/usr/local/tomcat/logs/catalina.out'
#该文件是用于记录上次读取日志文件的位置,执行脚本的用户要有创建该文件的权限
last_position_logfile = '/tmp/last_position.txt'
#匹配的错误信息关键字的正则表达式
pattern = compile (r 'Exception|^\t+\bat\b' ,IGNORECASE)
#发送邮件函数
def send_mail(error):
#定义邮件的头部信息
header = Header.Header
msg = MIMEText.MIMEText(error, 'plain' , 'utf-8' )
msg[ 'From' ] = header(From)
msg[ 'To' ] = header(To)
msg[ 'Subject' ] = header(subject + '\n' )
#连接SMTP服务器,然后发送信息
smtp = SMTP(smtpserver)
smtp.login(sender, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.close()
#读取上一次日志文件的读取位置
def get_last_position( file ):
try :
data = open ( file , 'r' )
last_position = data.readline()
if last_position:
last_position = int (last_position)
else :
last_position = 0
except :
last_position = 0
return last_position
#写入本次日志文件的本次位置
def write_this_position( file ,last_positon):
try :
data = open ( file , 'w' )
data.write( str (last_positon))
data.write( '\n' + "Don't Delete This File,It is Very important for Looking Tomcat Error Log !! \n" )
data.close()
except :
print "Can't Create File !" + file
exit()
#分析文件找出异常的行
def analysis_log( file ):
error_list = []
try :
data = open ( file , 'r' )
except :
exit()
 
last_position = get_last_position(last_position_logfile)
this_postion = getsize(tomcat_log)
if this_postion < last_position:
data.seek( 0 )
elif this_postion = = last_position:
exit()
elif this_postion > last_position:
data.seek(last_position)
for line in data:
if pattern.search(line):
error_list.append(line)
 
write_this_position(last_position_logfile,data.tell())
data.close()
return ''.join(error_list)
#调用发送邮件函数发送邮件
error_info = analysis_log(tomcat_log)
if error_info:
send_mail(error_info)

Tomcat错误日志发送邮件脚本下载

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2013年资料/5月/25日/收集Tomcat异常日志并发送邮件

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值