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 + '\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!

 

转载于:https://www.cnblogs.com/changbo/p/6783321.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值