forked from gryphius/syslog-stdout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyslog-stdout.py
executable file
·119 lines (103 loc) · 2.45 KB
/
syslog-stdout.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/python
import sys
import socket
import signal
import os
from daemon import Daemon
bufsiz = 2048
LOG_PRIMASK = 0x07
PRIMASK = [
"emerg",
"alert",
"crit",
"err",
"warning",
"notice",
"info",
"debug"
]
FACILITYMASK = [
"kern",
"user",
"mail",
"daemon",
"auth",
"syslog",
"lpr",
"news",
"uucp",
"cron",
"authpriv",
"ftp",
"ntp",
"security",
"console",
"mark",
"local0",
"local1",
"local2",
"local3",
"local4",
"local5",
"local6",
"local7"
]
def byte2string(byte):
'''
Convert syslog classification byte to facility and priority.
The first 5 bits are the facility
The last 3 bits are the priority
See pg9 http://www.syslog.cc/ietf/drafts/draft-ietf-syslog-protocol-23.txt
'''
try:
facility = FACILITYMASK[byte >> 3]
priority = PRIMASK[byte & LOG_PRIMASK]
return "%s.%s" % (facility, priority)
except Exception as err:
print "Unexpected facility or priority: %s.%s" % (facility, priority)
print err
return "unknown.unknown"
class SyslogListener(Daemon):
def datagramReceived(self, data):
"""strip priority tag"""
if data[2] == ">":
pri = byte2string(int(data[1]))
msg = data[3:]
elif data[3] == ">":
pri = byte2string(int(data[1:3]))
msg = data[4:]
else:
pri = None
msg = data
msg = msg.strip()
print "%s:%s" % (pri, msg)
def run(self):
try:
os.remove('/dev/log')
except Exception:
pass
try:
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
self.sock.bind("/dev/log")
except Exception:
print "Socket error: (%s) %s " % (sys.exc_info()[1][0],
sys.exc_info()[1][1])
sys.exit(1)
while 1:
try:
data, addr = self.sock.recvfrom(bufsiz)
self.datagramReceived(data)
except socket.error:
pass
def terminate(self):
try:
self.sock.close()
except Exception:
pass
try:
os.remove("/dev/log")
except Exception:
pass
if __name__ == '__main__':
syslog = SyslogListener('/tmp/syslog-stdout.pid', stdout='/dev/stdout')
syslog.start()