Skip to content

Commit

Permalink
Merge pull request #22 from napalm-automation/napalm-logs-cli
Browse files Browse the repository at this point in the history
Napalm logs cli
  • Loading branch information
mirceaulinic authored Mar 30, 2017
2 parents 48bffec + ecfd3b2 commit a5e8781
Show file tree
Hide file tree
Showing 9 changed files with 254 additions and 32 deletions.
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include requirements.txt
include napalm_logs/config/*.yml
10 changes: 9 additions & 1 deletion napalm_logs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@
from __future__ import absolute_import
from __future__ import unicode_literals

import pkg_resources

# Import napalm-logs pkgs
from napalm_logs.base import NapalmLogs

__all__ = ('NapalmLogs',)
try:
__version__ = pkg_resources.get_distribution('napalm-logs').version
except pkg_resources.DistributionNotFound:
__version__ = "Not installed"


__all__ = ('NapalmLogs', '__version__')
29 changes: 10 additions & 19 deletions napalm_logs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

# Import napalm-logs pkgs
from napalm_logs.config import VALID_CONFIG
from napalm_logs.config import LOGGING_LEVEL
from napalm_logs.transport import get_transport
from napalm_logs.device import NapalmLogsDeviceProc
from napalm_logs.server import NapalmLogsServerProc
from napalm_logs.listener import NapalmLogsListenerProc
from napalm_logs.exceptions import UnableToBindException
from napalm_logs.exceptions import MissConfigurationException
from napalm_logs.exceptions import BindException
from napalm_logs.exceptions import ConfigurationException

log = logging.getLogger(__name__)

Expand All @@ -38,7 +39,7 @@ def __init__(self,
extension_config_path=None,
extension_config_dict=None,
log_level='warning',
log_fmt='%(asctime)s,%(msecs)03.0f [%(name)-17s][%(levelname)-8s] %(message)s'):
log_format='%(asctime)s,%(msecs)03.0f [%(name)-17s][%(levelname)-8s] %(message)s'):
'''
Init the napalm-logs engine.
Expand All @@ -58,7 +59,7 @@ def __init__(self,
self.extension_config_path = extension_config_path
self.extension_config_dict = extension_config_dict
self.log_level = log_level
self.log_fmt = log_fmt
self.log_format = log_format
# Setup the environment
self._setup_log()
self._setup_transport()
Expand All @@ -81,14 +82,8 @@ def _setup_log(self):
'''
Setup the log object.
'''
logging_level = {
'debug': logging.DEBUG,
'info': logging.INFO,
'warning': logging.WARNING,
'error': logging.ERROR,
'critical': logging.CRITICAL
}.get(self.log_level.lower())
logging.basicConfig(format=self.log_fmt,
logging_level = LOGGING_LEVEL.get(self.log_level.lower())
logging.basicConfig(format=self.log_format,
level=logging_level)

def _setup_transport(self):
Expand Down Expand Up @@ -136,7 +131,7 @@ def _load_config(self, path):
@staticmethod
def _raise_config_exception(error_string):
log.error(error_string, exc_info=True)
raise MissConfigurationException(error_string)
raise ConfigurationException(error_string)

def _verify_config_key(self, key, value, valid, config, dev_os, key_path):
key_path.append(key)
Expand Down Expand Up @@ -174,17 +169,13 @@ def _verify_config(self):
'''
if not self.config_dict:
self._raise_config_exception('No config found')


# Check for device conifg, if there isn't anything then just log, do not raise an exception
for dev_os, dev_config in self.config_dict.items():
if not dev_config:
log.error('No config found for {}'.format(dev_os))
log.warning('No config found for {}'.format(dev_os))
continue

# Compare the valid opts with the conifg
self._verify_config_dict(VALID_CONFIG, dev_config, dev_os)

log.debug('Read the config without error \o/')


Expand Down Expand Up @@ -240,7 +231,7 @@ def start_engine(self):
except socket.error, msg:
error_string = 'Unable to bind to port {} on {}: {}'.format(self.port, self.address, msg)
log.error(error_string, exc_info=True)
raise UnableToBindException(error_string)
raise BindException(error_string)

log.info('Preparing the transport')
self.transport.start()
Expand Down
21 changes: 21 additions & 0 deletions napalm_logs/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@
from __future__ import absolute_import
from __future__ import unicode_literals

import os
import logging

ROOT_DIR = '/'
CONFIG_FILE = os.path.join(ROOT_DIR, 'etc', 'napalm', 'logs')
ADDRESS = '0.0.0.0'
PORT = 514
PUBLISH_ADDRESS = '0.0.0.0'
PUBLISH_PORT = 49017
LOG_LEVEL = 'warning'
LOG_FORMAT = '%(asctime)s,%(msecs)03.0f [%(name)-17s][%(levelname)-8s] %(message)s'
LOG_FILE = os.path.join(ROOT_DIR, 'var', 'log', 'napalm', 'logs')

LOGGING_LEVEL = {
'debug': logging.DEBUG,
'info': logging.INFO,
'warning': logging.WARNING,
'error': logging.ERROR,
'critical': logging.CRITICAL
}

DEFAULT_DELIM = '/'

VALID_CONFIG = {
Expand Down
6 changes: 2 additions & 4 deletions napalm_logs/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# Import napalm-logs pkgs
from napalm_logs.proc import NapalmLogsProc
from napalm_logs.config import DEFAULT_DELIM
from napalm_logs.exceptions import OpenConfigPathError
from napalm_logs.exceptions import OpenConfigPathException
from napalm_logs.exceptions import UnknownOpenConfigModel

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -198,7 +198,7 @@ def _emit(self, **kwargs):
except AttributeError:
error_string = 'Error whilst mapping to open config, please check that the mappings are correct for {}'.format(self._name)
log.error(error_string, exc_info=True)
raise OpenConfigPathError(error_string)
raise OpenConfigPathException(error_string)

return oc_obj.get(filter=True)

Expand All @@ -221,7 +221,6 @@ def _format_time(self, time, date):
timestamp = datetime.strptime('{} {} {}'.format(year, date, time), '%Y {}'.format(time_format))
return timestamp.strftime('%s')


def start(self):
'''
Start the worker process.
Expand All @@ -247,7 +246,6 @@ def start(self):
'message_details': msg_dict
}
self._publish(to_publish)


def stop(self):
'''
Expand Down
20 changes: 13 additions & 7 deletions napalm_logs/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,32 @@ def __init__(self, msg=''):
super(NapalmLogsException, self).__init__(msg)
self.strerror = msg

class UnableToBindException(NapalmLogsException):

class BindException(NapalmLogsException):
'''
We are unable to bind to the specified ip / port
Exception raised when unable to bind the listener to the specified IP
address / port. Either the values are not correct, either another processs
is already using them.
'''
pass

class MissConfigurationException(NapalmLogsException):

class ConfigurationException(NapalmLogsException):
'''
The configuration does not match the valid config template
Exception thrown when the user configuration is not correct.
'''
pass


class UnknownOpenConfigModel(NapalmLogsException):
'''
We are unable to log a model via napalm-yang
Unable to log a model via napalm-yang
'''
pass

class OpenConfigPathError(NapalmLogsException):

class OpenConfigPathException(NapalmLogsException):
'''
We are unable to set the open config path specified
Unable to set the open config path specified.
'''
pass
Empty file added napalm_logs/scripts/__init__.py
Empty file.
Loading

0 comments on commit a5e8781

Please sign in to comment.