-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcollector.py
71 lines (59 loc) · 1.86 KB
/
collector.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
#!/usr/bin/python
import multitail2
import hpfeeds
import sys
import datetime
import json
import hpfeeds
import logging
root = logging.getLogger()
root.setLevel(logging.ERROR)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
root.addHandler(ch)
logger = logging.getLogger("hpfeeds-collector")
def parse(line):
# TODO: extend this
return {}
def hpfeeds_connect(host, port, ident, secret):
try:
connection = hpfeeds.new(host, port, ident, secret)
except hpfeeds.FeedException, e:
logger.error('feed exception: %s'%e)
sys.exit(1)
logger.info('connected to %s (%s:%s)'%(connection.brokername, host, port))
return connection
def main():
cfg = {
'host' : '',
'port' : 10000,
'channel' : '',
'ident ' : '',
'secret' : '',
'tail_file' : ''
}
if len(sys.argv) > 1:
logger.info("Parsing config file: %s"%sys.argv[1])
cfg.update(json.load(file(sys.argv[1])))
for name,value in cfg.items():
if isinstance(value, basestring):
# hpfeeds protocol has trouble with unicode, hence the utf-8 encoding here
cfg[name] = value.encode("utf-8")
else:
logger.warning("Warning: no config found, using default values for hpfeeds server")
publisher = hpfeeds_connect(cfg['host'], cfg['port'], cfg['ident'], cfg['secret'])
tail = multitail2.MultiTail(cfg['tail_file'])
for filemeta, line in tail:
logger.debug(filemeta, line)
record = parse(line)
if record:
publisher.publish(cfg['channel'], json.dumps(record))
publisher.stop()
return 0
if __name__ == '__main__':
try:
sys.exit(main())
except KeyboardInterrupt:
sys.exit(0)