-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmonitor.py
More file actions
32 lines (24 loc) · 991 Bytes
/
monitor.py
File metadata and controls
32 lines (24 loc) · 991 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import config
import pyinotify
import os
import pika
class LogMonitor(pyinotify.ProcessEvent):
def __init__(self, filename, channel):
self.fh = open(filename, 'r')
self.channel = channel
file_size = os.stat(filename).st_size
self.fh.seek(file_size)
def process_IN_MODIFY(self, event):
line = self.fh.readline()
self.channel.basic_publish(exchange='logs', routing_key='', body=line)
# TODO - should handle logrotate, deletion, creation of a new file
if __name__ == "__main__":
rabbit_connection = pika.BlockingConnection(pika.ConnectionParameters(host=config.rabbitmq['host']))
channel = rabbit_connection.channel()
channel.exchange_declare(exchange='logs', type='fanout')
wm = pyinotify.WatchManager()
mask = pyinotify.IN_MODIFY
handler = LogMonitor(config.log_file, channel)
notifier = pyinotify.Notifier(wm, handler)
wdd = wm.add_watch(config.log_file, mask, rec=True)
notifier.loop()