Chinaunix首页 | 论坛 | 博客
  • 博客访问: 628870
  • 博文数量: 149
  • 博客积分: 3901
  • 博客等级: 中校
  • 技术积分: 1558
  • 用 户 组: 普通用户
  • 注册时间: 2009-02-16 14:33
文章分类

全部博文(149)

文章存档

2014年(2)

2013年(10)

2012年(32)

2011年(21)

2010年(84)

分类: Python/Ruby

2012-02-10 20:21:35


使用 :
测试 代码中的 一些 sleep 都去除 就可以用了 .
  1. #encoding: utf-8
  2. import sys,time,os
  3. import threading
  4. import pyinotify

  5. # 全局变量
  6. # 一个 job ,只能 有一个 TAIL_JOB 线程
  7. GLOBAL_MEM = '' # buff
  8. TAIL_JOB = None

  9. class ThreadTail(threading.Thread):
  10.     
  11.     def __init__(self,log,sep='\n',read_seek=-1,buff_size=12,blocking=-1,sleep=2):
  12.         threading.Thread.__init__(self)
  13.         self.log = log # 日志文件名
  14.         self.sep = sep # 读取文件分割名称
  15.         self.read_seek = read_seek # 开始读取 数据位置,默认 文件开始
  16.         self.buff_size = buff_size # 读取 缓冲区
  17.         self.blocking = blocking # 文件 读取到最后 是否 拥塞/-1 ; 或等待 n/num 个sleep 时间
  18.         self.sleep = sleep # 文件读取到最后 sleep 单元时间
  19.         self._isStop = False

  20.     def isStop(self,isstop):
  21.         self._isStop = isstop

  22.     def run(self):
  23.         global GLOBAL_MEM
  24.         if self.read_seek == -1 :
  25.             self.read_seek = os.stat(self.log)[6]
  26.         try :
  27.             log_file=open(self.log,'r')
  28.             print "read ~ %s " % self.log
  29.         except : return
  30.         log_file.seek(self.read_seek)
  31.         blocking_num = 0
  32.         while self._isStop == False :
  33.             time.sleep(2)
  34.             block = log_file.read(self.buff_size)
  35.             if block :
  36.                 if block[-1] == self.sep : block = block[:-1]
  37.                 self.read_seek += len(block)
  38.             
  39.                 buffs = block.split(self.sep)
  40.                 if len(buffs)!=1:
  41.                     GLOBAL_MEM += buffs[0]
  42.                     send(GLOBAL_MEM)

  43.                     for t_buff in buffs[1:-1] :
  44.                         send(t_buff)

  45.                     GLOBAL_MEM = buffs[-1]
  46.                 else :
  47.                     GLOBAL_MEM += buffs[0]
  48.             else :
  49.                 time.sleep(self.sleep)
  50.                 log_file.seek(self.read_seek )
  51.                 blocking_num += 1
  52.                 if self.blocking != -1 and blocking_num > self.blocking :
  53.                     send(GLOBAL_MEM)
  54.                     print 'end ~ %s' % self.log
  55.                     break



  56. def send(row):
  57.     print row


  58. class EventHandler(pyinotify.ProcessEvent):
  59.     
  60.     def __init__(self,log_file):
  61.         pyinotify.ProcessEvent.__init__(self)
  62.         self.log_file = log_file
  63.         self.dir,self.filename = os.path.split(log_file)
  64.         self.isMoveLog = False
  65.          
  66.         
  67.     def process_IN_MOVED_FROM(self, event):
  68.         global TAIL_JOB
  69.         if event.name == self.filename :
  70.             print "process_IN_MOVED_FROM %s " % event.name
  71.             TAIL_JOB.isStop(True)
  72.             self.isMoveLog = True

  73.     def process_IN_MOVED_TO(self, event):
  74.         global TAIL_JOB
  75.         if self.isMoveLog :
  76.             print "process_IN_MOVED_TO %s " % event.name
  77.             if TAIL_JOB :
  78.                 TAIL_JOB.isStop(True)
  79.             ttail = ThreadTail(self.dir+'/'+event.name,read_seek= TAIL_JOB.read_seek-1,blocking=1)
  80.             ttail.start()
  81.         
  82.             TAIL_JOB = ThreadTail(self.log_file,read_seek=0)
  83.             TAIL_JOB.start()

  84.     def process_IN_CREATE(self, event):
  85.         global TAIL_JOB
  86.         if self.filename == event.name :
  87.             print "process_IN_CREATE %s " % event.name
  88.             if TAIL_JOB : TAIL_JOB.isStop(True)
  89.             TAIL_JOB = ThreadTail(log_file,read_seek=0)
  90.             TAIL_JOB.start()


  91.           
  92. # /opt/sohumc/bin/python read_log.py /tmp/t.txt
  93. # 动态 收集 日志
  94. # 支持即使 有 mv /tmp/t.txt /tmp/x.txt 的变动,一样能收集
  95. # 如有需要 只需 重写 下 send 方法
  96. if __name__ == '__main__':
  97.     log_file = sys.argv[1]
  98.     dir,filename = os.path.split(log_file)

  99.     handler = EventHandler(log_file)
  100.     wm = pyinotify.WatchManager()
  101.     mask = pyinotify.IN_MOVED_FROM | pyinotify.IN_MOVED_TO | pyinotify.IN_CREATE
  102.     wdd = wm.add_watch( dir , mask, auto_add=True )
  103.     notifier = pyinotify.Notifier(wm, handler)

  104.     TAIL_JOB = ThreadTail(log_file,read_seek=0)
  105.     try :
  106.         TAIL_JOB.start()
  107.     except :
  108.         print "not open %s " % log_file
  109.     
  110.     print 'test~'

  111.     notifier.loop()









阅读(1379) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~