#!/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)
相关推荐:
seo线是什么意思,seo是什么意思 老板让兼职seo
亚马逊seo是什么公司的,“亚马逊” ,你身边的ai写作助手
seo学什么技术好,seo学什么技术好就业 聊城网站建设策划书模板
seo用什么法宝,列出5种seo赚钱方式 ,ai新闻写作的缺点
Zkept:重塑未来生活的智能科技革新,中山找网站建设
SEO收费如何选择合适的SEO服务,提升网站排名并增加曝光度,饰品营销推广文案范文
香港VPS访问日本网络是否畅通无阻?深度解析网络边界问题!
seo培训学什么好,seo专业培训学费多少钱 南充百度网站推广
seo帽子是什么,seo帽子是什么意思 台州网站推广威欣hfqjwl下拉
VPS MySQL频繁重启问题深度解析与实用解决方案
seo论坛什么意思,seowhy论坛 饮品营销推广策划方案模板
seo用到什么软件,做seo需要用到什么软件 ,AI锤
SEO是什么粉底液,seo是什么seo怎么做 ,ai文字设计 价格
SEO笔记:如何打造高效的SEO策略提升网站排名,新昌县网站建设推广
seo站长什么意思,站长工具 - seo综合查询 ,ai描边字
揭秘汽车费用之谜,GPS定位系统的费用解析
SEO站内优化:提升网站排名的核心策略,潍坊营销型网站建设费用
重磅揭秘,VPS售后客服电话号码快速查询指南!
seo网站关键字排名优化,网站seo关键词 ,环形拼图海报AI咋做
seo搜索矩阵平台是什么,seo搜索工具 ,小茗ai
二级泛站群,zblog二级泛站群 ,明星ai资源网站
揭秘英诺VPS的神秘面纱,究竟是何方神圣?
怎么样提高机房网络速度?
Conoha VPS控制台操作指南,一步步带你掌握连接与控制功能
seo智能优化是什么,seo自动优化工具 ,ai描边的形状
VPS NAT开启UDP 80端口,操作指南与详细解析
seo有什么工具包,seo常用软件 律师网站推广的广告语
揭秘流畅观影秘籍,无忧VPS视频观看体验,告别卡顿烦恼!
SEO是什么防晒口罩,seo是什么防晒口罩品牌 ,ai论文大学生
SEO模块:提升网站排名,驾驭数字营销未来,seo连字符
seo外链用什么形式,seo外链用什么形式好 saas网站建设优劣
关键词优化手机,关键词优化手机软件 东营网站建设知识培训
seo有什么证件,seo需要具备什么知识 ,ai196112
SEO招标:如何通过专业SEO服务助力企业脱颖而出,昆明短视频seo定制
SEO实现:如何通过SEO优化提升网站流量与排名,优化网站快速排名软件
揭秘VPS的IP变换真相,真的会换IP吗?
探索游戏迷们的最佳伙伴,优质游戏VPS推荐!
什么是蜘蛛弛?揭秘这个SEO优化背后的神秘工具,济宁网站建设机构名单查询
seo链接有什么好处,seo链接有什么好处和作用 可靠的福州seo策划
为什么越来越多的人选择在知乎发布文章?这里面的机会你可能还没发现!,内江商业推广招聘网站大全
SEO要好,网站流量翻倍的关键秘诀,小说网站建设银行
seo涉及什么内容,seo主要包括 ,ai模拟拍照
ups机房太热会怎么样?
SEO导航:助力网站提升流量与排名的必备工具,安康高端营销推广价格
移动公司买房做机房怎么样?
seo是什么游轮,seo you ,ai虚拟演员
seo是什么介入术式,seo术语解释 ,ai住
seo是什么样的人,seo具体是什么 泰安抖音seo排名推荐
“洗稿技巧如何让你的文章脱颖而出,轻松提升内容创作水平!”,舞钢seo优化软件
seo稿件是什么意思,seo文章写作要求 ,ai胡涂涂