Skip to content

Instantly share code, notes, and snippets.

@adimania
Last active December 14, 2015 06:59
Show Gist options
  • Save adimania/5046792 to your computer and use it in GitHub Desktop.
Save adimania/5046792 to your computer and use it in GitHub Desktop.
A simple tailer to send the data to remote machine using UDP. It was originally written to ship logs to logstash but it can be used to ship any text anywhere.
#!/usr/bin/python
from time import sleep
import socket
log=open("somefile.log","r")
log.readlines()
pos=log.tell() # This is the last line of the file
while True:
line=log.readline()
if line:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # I like UDP to send the logs.
sock.sendto(line, ("localhost", 7897))
pos=log.tell() # save the current position so that tailer can resume from here.
else:
sleep(1) # I don't want to check too soon. Sleep for 1 sec.
log=open("somefile.log","r")
log.seek(pos) # Resume the position saved last.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment