python解析日志并邮件告警

#!/usr/bin/env python
# coding=gbk
from os.path import getsize
import sys
import re
import time
import datetime
import os
from re import compile,IGNORECASE
import smtplib
from email.MIMEText import MIMEText
import socket
reload(sys)
sys.setdefaultencoding('gbk')

##日志文件
log = r'tomcat.log'
##每次执行后的位置,保证不重复统计
last_position_logfile = r'pos.txt'

##获取保存的位置
def get_last_position(file):
    data = open(file,'r')
    last_position = data.readline()
    if last_position:
        last_position = int(last_position)
    else:
        last_position = 0
    data.close()
    return last_position

##保存上次读写后的位置
def write_this_position(file,last_position):
    data = open(file,'w')
    data.write(str(last_position))
    data.close()

##获取主机ip
def getIpaddr():
    myname = socket.getfqdn(socket.gethostname())
    myaddr = socket.gethostbyname(myname)
    return myaddr

##解析日志文件
def anlysis_log(file):
    ##设置超时告警阈值
    timeout_count_threshold=5
    msg_result={}
    currentTime=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
    ip = getIpaddr()
    data = open(file,'r')
    last_position = get_last_position(last_position_logfile)
    this_position = getsize(log)
    ##快速定位位置
    if this_position < last_position:
        data.seek(0)
    elif this_position == last_position:
        exit()
    elif this_position > last_position:
        data.seek(last_position)

    ##循环读取日志行
    for line in data.readlines():
        ##正则匹配
        match=re.search(r'(ASClient)(.*)(elapsed=\d{5})(.*)(method=.*)(.*)(servicename=.*),',line)
        if ( not match ):
            pass
        else:
            msg_method=match.group(5).split(',')[0].split('=')[1]
            msg_service=match.group(7).split(',')[0].split('=')[1]
            if msg_service not in msg_result:
                msg_result[msg_service]={'timeout_total':0}
            if msg_method not in msg_result[msg_service]:
                msg_result[msg_service][msg_method]={'timeout':0}
            msg_result[msg_service]['timeout_total']+=1
            msg_result[msg_service][msg_method]['timeout']+=1   
    for (k,v) in msg_result.items():
        if ( (k=='test') and v['timeout_total']>timeout_count_threshold ):
            msg_report=''
            for (x,y) in v.items():
                if(x != 'timeout_total'):
                    pp='  %s [超时次数:%s],'% (x,y['timeout'])
                    msg_report=msg_report+pp          
            content='%s超时次数为%s, IP:%s'%(k.upper(),v['timeout_total'],ip)

            sendmail('IP:%s, %s调用超时次数为:%s, 超时阀值为10秒, 具体的method如下:<br>%s'%(ip,k,v['timeout_total'],msg_report.replace(',','<br>')),'[报警]系统%s超时'%k.upper())

    ##把当前位置保存到文件
    write_this_position(last_position_logfile,data.tell())
    data.close()
    print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))

def sendmail(content,subject):
    url = r"smtp.163.com"
    sender = 'kong@163.com'
    receivers = ['kong@163.com']
    msg = MIMEText(content,'html','gbk')
    msg['Subject'] = subject
    msg['To'] = ",".join(receivers)
    conn = smtplib.SMTP(url,25)
    conn.sendmail(sender,receivers,msg.as_string())
    conn.close()

anlysis_log(log)
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值