#!/usr/bin/env python
#coding:utf8
import re
import os
import time
import smtplib
import socket
import fcntl
import struct
from email.mime.text import MIMEText
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
def sendemail(subject,msg,fromemail,emailpasswd,toemail):
'''实现发送邮件功能函数'''
_user = fromemail
_pwd = emailpasswd
_to = toemail
nowtime = time.strftime('%Y-%m-%d %H:%M:%S')
msg = MIMEText(msg)
msg["Subject"] = subject
msg["From"] = _user
msg["To"] = _to
try:
s = smtplib.SMTP_SSL('smtp.qq.com', 465)
s.login(_user, _pwd)
s.sendmail(_user, _to, msg.as_string())
s.quit()
print "[%s]INFO:Email send Success!" % nowtime
except smtplib.SMTPException,e:
print "[%s]ERROR:Email send Falied,%s" % (nowtime,e)
def matchkeyword(pattern,alertlogfile):
'''实现匹配关键字函数'''
re.compile(pattern)
posfile = "/tmp/posfile"
if not os.path.exists(posfile):
os.mknod(posfile)
if not os.path.getsize(posfile):
with open(posfile,'w') as fobj:
fobj.write('0')
#打开文件
f = open(alertlogfile,'r')
#移动到文件结尾
f.seek(0,2)
#读出文件所在的字节位置
endpos = f.tell()
#移动到文件的开头
with open(posfile,'r') as fobj:
startpos = int(fobj.read())
f.seek(startpos)
if endpos-startpos > 0:
data = f.read(endpos-startpos)
f.close()
with open(posfile,'w') as fobj:
fobj.write(str(endpos))
m = re.findall(pattern, data,re.IGNORECASE)
if m:
content = '\n'.join(m)
return content
else:
return ''
if __name__ == '__main__':
local_ip = get_ip_address('eth0')
subject = '服务器[%s]日志报警了!' % local_ip
fromemail = 'xxxxxxxx@qq.com'
#emailpasswd为QQ邮箱的授权码
emailpasswd = 'mdkuasfhnjbrbhdj'
toemail = 'xxxxxx@qq.com'
alertlogfile = "/data/mysql/mysql_3306/log/error.log"
#pattern = ".*\[Warning\].*\s|.*\[Note\].*\s"
pattern = ".*Warning.*\s|.*error.*\s"
while True:
content = matchkeyword(pattern, alertlogfile)
if content:
sendemail(subject, content, fromemail, emailpasswd, toemail) 0
0
相关文章
如何搭建多实例mysql环境_mysql多环境共存方案
mysql 自动生成SQL工具_mysql开发效率提升
mysql如何实现学生管理系统_mysql初级项目思路
mysql 开发测试工具推荐_mysql调试类库整理
如何用mysql实现客户管理系统_mysql客户信息管理方法
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
相关标签:
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门AI工具
相关专题
2026年最新赚钱平台入口汇总,涵盖任务众包、内容创作、电商运营、技能变现等多类正规渠道,助你轻松开启副业增收之路。阅读专题下面的文章了解更多详细内容。
76
2026.01.31
想找真正免费又无套路的漫画App?本合集精选多款永久免费、资源丰富、无广告干扰的优质漫画应用,涵盖国漫、日漫、韩漫及经典老番,满足各类阅读需求。阅读专题下面的文章了解更多详细内容。
67
2026.01.31
想找免费又资源丰富的漫画网站?本合集精选2025-2026年热门平台,涵盖国漫、日漫、韩漫等多类型作品,支持高清流畅阅读与离线缓存。阅读专题下面的文章了解更多详细内容。
19
2026.01.31
热门下载
相关下载
精品课程






