forked from autotest/virt-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syslog_server.py
177 lines (143 loc) · 4.2 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import re, logging, SocketServer
SYSLOG_PORT = 514
DEFAULT_FORMAT = '[AutotestSyslog (%s.%s)] %s'
def set_default_format(message_format):
'''
Changes the default message format
@type message_format: string
@param message_format: a message format string with 3 placeholders:
facility, priority and message.
'''
global DEFAULT_FORMAT
DEFAULT_FORMAT = message_format
def get_default_format():
'''
Returns the current default message format
'''
return DEFAULT_FORMAT
class RequestHandler(SocketServer.BaseRequestHandler):
'''
A request handler that relays all received messages as DEBUG
'''
RECORD_RE = re.compile('\<(\d+)\>(.*)')
(LOG_EMERG,
LOG_ALERT,
LOG_CRIT,
LOG_ERR,
LOG_WARNING,
LOG_NOTICE,
LOG_INFO,
LOG_DEBUG) = range(8)
(LOG_KERN,
LOG_USER,
LOG_MAIL,
LOG_DAEMON,
LOG_AUTH,
LOG_SYSLOG,
LOG_LPR,
LOG_NEWS,
LOG_UUCP,
LOG_CRON,
LOG_AUTHPRIV,
LOG_FTP) = range(12)
(LOG_LOCAL0,
LOG_LOCAL1,
LOG_LOCAL2,
LOG_LOCAL3,
LOG_LOCAL4,
LOG_LOCAL5,
LOG_LOCAL6,
LOG_LOCAL7) = range(16, 24)
PRIORITY_NAMES = {
LOG_ALERT : "alert",
LOG_CRIT : "critical",
LOG_DEBUG : "debug",
LOG_EMERG : "emerg",
LOG_ERR : "err",
LOG_INFO : "info",
LOG_NOTICE : "notice",
LOG_WARNING: "warning"
}
FACILITY_NAMES = {
LOG_AUTH : "auth",
LOG_AUTHPRIV : "authpriv",
LOG_CRON : "cron",
LOG_DAEMON : "daemon",
LOG_FTP : "ftp",
LOG_KERN : "kern",
LOG_LPR : "lpr",
LOG_MAIL : "mail",
LOG_NEWS : "news",
LOG_AUTH : "security",
LOG_SYSLOG : "syslog",
LOG_USER : "user",
LOG_UUCP : "uucp",
LOG_LOCAL0 : "local0",
LOG_LOCAL1 : "local1",
LOG_LOCAL2 : "local2",
LOG_LOCAL3 : "local3",
LOG_LOCAL4 : "local4",
LOG_LOCAL5 : "local5",
LOG_LOCAL6 : "local6",
LOG_LOCAL7 : "local7",
}
def decodeFacilityPriority(self, priority):
'''
Decode both the facility and priority embedded in a syslog message
@type priority: integer
@param priority: an integer with facility and priority encoded
@return: a tuple with two strings
'''
f = priority >> 3
p = priority & 7
return (self.FACILITY_NAMES.get(f, 'unknown'),
self.PRIORITY_NAMES.get(p, 'unknown'))
def log(self, data, message_format=None):
'''
Logs the received message as a DEBUG message
'''
match = self.RECORD_RE.match(data)
if match:
if message_format is None:
message_format = get_default_format()
pri = int(match.groups()[0])
msg = match.groups()[1]
(facility_name, priority_name) = self.decodeFacilityPriority(pri)
logging.debug(message_format, facility_name, priority_name, msg)
class RequestHandlerTcp(RequestHandler):
def handle(self):
'''
Handles a single request
'''
data = self.request.recv(4096)
self.log(data)
class RequestHandlerUdp(RequestHandler):
def handle(self):
'''
Handles a single request
'''
data = self.request[0]
self.log(data)
class SysLogServerUdp(SocketServer.UDPServer):
def __init__(self, address):
SocketServer.UDPServer.__init__(self, address, RequestHandlerUdp)
class SysLogServerTcp(SocketServer.TCPServer):
def __init__(self, address):
SocketServer.TCPServer.__init__(self, address, RequestHandlerTcp)
def syslog_server(address='', port=SYSLOG_PORT,
tcp=True, terminate_callable=None):
if tcp:
klass = SysLogServerTcp
else:
klass = SysLogServerUdp
syslog = klass((address, port))
while True:
if terminate_callable is not None:
terminate = terminate_callable()
else:
terminate = False
if not terminate:
syslog.handle_request()
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
syslog_server()