forked from EasyPost/syslog-rfc5424-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_syslog_server.py
executable file
·38 lines (29 loc) · 1.07 KB
/
example_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
#!/usr/bin/env python
from __future__ import print_function
import argparse
import socket
import os
import sys
import json
from syslog_rfc5424_parser import SyslogMessage, ParseError
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-B', '--bind-path', required=True,
help='Path at which to bind a Datagram-mode UNIX domain socket')
args = parser.parse_args()
s = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
temp_name = args.bind_path + '.' + str(os.getpid())
s.bind(temp_name)
os.rename(temp_name, args.bind_path)
while True:
message = s.recv(4096)
# Technically, messages are only UTF-8 if they have a BOM; otherwise they're binary. However, I'm not
# aware of any Syslog servers that handle that. *shrug*
message = message.decode('utf-8')
try:
message = SyslogMessage.parse(message)
print(json.dumps(message.as_dict()))
except ParseError as e:
print(e, file=sys.stderr)
if __name__ == '__main__':
sys.exit(main())