-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyslog_server.py
43 lines (33 loc) · 1.28 KB
/
syslog_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import logging
import utils
import socketserver
from db import create_scoped_session, Switch, SyslogEntry
LOG_FILE = "syslog_server.log"
# With explicit address of the interface you always have to wait until the switch is powered up...
HOST, PORT = "0.0.0.0", 514
Session = create_scoped_session()
class SyslogUDPHandler(socketserver.BaseRequestHandler):
def handle(self):
syslog_message = str(bytes.decode(self.request[0].strip()))
switch_ip = self.client_address[0]
with Session() as session:
sw = session.query(Switch).filter(Switch.ztp_ip == switch_ip).one()
sw.syslog_entries.append(SyslogEntry(msg=syslog_message))
session.commit()
class SyslogServer:
def __init__(self):
self._server = socketserver.UDPServer((HOST, PORT), SyslogUDPHandler)
def start(self):
logging.info("Starting syslog server")
self._server.serve_forever(poll_interval=0.1)
def stop(self):
logging.info("Stopping syslog server")
self._server.shutdown()
logging.info("Stopped syslog server")
if __name__ == "__main__":
utils.configure_logging()
try:
syslog_server = SyslogServer()
syslog_server.start()
except KeyboardInterrupt:
logging.info("Exit syslog server")