Exemple #1
0
def start(transport='zmq', address='0.0.0.0', port=49017):
    '''
    Listen to napalm-logs and publish events into the Salt event bus.

    transport: ``zmq``
        Choose the desired transport.

        .. note::
            Currently ``zmq`` is the only valid option.

    address: ``0.0.0.0``
        The address of the publisher.

    port: ``49017``
        The port of the publisher.
    '''
    transport_recv_fun = _get_transport_recv(name=transport,
                                             address=address,
                                             port=port)
    if not transport_recv_fun:
        log.critical('Unable to start the engine', exc_info=True)
        return
    master = False
    if __opts__['__role'] == 'master':
        master = True
    while True:
        raw_object = transport_recv_fun()
        log.debug('Received from napalm-logs:')
        log.debug(raw_object)
        try:
            dict_object = json.loads(raw_object)
        except ValueError:
            log.error(
                'Unable to deserialise JSON object: {0}'.format(raw_object),
                exc_info=True)
            continue  # and go the the next item
        if not isinstance(dict_object, dict):
            log.error('Invalid object read from napalm-logs:')
            log.error(dict_object)
            continue  # ignore
        tag = 'napalm/syslog/{os}/{error}/{device}'.format(
            os=dict_object['os'],
            error=dict_object['error'],
            device=dict_object.get('host', dict_object.get('ip')))
        log.debug('Sending event {0}'.format(tag))
        log.debug(raw_object)
        if master:
            event.get_master_event(__opts__, __opts__['sock_dir']).fire_event(
                dict_object, tag)
        else:
            __salt__['event.send'](tag, dict_object)
Exemple #2
0
    def send_event_to_salt(self, result):
        '''
        This function identifies whether the engine is running on the master
        or the minion and sends the data to the master event bus accordingly.

        :param result: It's a dictionary which has the final data and topic.

        '''
        if result['send']:
            data = result['data']
            topic = result['topic']
            # If the engine is run on master, get the event bus and send the
            # parsed event.
            if __opts__['__role'] == 'master':
                event.get_master_event(__opts__,
                                       __opts__['sock_dir']).fire_event(
                                           data, topic)
            # If the engine is run on minion, use the fire_master execution
            # module to send event on the master bus.
            else:
                __salt__['event.fire_master'](data=data, tag=topic)
Exemple #3
0
    def send_event_to_salt(self, result):
        """
        This function identifies whether the engine is running on the master
        or the minion and sends the data to the master event bus accordingly.

        :param result: It's a dictionary which has the final data and topic.

        """
        if result["send"]:
            data = result["data"]
            topic = result["topic"]
            # If the engine is run on master, get the event bus and send the
            # parsed event.
            if __opts__["__role"] == "master":
                event.get_master_event(__opts__,
                                       __opts__["sock_dir"]).fire_event(
                                           data, topic)
            # If the engine is run on minion, use the fire_master execution
            # module to send event on the master bus.
            else:
                __salt__["event.fire_master"](data=data, tag=topic)
Exemple #4
0
def start(
    transport="zmq",
    address="0.0.0.0",
    port=49017,
    auth_address="0.0.0.0",
    auth_port=49018,
    disable_security=False,
    certificate=None,
    os_whitelist=None,
    os_blacklist=None,
    error_whitelist=None,
    error_blacklist=None,
    host_whitelist=None,
    host_blacklist=None,
):
    """
    Listen to napalm-logs and publish events into the Salt event bus.

    transport: ``zmq``
        Choose the desired transport.

        .. note::
            Currently ``zmq`` is the only valid option.

    address: ``0.0.0.0``
        The address of the publisher, as configured on napalm-logs.

    port: ``49017``
        The port of the publisher, as configured on napalm-logs.

    auth_address: ``0.0.0.0``
        The address used for authentication
        when security is not disabled.

    auth_port: ``49018``
        Port used for authentication.

    disable_security: ``False``
        Trust unencrypted messages.
        Strongly discouraged in production.

    certificate: ``None``
        Absolute path to the SSL certificate.

    os_whitelist: ``None``
        List of operating systems allowed. By default everything is allowed.

    os_blacklist: ``None``
        List of operating system to be ignored. Nothing ignored by default.

    error_whitelist: ``None``
        List of errors allowed.

    error_blacklist: ``None``
        List of errors ignored.

    host_whitelist: ``None``
        List of hosts or IPs to be allowed.

    host_blacklist: ``None``
        List of hosts of IPs to be ignored.
    """
    if not disable_security:
        if not certificate:
            log.critical("Please use a certificate, or disable the security.")
            return
        auth = napalm_logs.utils.ClientAuth(
            certificate, address=auth_address, port=auth_port
        )

    transport_recv_fun = _get_transport_recv(name=transport, address=address, port=port)
    if not transport_recv_fun:
        log.critical("Unable to start the engine", exc_info=True)
        return
    master = False
    if __opts__["__role"] == "master":
        master = True
    while True:
        log.debug("Waiting for napalm-logs to send anything...")
        raw_object = transport_recv_fun()
        log.debug("Received from napalm-logs:")
        log.debug(raw_object)
        if not disable_security:
            dict_object = auth.decrypt(raw_object)
        else:
            dict_object = napalm_logs.utils.unserialize(raw_object)
        try:
            event_os = dict_object["os"]
            if os_blacklist or os_whitelist:
                valid_os = salt.utils.stringutils.check_whitelist_blacklist(
                    event_os, whitelist=os_whitelist, blacklist=os_blacklist
                )
                if not valid_os:
                    log.info("Ignoring NOS %s as per whitelist/blacklist", event_os)
                    continue
            event_error = dict_object["error"]
            if error_blacklist or error_whitelist:
                valid_error = salt.utils.stringutils.check_whitelist_blacklist(
                    event_error, whitelist=error_whitelist, blacklist=error_blacklist
                )
                if not valid_error:
                    log.info(
                        "Ignoring error %s as per whitelist/blacklist", event_error
                    )
                    continue
            event_host = dict_object.get("host") or dict_object.get("ip")
            if host_blacklist or host_whitelist:
                valid_host = salt.utils.stringutils.check_whitelist_blacklist(
                    event_host, whitelist=host_whitelist, blacklist=host_blacklist
                )
                if not valid_host:
                    log.info(
                        "Ignoring messages from %s as per whitelist/blacklist",
                        event_host,
                    )
                    continue
            tag = "napalm/syslog/{os}/{error}/{host}".format(
                os=event_os, error=event_error, host=event_host
            )
        except KeyError as kerr:
            log.warning("Missing keys from the napalm-logs object:", exc_info=True)
            log.warning(dict_object)
            continue  # jump to the next object in the queue
        log.debug("Sending event %s", tag)
        log.debug(raw_object)
        if master:
            event.get_master_event(__opts__, __opts__["sock_dir"]).fire_event(
                dict_object, tag
            )
        else:
            __salt__["event.send"](tag, dict_object)
Exemple #5
0
def start(transport='zmq',
          address='0.0.0.0',
          port=49017,
          auth_address='0.0.0.0',
          auth_port=49018,
          disable_security=False,
          certificate=None,
          os_whitelist=None,
          os_blacklist=None,
          error_whitelist=None,
          error_blacklist=None,
          host_whitelist=None,
          host_blacklist=None):
    '''
    Listen to napalm-logs and publish events into the Salt event bus.

    transport: ``zmq``
        Choose the desired transport.

        .. note::
            Currently ``zmq`` is the only valid option.

    address: ``0.0.0.0``
        The address of the publisher, as configured on napalm-logs.

    port: ``49017``
        The port of the publisher, as configured on napalm-logs.

    auth_address: ``0.0.0.0``
        The address used for authentication
        when security is not disabled.

    auth_port: ``49018``
        Port used for authentication.

    disable_security: ``False``
        Trust unencrypted messages.
        Strongly discouraged in production.

    certificate: ``None``
        Absolute path to the SSL certificate.

    os_whitelist: ``None``
        List of operating systems allowed. By default everything is allowed.

    os_blacklist: ``None``
        List of operating system to be ignored. Nothing ignored by default.

    error_whitelist: ``None``
        List of errors allowed.

    error_blacklist: ``None``
        List of errors ignored.

    host_whitelist: ``None``
        List of hosts or IPs to be allowed.

    host_blacklist: ``None``
        List of hosts of IPs to be ignored.
    '''
    if not disable_security:
        if not certificate:
            log.critical('Please use a certificate, or disable the security.')
            return
        priv_key, verify_key = napalm_logs.utils.authenticate(
            certificate, address=auth_address, port=auth_port)
    transport_recv_fun = _get_transport_recv(name=transport,
                                             address=address,
                                             port=port)
    if not transport_recv_fun:
        log.critical('Unable to start the engine', exc_info=True)
        return
    master = False
    if __opts__['__role'] == 'master':
        master = True
    while True:
        log.debug('Waiting for napalm-logs to send anything...')
        raw_object = transport_recv_fun()
        log.debug('Received from napalm-logs:')
        log.debug(raw_object)
        if not disable_security:
            dict_object = napalm_logs.utils.decrypt(raw_object, verify_key,
                                                    priv_key)
        else:
            dict_object = napalm_logs.utils.unserialize(raw_object)
        try:
            event_os = dict_object['os']
            if os_blacklist or os_whitelist:
                valid_os = salt.utils.check_whitelist_blacklist(
                    event_os, whitelist=os_whitelist, blacklist=os_blacklist)
                if not valid_os:
                    log.info(
                        'Ignoring NOS {} as per whitelist/blacklist'.format(
                            event_os))
                    continue
            event_error = dict_object['error']
            if error_blacklist or error_whitelist:
                valid_error = salt.utils.check_whitelist_blacklist(
                    event_error,
                    whitelist=error_whitelist,
                    blacklist=error_blacklist)
                if not valid_error:
                    log.info(
                        'Ignoring error {} as per whitelist/blacklist'.format(
                            event_error))
                    continue
            event_host = dict_object.get('host') or dict_object.get('ip')
            if host_blacklist or host_whitelist:
                valid_host = salt.utils.check_whitelist_blacklist(
                    event_host,
                    whitelist=host_whitelist,
                    blacklist=host_blacklist)
                if not valid_host:
                    log.info(
                        'Ignoring messages from {} as per whitelist/blacklist'.
                        format(event_host))
                    continue
            tag = 'napalm/syslog/{os}/{error}/{host}'.format(os=event_os,
                                                             error=event_error,
                                                             host=event_host)
        except KeyError as kerr:
            log.warning('Missing keys from the napalm-logs object:',
                        exc_info=True)
            log.warning(dict_object)
            continue  # jump to the next object in the queue
        log.debug('Sending event {0}'.format(tag))
        log.debug(raw_object)
        if master:
            event.get_master_event(__opts__, __opts__['sock_dir']).fire_event(
                dict_object, tag)
        else:
            __salt__['event.send'](tag, dict_object)
Exemple #6
0
def start(transport='zmq',
          address='0.0.0.0',
          port=49017,
          auth_address='0.0.0.0',
          auth_port=49018,
          disable_security=False,
          certificate=None):
    '''
    Listen to napalm-logs and publish events into the Salt event bus.

    transport: ``zmq``
        Choose the desired transport.

        .. note::
            Currently ``zmq`` is the only valid option.

    address: ``0.0.0.0``
        The address of the publisher.

    port: ``49017``
        The port of the publisher.

    auth_address: ``0.0.0.0``
        The address used for authentication
        when security is not disabled.

    auth_port: ``49018``
        Port used for authentication.

    disable_security: ``False``
        Trust unencrypted messages.
        Strongly discouraged in production.

    certificate
        Absolute path to the SSL certificate.
    '''
    if not disable_security:
        priv_key, verify_key = napalm_logs.utils.authenticate(certificate,
                                                              address=auth_address,
                                                              port=auth_port)
    transport_recv_fun = _get_transport_recv(name=transport,
                                             address=address,
                                             port=port)
    if not transport_recv_fun:
        log.critical('Unable to start the engine', exc_info=True)
        return
    master = False
    if __opts__['__role'] == 'master':
        master = True
    while True:
        log.debug('Waiting for napalm-logs to send anything...')
        raw_object = transport_recv_fun()
        log.debug('Received from napalm-logs:')
        log.debug(raw_object)
        if not disable_security:
            dict_object = napalm_logs.utils.decrypt(raw_object, verify_key, priv_key)
        else:
            dict_object = napalm_logs.utils.unserialize(raw_object)
        try:
            tag = 'napalm/syslog/{os}/{error}/{host}'.format(
                os=dict_object['os'],
                error=dict_object['error'],
                host=(dict_object.get('host') or dict_object.get('ip'))
            )
        except KeyError as kerr:
            log.warning('Missing keys from the napalm-logs object:', exc_info=True)
            log.warning(dict_object)
        log.debug('Sending event {0}'.format(tag))
        log.debug(raw_object)
        if master:
            event.get_master_event(__opts__,
                                   __opts__['sock_dir']
                                   ).fire_event(dict_object, tag)
        else:
            __salt__['event.send'](tag, dict_object)