Skip to content

Instantly share code, notes, and snippets.

@tianweiliu
Forked from marcelom/pysyslog.py
Last active October 26, 2021 15:08
Show Gist options
  • Save tianweiliu/53e26e198d6d828a6b9b to your computer and use it in GitHub Desktop.
Save tianweiliu/53e26e198d6d828a6b9b to your computer and use it in GitHub Desktop.
Tiny Python Syslog Server
#!/usr/bin/env python
## Tiny Syslog Server in Python.
##
## This is a tiny syslog server that is able to receive UDP based syslog
## entries on a specified port and save them to a file.
## That's it... it does nothing else...
## There are a few configuration parameters.
LOG_FILE = 'yourlogfile.log'
HOST = '0.0.0.0'
TCP_PORT = 514
UDP_PORT = 1514
#
# NO USER SERVICEABLE PARTS BELOW HERE...
#
import logging
import time
import threading
import SocketServer
listening = False
logging.basicConfig(level=logging.INFO, format='%(message)s', datefmt='', filename=LOG_FILE, filemode='a')
class SyslogUDPHandler(SocketServer.BaseRequestHandler):
def handle(self):
# data = bytes.decode(self.request[0].strip())
data = self.request[0].strip()
# socket = self.request[1]
print( "%s : " % self.client_address[0], str(data))
logging.info(str(data))
class SyslogTCPHandler(SocketServer.BaseRequestHandler):
End = '\n'
def join_data(self, total_data):
final_data = ''.join(total_data)
for data in final_data.split(self.End):
print( "%s : " % self.client_address[0], str(data))
logging.info(str(data))
def handle(self):
total_data = []
while listening:
data = self.request.recv(8192).strip()
if not data: break
if self.End in data:
split_index = data.rfind(self.End)
total_data.append(data[:split_index])
self.join_data(total_data)
del total_data[:]
total_data.append(data[split_index + 1:])
else:
total_data.append(data)
if len(total_data) > 0:
self.join_data(total_data)
# logging.info(str(data))
if __name__ == "__main__":
listening = True
try:
# UDP server
udpServer = SocketServer.UDPServer((HOST, UDP_PORT), SyslogUDPHandler)
udpThread = threading.Thread(target=udpServer.serve_forever)
udpThread.daemon = True
udpThread.start()
# udpServer.serve_forever(poll_interval=0.5)
# TCP server
tcpServer = SocketServer.TCPServer((HOST, TCP_PORT), SyslogTCPHandler)
tcpThread = threading.Thread(target=tcpServer.serve_forever)
tcpThread.daemon = True
tcpThread.start()
while True:
time.sleep(1)
# tcpServer.serve_forever(poll_interval=0.5)
except (IOError, SystemExit):
raise
except KeyboardInterrupt:
listening = False
udpServer.shutdown()
udpServer.server_close()
tcpServer.shutdown()
tcpServer.server_close()
print ("Crtl+C Pressed. Shutting down.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment