forked from alfredgg/supervisor2fluentd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
supervisor2fluentd.py
50 lines (37 loc) · 1.32 KB
/
supervisor2fluentd.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import config
from fluent import sender, event
def write_stdout(s):
sys.stdout.write(s)
sys.stdout.flush()
def write_stderr(s):
sys.stderr.write(s)
sys.stderr.flush()
def read_data():
line = sys.stdin.readline()
headers = dict([x.split(':') for x in line.split()])
_data = sys.stdin.read(int(headers['len']))
_data = _data.split('\n')
info = dict([x.split(':') for x in _data[0].split()])
return {
'logline': '\n'.join(_data[1:]), # log line
'processname': info['processname'], # the same identifier than supervisord
'channel': info['channel'] # stdout or stderr
}
def send_logline(data):
tag = '{0}.{1}'.format(data['processname'], data['channel'])
sender.setup('supervisor', host=config.FLUENTD_HOST, port=config.FLUENTD_PORT)
event.Event(tag, {'logline': data['logline']})
def main():
while True:
try:
write_stdout('READY\n') # transition from ACKNOWLEDGED to READY
data = read_data()
send_logline(data)
except Exception as e:
write_stderr(str(e) + '\n')
write_stdout('RESULT 2\nOK') # transition from READY to ACKNOWLEDGED
if __name__ == '__main__':
main()