tomcat错误日志监控脚本
2024-08-25 22:11:41
#!/usr/bin/env python
#-*-coding:utf-8-*-
#CreateDate:2017/04/14
#Author:Eivll0m
#ScriptName:monitor-log.py
#Crontab:*/5 * * * * /app/sbin/monitor-log.py &>/dev/null import os
import time
import stat
import socket
import smtplib
from email.mime.text import MIMEText
#from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.utils import parseaddr, formataddr def formatAddr(s):
name, addr = parseaddr(s)
return formataddr((Header(name, 'utf-8').encode(), addr))
def sendMail(body,subject):
smtp_server = 'smtp.exmail.qq.com'
from_mail = 'xxx@xxxx.com'
mail_pass = 'password'
to_mail = ['aaa@xxx.com','bbb@xxx.com','ccc@xxx.com']
cc_mail = ['ddd@xxx.com','eee@xxx.com','fff@xxx.com','ggg@xxx.com','hhh@xxx.com']
msg = MIMEText(body,'html','utf-8')
msg['From'] = formatAddr('日志监控 <%s>' % from_mail).encode()
msg['To'] = ','.join(to_mail)
msg['Cc'] = ','.join(cc_mail)
msg['Subject'] = Header(subject, 'utf-8').encode()
try:
s = smtplib.SMTP()
s.connect(smtp_server, "")
s.login(from_mail, mail_pass)
s.sendmail(from_mail, to_mail+cc_mail, msg.as_string())
s.quit()
except smtplib.SMTPException as e:
print "Error: %s" % e def GetText(file):
cmd = "awk -vf='%s' 'BEGIN{while(getline < f){j++;if(/ERROR/)i=j}}NR>=i' %s" %(file,file)
text = os.popen(cmd).readlines()
return ''.join(text) def CheckFileStat(file):
FileStats = os.stat(file)
ModifyTime = time.mktime(time.strptime(time.ctime(FileStats[stat.ST_MTIME]),"%a %b %d %H:%M:%S %Y")) + 300
LocalTime = time.time()
if LocalTime < ModifyTime:
return True
else:
return False if __name__ == "__main__":
logs = ['/app/log/channelmanage/channelerror.log','/app/log/assetmanage/error.log']
LocalIP = socket.gethostbyname(socket.gethostname())
for log in logs:
if CheckFileStat(log):
sj = log.split('/')[3] + '日志报错' + '【主机IP:' + LocalIP + '】'
body = GetText(log)
sendMail(body,sj)
监控邮件:
最新文章
- nyoj 473 A^B Problem
- Ibatis 使用心得
- windows 2012 r2下安装sharepoint 2013错误解决
- 数据库热备份工具innobackupex的安装
- PHP热身
- HackerRank ";Minimum Penalty Path";
- mariadb的explain分析及InnoDB存储引擎
- ReactiveCocoa 谈谈RACMulticastConnection
- hdu1047(模拟大量的循环添加)
- C语言漫谈(二) 图像显示 Windows和Linux
- “jquery于each方法和选择”学习笔记
- MySQL &#183; 引擎特性 &#183; InnoDB奔溃恢复
- 一起来学linux:网络命令
- [COGS 2401]Time is Money
- Windows系统pip安装whl包
- xpath | 计算两个节点集
- Win10安装sqlserver2014打开显示黑色界面,mardown打开显示报错
- chrome 下 input[file] 元素cursor设置pointer不生效的解决
- docker 的使用
- css中 ~的作用
热门文章
- java.lang.IllegalStateException: ApplicationEventMulticaster not initialized
- PowerDesigner16.5物理数据表生成C#实体类Model
- RabbitMQ(四):RPC的实现
- python+soket实现 TCP 协议的客户/服务端中文(自动回复)聊天程序
- CAN总线的显性电平为什么能覆盖隐性电平?
- 180804-Spring之动态注册bean
- Post请求和Get请求;@RequestBody和@RequestParam
- php_package v2.7发布了 宋正河作品
- Linux命令的那些事(一)
- 一个IT男的表白