最新消息:

python 加 scribe 监控文件变化

未分类 admin 3749浏览 0评论

下载pyinotify,解压 ./setup.py install

#!/usr/bin/env python
#-*- encoding:UTF-8 -*-

import os
import sys
from subprocess import *
from pyinotify import WatchManager,Notifier,ProcessEvent,IN_DELETE,IN_CREATE,IN_MODIFY
from scribe import scribe
from thrift.transport import TTransport, TSocket
from thrift.protocol import TBinaryProtocol

g_linesNum = 0
global client

# 发送消息
def PostMsg(lineStr):
        global client

        log_entry = scribe.LogEntry(category=sys.argv[3], message=lineStr)
        result = client.Log(messages=[log_entry])
# 事件处理
class EventHandler(ProcessEvent):
        def process_IN_MODIFY(self,event):
                global g_linesNum
                print "Modify file: %s " % os.path.join(event.path,event.name)

                linesNum = 0
                if (event.name == sys.argv[1]):
                        file = Popen("wc -l " + sys.argv[1],stdout=PIPE,shell=True).stdout
                        linesNum = int(file.readline().split()[0])
                        file.close()

                        if (linesNum > g_linesNum):
                                disLine = str(linesNum - g_linesNum)
                                file = Popen("tail -" + disLine  + ' ' + sys.argv[1],stdout=PIPE,shell=True).stdout
                                linesStr = file.readlines()
                                file.close()

                                for lineStr in linesStr:
                                        PostMsg(lineStr)
                        g_linesNum = linesNum

        def process_IN_DELETE(self,event):
                print "Delete file: %s " % os.path.join(event.path,event.name)

        def process_IN_CREATE(self,event):
                print "Create file: %s " % os.path.join(event.path,event.name)

# 初始化处理
def ReadFile(path):
        global g_linesNum

        for lineStr in open(sys.argv[1],'rb'):
                g_linesNum += 1
                PostMsg(lineStr)
# 监控文件目录变化
def FSMonitor(path='.'):
        wm = WatchManager()
        mask = IN_MODIFY
        notifier = Notifier(wm,EventHandler())
        wm.add_watch(path,mask,rec=True)
        while True:
                try:
                        notifier.process_events()
                        if notifier.check_events():
                                notifier.read_events()
                except KeyboardInterrupt:
                        notifier.stop()
                        break
# 主程序
if __name__ == "__main__":
        global client

        if (len(sys.argv) < 4):
                print "Usage:./watcher.py watchFileName host category"
                sys.exit(-1)

        host_port = sys.argv[2].split(':')
        host = host_port[0]
        if len(host_port) > 1:
                port = int(host_port[1])
        else:
                port = 1463

        socket = TSocket.TSocket(host=host,port=port)
        transport = TTransport.TFramedTransport(socket)
        protocol = TBinaryProtocol.TBinaryProtocol(trans=transport, strictRead=False, strictWrite=False)
        client = scribe.Client(iprot=protocol, oprot=protocol)

        transport.open()
  ReadFile(sys.argv[1])
        FSMonitor()

        transport.close()

转载请注明:爱开源 » python 加 scribe 监控文件变化

您必须 登录 才能发表评论!