Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
live-log-analyzer/liveloganalyzer/sourceexecutive.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
62 lines (50 sloc)
1.75 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |