monitor-daemon.sh
持续监控server.log文件,抓取到Error关键字后执行Python脚本发送邮件,并记录发送日志。
nohup tail -f /usr/local/tomcat/logs/server.log | grep --line-buffered Error | awk '{print $4; fflush()}' | while read msg ; do /usr/bin/python3 mail.py $msg; done & >> mail.log
命令解释:
– tail -f 用于持续监控日志
– grep 抓取相关错误信息
– awk 获取错误信息中的关键信息
– while read msg 循环监测是否有消息过来
mail.py
依赖python3,使用yum install python34安装
#!/usr/bin/python3
import sys
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
host = "smtp.sina.cn"
port = 465
sender = "a858134843@sina.com"
receivers = ["yuki.cheng@all-dream.com"]
message = MIMEText(sys.argv[1], 'plain', 'utf-8')
message['Subject'] = "糖果编程服务异常"
message['From'] = formataddr(["糖果编程",sender])
message['To'] = formataddr(["程宇",receivers[0]])
server=smtplib.SMTP_SSL(host, port)
server.login(sender, "858134843")
server.sendmail(sender, receivers, message.as_string())
server.quit()
print(sys.argv[1])
使用说明
直接执行./monitor-daemon.sh即可
发表评论
抢沙发~