-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I have changed the listener to be plugable in the same way that the publisher transport is. I have also added a kafka listener too.
- Loading branch information
1 parent
61c60ad
commit 2a8f164
Showing
10 changed files
with
241 additions
and
81 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# -*- coding: utf-8 -*- | ||
''' | ||
napalm-logs pluggable listener. | ||
''' | ||
from __future__ import absolute_import | ||
from __future__ import unicode_literals | ||
|
||
# Import napalm-logs pkgs | ||
from napalm_logs.listener.base import ListenerBase | ||
from napalm_logs.listener.kafka import KafkaListener | ||
from napalm_logs.listener.tcp import TCPListener | ||
from napalm_logs.listener.udp import UDPListener | ||
from napalm_logs.listener.kafka import HAS_KAFKA | ||
|
||
LISTENER_LOOKUP = { | ||
'tcp': TCPListener, | ||
'udp': UDPListener, | ||
'*': UDPListener # default listener | ||
} | ||
|
||
if HAS_KAFKA: | ||
LISTENER_LOOKUP['kafka'] = KafkaListener | ||
|
||
def get_listener(name): | ||
''' | ||
Return the listener class. | ||
''' | ||
return LISTENER_LOOKUP.get(name, LISTENER_LOOKUP['*']) | ||
|
||
__all__ = ( | ||
'get_listener', | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# -*- coding: utf-8 -*- | ||
''' | ||
napalm-logs listener base. | ||
''' | ||
|
||
|
||
class ListenerBase: | ||
''' | ||
The base class for the listener. | ||
''' | ||
def __init__(self, addr, port): | ||
pass | ||
|
||
def start(self): | ||
pass | ||
|
||
def publish(self, obj): | ||
pass | ||
|
||
def stop(self): | ||
pass |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# -*- coding: utf-8 -*- | ||
''' | ||
Kafka listener for napalm-logs. | ||
''' | ||
from __future__ import absolute_import | ||
from __future__ import unicode_literals | ||
|
||
# Import stdlib | ||
import json | ||
import signal | ||
import logging | ||
|
||
# Import third party libs | ||
try: | ||
import kafka | ||
HAS_KAFKA = True | ||
except ImportError as err: | ||
HAS_KAFKA = False | ||
|
||
# Import napalm-logs pkgs | ||
from napalm_logs.exceptions import NapalmLogsException | ||
from napalm_logs.listener.base import ListenerBase | ||
from napalm_logs.config import KAFKA_LISTENER_TOPIC | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class KafkaListener(ListenerBase): | ||
''' | ||
Kafka listener class. | ||
''' | ||
def __init__(self, address, port, pipe): | ||
self.address = address | ||
self.port = port | ||
self.pipe = pipe | ||
self.topic = KAFKA_LISTENER_TOPIC | ||
self.__up = False | ||
|
||
def _exit_gracefully(self, signum, _): | ||
log.debug('Caught signal in listener process') | ||
self.stop() | ||
|
||
def start(self): | ||
''' | ||
Start listening for messages | ||
''' | ||
# Start suicide polling thread | ||
signal.signal(signal.SIGTERM, self._exit_gracefully) | ||
self.__up = True | ||
try: | ||
self.consumer = kafka.KafkaConsumer(bootstrap_servers='{}:{}'.format(self.address, self.port), group_id='napalm-logs') | ||
except kafka.errors.NoBrokersAvailable as err: | ||
log.error(err, exc_info=True) | ||
raise NapalmLogsException(err) | ||
self.consumer.subscribe(topics=[self.topic]) | ||
while self.__up: | ||
try: | ||
msg = next(self.consumer) | ||
except ValueError as error: | ||
if self.__up is False: | ||
return | ||
else: | ||
msg = 'Received kafka error: {}'.format(error) | ||
log.error(msg, exc_info=True) | ||
raise NapalmLogsExit(msg) | ||
decoded = json.loads(msg.value) | ||
log_message = decoded.get('message') | ||
log_source = decoded.get('logsource') | ||
log.debug('[{2}] Received {0} from {1}. Adding in the queue'.format(log_message, log_source, time.time())) | ||
self.pipe.send((log_message, log_source)) | ||
|
||
def stop(self): | ||
log.info('Stopping listener process') | ||
self.__up = False | ||
self.consumer.unsubscribe() | ||
self.consumer.close() | ||
self.pipe.close() |
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
Oops, something went wrong.