码迷,mamicode.com
首页 > 编程语言 > 详细

python 实时分析日志

时间:2017-04-28 22:18:48      阅读:409      评论:0      收藏:0      [点我收藏+]

标签:spec   ast   模拟   read   nginx log   blog   ==   ber   where   

#!/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 + \n)
            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!

 

python 实时分析日志

标签:spec   ast   模拟   read   nginx log   blog   ==   ber   where   

原文地址:http://www.cnblogs.com/changbo/p/6783321.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!