Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 = 'youlogfile.log'
HOST, PORT = "0.0.0.0", 514
#
# NO USER SERVICEABLE PARTS BELOW HERE...
#
import logging
import SocketServer
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())
socket = self.request[1]
print( "%s : " % self.client_address[0], str(data))
logging.info(str(data))
if __name__ == "__main__":
try:
server = SocketServer.UDPServer((HOST,PORT), SyslogUDPHandler)
server.serve_forever(poll_interval=0.5)
except (IOError, SystemExit):
raise
except KeyboardInterrupt:
print ("Crtl+C Pressed. Shutting down.")
@iobear-zz

This comment has been minimized.

Copy link

@iobear-zz iobear-zz commented Mar 27, 2013

Thanks, I've used it as a syslog frontend to redis.

https://github.com/iobear/beewatch/blob/master/bin/psyslog.py

@seefor

This comment has been minimized.

Copy link

@seefor seefor commented Jan 29, 2015

Great Script BTW
Question, instead of Crtl+C to end, how would you have it end in 30 minutes?

@somma

This comment has been minimized.

Copy link

@somma somma commented Apr 20, 2015

Thank's for sharing nice code. 😃

@ilmarh

This comment has been minimized.

Copy link

@ilmarh ilmarh commented Jun 5, 2015

Thanks!

@pickeld

This comment has been minimized.

Copy link

@pickeld pickeld commented Jul 18, 2015

Thanks!!

@lidaling

This comment has been minimized.

Copy link

@lidaling lidaling commented Sep 8, 2015

I am using rsyslog on centos as a client .
But I can't get message with your code.
Please help me.
: )

@netravler

This comment has been minimized.

Copy link

@netravler netravler commented Mar 30, 2016

Excellent! just what I needed...

Thank you:)

@pakru

This comment has been minimized.

Copy link

@pakru pakru commented May 18, 2017

Nice one!

Thanks!

@rkreddy46

This comment has been minimized.

Copy link

@rkreddy46 rkreddy46 commented Jun 2, 2017

Thanks for the initiative. How do we add TLS support to this?

@sagism

This comment has been minimized.

Copy link

@sagism sagism commented Sep 10, 2017

Can you explain why would I use this instead of the real syslog facility?

@kmombergZF

This comment has been minimized.

Copy link

@kmombergZF kmombergZF commented Nov 24, 2017

You can add utf-8 encoding to improve the server:

def handle(self):
    data = bytes.decode(self.request[0].strip(), encoding="utf-8")
    socket = self.request[1]
    print( "%s : " % self.client_address[0], str(data.encode("utf-8")))
    logging.info(str(data.encode("utf-8")))

It worked very well when I needed a syslog server for some tests. Thanks for sharing.

@truthadjustr

This comment has been minimized.

Copy link

@truthadjustr truthadjustr commented Mar 28, 2018

Cute. If you are in python3, simply change SocketServer to socketserver. Official reference here

@choeffer

This comment has been minimized.

Copy link

@choeffer choeffer commented Jan 8, 2019

I developed a Python 3 implementation of a simple UDP syslog server which inserts the recieved messages into a MariaDB or MySQL database. https://github.com/choeffer/py3syslog
Your piece of code was helping me a lot to get started so I share mine also.

@laiaga

This comment has been minimized.

Copy link

@laiaga laiaga commented May 7, 2019

Thx, using it for routers monitoring :p

@neeles83

This comment has been minimized.

Copy link

@neeles83 neeles83 commented Nov 16, 2019

Nice thanks, will use it to trigger another script after syslog message is sent through EEM script.

@selimk92

This comment has been minimized.

Copy link

@selimk92 selimk92 commented Feb 2, 2020

I am just beginner in codding and my apoligise if I miss out the point but can someone explain me how to receive logs from regarding system? Don't you need to add this code in somewhere?

logging.getLogger()

@titaneric

This comment has been minimized.

Copy link

@titaneric titaneric commented Jun 19, 2020

Thanks for this useful script to help to receive the syslog and send to Apache Kafka.

@maxenc7

This comment has been minimized.

Copy link

@maxenc7 maxenc7 commented Oct 29, 2020

@marcelom Hello, "logging.info(str(data))" causes latency, if I send million of record in one minute, it saves only half of packet. How can we fix them? Can you help me :/

@marcelom

This comment has been minimized.

Copy link
Owner Author

@marcelom marcelom commented Oct 29, 2020

@maxenc7, seems like the contention is in the implementation of the logging.info() call itself. Nothing I can do there. That said, 1M/s seems a little bit too much for this tiny logger to handle. In fact, seems a lot for anything to handle in that amount of time. You are probably gonna have to use something to load balance this (haproxy or nginx) and then use several backends. This is completely outside the scope of this gist.

@maxenc7

This comment has been minimized.

Copy link

@maxenc7 maxenc7 commented Oct 29, 2020

@marcelom Could you please create a buffer field for "str(data)" information and write it to the file without logging.info()?

@marcelom

This comment has been minimized.

Copy link
Owner Author

@marcelom marcelom commented Oct 29, 2020

data is already the buffer. No need to double-buffer it or you will incur in extra latency. In regards to writing directly into the file, that can surely be done. You might have noticed that this code is 8+ years old, and I havent touched it (or even used it) in a long time. you are more than welcome to suggest a fix ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.