#!/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)

监控邮件:

最新文章

  1. nyoj 473 A^B Problem
  2. Ibatis 使用心得
  3. windows 2012 r2下安装sharepoint 2013错误解决
  4. 数据库热备份工具innobackupex的安装
  5. PHP热身
  6. HackerRank &quot;Minimum Penalty Path&quot;
  7. mariadb的explain分析及InnoDB存储引擎
  8. ReactiveCocoa 谈谈RACMulticastConnection
  9. hdu1047(模拟大量的循环添加)
  10. C语言漫谈(二) 图像显示 Windows和Linux
  11. “jquery于each方法和选择”学习笔记
  12. MySQL &#183; 引擎特性 &#183; InnoDB奔溃恢复
  13. 一起来学linux:网络命令
  14. [COGS 2401]Time is Money
  15. Windows系统pip安装whl包
  16. xpath | 计算两个节点集
  17. Win10安装sqlserver2014打开显示黑色界面,mardown打开显示报错
  18. chrome 下 input[file] 元素cursor设置pointer不生效的解决
  19. docker 的使用
  20. css中 ~的作用

热门文章

  1. java.lang.IllegalStateException: ApplicationEventMulticaster not initialized
  2. PowerDesigner16.5物理数据表生成C#实体类Model
  3. RabbitMQ(四):RPC的实现
  4. python+soket实现 TCP 协议的客户/服务端中文(自动回复)聊天程序
  5. CAN总线的显性电平为什么能覆盖隐性电平?
  6. 180804-Spring之动态注册bean
  7. Post请求和Get请求;@RequestBody和@RequestParam
  8. php_package v2.7发布了 宋正河作品
  9. Linux命令的那些事(一)
  10. 一个IT男的表白