Repository navigation
Expand file tree
/
Copy pathsourceexecutive.py
More file actions
62 lines (50 loc) · 1.75 KB
/
Copy pathsourceexecutive.py
File metadata and controls
62 lines (50 loc) · 1.75 KB
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
import time
from threading import Thread
from pymongo import Connection
from pymongo.errors import CollectionInvalid, InvalidStringData
from debuglogging import error
from settings import MONGODB_NAME, MAX_COLLECTION_SIZE, SOURCES_SETTINGS
def main():
for ss in SOURCES_SETTINGS:
t = Thread(target=run_one, args=(ss,))
t.start()
time.sleep(1)
def run_one(settings):
s = SourceExecutive(settings)
s.start()
class SourceExecutive(object):
def __init__(self, settings):
self.collection = settings['collection']
self.parser = settings['parser']
self.source_class = settings['source'][0]
self.kwargs = settings['source'][1]
def start(self):
self.start_source_stream()
self.connect_to_mongo()
self.store_data()
def start_source_stream(self):
self.source = self.source_class(**self.kwargs)
self.source.start_stream()
def connect_to_mongo(self):
conn = Connection()
db = conn[MONGODB_NAME]
try:
self.mongo = db.create_collection(
self.collection, capped=True,
size=MAX_COLLECTION_SIZE * 1048576)
except CollectionInvalid:
self.mongo = db[self.collection]
def store_data(self):
while True:
line = self.source.get_line()
data = self.parser.parse_line(line)
if data:
data['server'] = self.source.ssh_params['host']
try:
self.mongo.insert(data)
except InvalidStringData, e:
error('%s\n%s' % (str(e), line))
else:
error("%s couldn't parse line:\n%s" % (str(self.parser), line))
if __name__ == '__main__':
main()