美文网首页
抓取项目日志

抓取项目日志

作者: Canon_2020 | 来源:发表于2020-04-20 09:10 被阅读0次
    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    # @Date    : 2018-10-16 09:00:00
    # @Author  : Canon
    # @Link    : https://www.python.org
    # @Version : 3.6.5
    
    """
    抓取项目日志进行报警
    """
    
    import os
    import re
    import socket
    import time
    
    
    # 定义日志匹配函数
    def Log_re(mode,data):
        # 根据传递过来的模式及内网,过滤出相应的内容
        m = re.findall(mode,data, re.I|re.M)
        if m:
            # 将正则匹配得到的数组,以html的换行符"<br/>"作为分割符,连接成字符串
            return m 
        else:
            return ''
    
    
    # 获取服务器信息
    hostname = socket.gethostname()
    ip = socket.gethostbyname(hostname)
    year = time.strftime('%Y')
    date = time.strftime('%Y-%m-%d %H:%M')
    
    
    # 定义日志检查函数
    def check_log(log_path):
        """
        检查日志是否报错, 例: check_log("/home/tomcat/PG/logs/")
        :param log_path: 日志目录路径, 注意加上最后一个"/"
        """
        # 遍历给定路径下的文件及子目录
        for root, dirs, files in os.walk(log_path, topdown=False):
            # 根据给定的不同路径分割出不同的项目名称
            if "/home/tomcat" in root:
                t = os.path.split(root)[0]
                t2 = os.path.split(t)[0]
                dirname = os.path.split(t2)[1]
            else:
                t = os.path.split(root)[0]
                dirname = os.path.split(t)[1]
            # 循环给定目录下的文件
            for name in files:
                # 定义存放日志读取位置的文件
                pos_file = "logcheck/{0}-{1}.line".format(dirname,name)
                # 定义用于抓取匹配的文件名
                file_name = os.path.join(root, name)
                # 判断文件名是否符合相应的条件
                if ( os.path.splitext(file_name)[1] == ".log" or os.path.splitext(file_name)[1] == ".out" ) and year not in os.path.splitext(file_name)[0]:
                    with open(file_name,'r') as log_file:
                        if os.path.exists(pos_file):
                            with open(pos_file,'r') as lf:
                                # 从 pos_file 中读取日志位置信息
                                start_pos=lf.read()
                                start_pos=int(start_pos)
                                # 将日志文件的读取光标定位到文件末尾并将位置信息记录为 end_pos
                                log_file.seek(0,2)
                                end_pos=log_file.tell()
                        else:
                            log_file.seek(0,2)
                            end_pos=log_file.tell()
                            start_pos=log_file.tell()
                        # 当结束位置大于开始位置时,执行日志匹配
                        if end_pos > start_pos:
                            # 将日志文件的读取光标移动到 start_post 的位置开始读取
                            log_file.seek(start_pos,0)
                            log_content=log_file.read()
                            pattern = ".*exception.*\s|.*error.*\s"
                            content=Log_re(pattern, log_content)
                            # 如果匹配内容不为空,则发送报警邮件
                            if content:
                                Error_nums=len(content)
                                result='<br/>'.join(content)
                                # subject="%s-%s ERROR:%s have errors." % (hostname,ip,file_name)
                                subject="{0} Alert in MIRROR! {1} have {2} errors in the last 10 minutes.".format(date,file_name,Error_nums)
                                # print(subject)
                                # 定义收件人
                                to=["ron@oceanpayment.com.cn","lomo@oceanpayment.com.cn","dick@oceanpayment.com.cn","carol@oceanpayment.com.cn","canon@oceanpayment.com.cn"]
                                # Send_email(subject,result,to)
                                print(result)
                        else:
                            pass
                        # 保存日志匹配的结束位置到pos_file
                        with open(pos_file,'w') as lf:
                            lf.write(str(end_pos))
    
    
    # check_log("/usr/local/logs/AdminSystem/")
    # check_log("/home/tomcat/AS/logs/")
    
    

    相关文章

      网友评论

          本文标题:抓取项目日志

          本文链接:https://www.haomeiwen.com/subject/vzqtwctx.html