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 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 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 commented Apr 20, 2015

Thank's for sharing nice code. 😃

@ilmarh

This comment has been minimized.

Copy link

ilmarh commented Jun 5, 2015

Thanks!

@pickeld

This comment has been minimized.

Copy link

pickeld commented Jul 18, 2015

Thanks!!

@lidaling

This comment has been minimized.

Copy link

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 commented Mar 30, 2016

Excellent! just what I needed...

Thank you:)

@pakru

This comment has been minimized.

Copy link

pakru commented May 18, 2017

Nice one!

Thanks!

@rkreddy46

This comment has been minimized.

Copy link

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 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 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 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 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 commented May 7, 2019

Thx, using it for routers monitoring :p

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.