Skip to content

Instantly share code, notes, and snippets.

@mason-splunk
Forked from marcelom/pysyslog.py
Created December 19, 2019 23:53
Show Gist options
  • Save mason-splunk/c2ce0553da45882aca922cc6be10458b to your computer and use it in GitHub Desktop.
Save mason-splunk/c2ce0553da45882aca922cc6be10458b to your computer and use it in GitHub Desktop.

Revisions

  1. @marcelom marcelom created this gist Dec 5, 2012.
    37 changes: 37 additions & 0 deletions pysyslog.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    #!/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.")