• python 实时分析日志


    #!/usr/local/bin/python3
    # coding:utf-8
    
    # ====================================================
    # Author: chang - EMail:changbo@hmg100.com
    # Last modified: 2017-4-28
    # Filename: nginxanalysis.py
    # Description: real time analysis nginx log,base time, os
    # blog:http://www.cnblogs.com/changbo
    # ====================================================
    
    """
    需求:每隔1分钟读取nginx日志文件
    """
    
    
    import time
    import os
    
    
    # write log offset
    def writeoffset(number):
        with open('offset.txt', 'w+') as f3:
            f3.write(number)
            f3.flush()
    
    
    # get log offset
    def getoffset():
        with open('offset.txt') as f2:
            offset = f2.readline()
            return offset
    
    
    # online analysis log
    def analysislog():
        with open('access.log') as f1:
            while True:
                # get offset
                lastoffset = getoffset()
                # jump the Specify log line
                f1.seek(int(lastoffset))
                # 获取该行偏移量
                where = f1.tell()
                line = f1.readline()
                writeoffset(str(where))
                if not line:
                    time.sleep(10)
                    f1.seek(where)
                else:
                    # 处理该行,并获取改行的偏移量且写入文件
                    print(line)
                    nowoffset = f1.tell()
                    writeoffset(str(nowoffset))
    
    if __name__ == '__main__':
        if not os.path.exists('offset.txt'):
            with open("offset.txt", 'w') as f:
                f.write('0')
    
        analysislog()

    模拟日志脚本

    #!/usr/local/bin/python3
    # coding:utf-8
    
    # ====================================================
    # Author: chang - EMail:changbo@hmg100.com
    # Last modified: 2017-4-28
    # Filename: testlog.py
    # Description: create log files,base time, os
    # blog:http://www.cnblogs.com/changbo
    # ====================================================
    
    import time
    import os
    
    
    def writecount(number):
        with open('count.txt', 'w+') as f1:
            f1.write(number)
            f1.flush()
    
    
    def getcount():
        with open('count.txt') as f2:
            line = f2.readline()
            return line
    
    
    def writetest():
        with open('access.log', 'a+') as f:
            count = getcount()
            count = int(count)
            while True:
                nowtime = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
                eglogtxt = "this is %d line" % count
                time.sleep(3)
                f.write(nowtime + " " + eglogtxt + '
    ')
                count += 1
                f.flush()
                writecount(str(count))
    
    if __name__ == '__main__':
        if not os.path.exists('count.txt'):
            with open("count.txt", 'w') as f:
                f.write('1')
    
        writetest()

    模拟日志切割过程中初始化脚本参数

    cp access.log access20170408.log  && echo > access.log && echo '0'> offset.txt

    成功

    End!

  • 相关阅读:
    玩转MySQL之Linux下的简单操作(服务启动与关闭、启动与关闭、查看版本)
    玩转MySQL之Linux下修改默认编码
    机器学习算法及应用领域相关的中国大牛
    [转载]Python 包管理工具解惑
    Vim常用操作和快捷键技巧总结
    [转载]那些C++牛人的博客
    [转载]学习c/c++的好网站
    [转载]C++内存管理
    [转载]SQL数据库如何加快查询速度
    [转载]Python3.x和Python2.x的区别
  • 原文地址:https://www.cnblogs.com/changbo/p/6783321.html
Copyright © 2020-2023  润新知