Multi-threaded

This commit is contained in:
Ronny Grobel 2020-11-27 23:04:59 +01:00 committed by GitHub
parent 41015bdbcd
commit 8a3fc9ef6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,57 +8,53 @@ HOST, PORT = "0.0.0.0", 514
OracleUser = '' OracleUser = ''
OraclePasswd = '' OraclePasswd = ''
OracleServerSid = "localhost/orclpdb1" OracleServerSid = "localhost/orclpdb1"
#
# NO USER SERVICEABLE PARTS BELOW HERE...
# #
import sys import sys
import cx_Oracle import cx_Oracle
import config import socketserver
import SocketServer
import json import json
import threading
connection = cx_Oracle.connect(OracleUser, OraclePasswd, OracleServerSid) connection = cx_Oracle.connect(OracleUser, OraclePasswd, OracleServerSid, threaded = True)
cursor = connection.cursor() cursor = connection.cursor()
def insert_logging(IP,PRIO,STAMP,HOST,APP,PID,MESS): def insert_logging(IP,PRIO,STAMP,HOST,APP,PID,MESS):
""" """
Insert a row to the SYSLOG table Insert a row to the SYSLOG table
:param IP: :param IP:
:param APP: :param APP:
:param STAMP: :param STAMP:
:param HOST: :param HOST:
:param PID: :param PID:
:param PRIO: :param PRIO:
:param MESS: :param MESS:
:return: :return:
""" """
sql = ('insert into SYSLOG(REMOTEIP,APPNAME,ZEIT,HOSTNAME,PID,PRIORITY,MESSAGE) ' # construct an insert statement that add a new row to the billing_headers table
'values(:IP,:APP,:STAMP,:HOST,:PID,:PRIO,:MESS)') sql = ('insert into SYSLOG(REMOTEIP,APPNAME,ZEIT,HOSTNAME,PID,PRIORITY,MESSAGE) values (:IP,:APP,:STAMP,:HOST,:PID,:PRIO,:MESS)')
try:
try: cursor.execute(sql, [IP, APP, STAMP, HOST, PRIO, PID, MESS])
cursor.execute(sql, [IP, APP, STAMP, HOST, PRIO, PID, MESS]) connection.commit()
connection.commit() except cx_Oracle.Error as error:
except cx_Oracle.Error as error: print('Error occurred:')
print('Error occurred:') print(error)
print(error)
class SyslogUDPHandler(SocketServer.BaseRequestHandler): class SyslogUDPHandler(socketserver.BaseRequestHandler):
def handle(self): def handle(self):
data = bytes.decode(self.request[0].strip()) data = bytes.decode(self.request[0].strip())
socket = self.request[1] socket = self.request[1]
print( "%s : " % self.client_address[0], str(data))
fields = json.loads(str(data)) fields = json.loads(str(data))
insert_logging("%s" % self.client_address[0],fields["PRIO"].strip(),fields["TIME"],fields["HOST"],fields["APP"],fields["PID"].strip(),fields["MSG"]) insert_logging("%s" % self.client_address[0],fields["PRIO"].strip(),fields["TIME"],fields["HOST"],fields["APP"],fields["PID"].strip(),fields["MSG"])
print("Recieved one request from {}".format(self.client_address[0]))
print("Thread Name:{}".format(threading.current_thread().name))
if __name__ == "__main__": if __name__ == "__main__":
try: try:
server = SocketServer.UDPServer((HOST,PORT), SyslogUDPHandler) server = socketserver.ThreadingUDPServer((HOST,PORT), SyslogUDPHandler)
server.serve_forever(poll_interval=0.5) server.serve_forever()
except (IOError, SystemExit): except (IOError, SystemExit):
raise raise
except KeyboardInterrupt: except KeyboardInterrupt: