File manager - Edit - /opt/saltstack/salt/lib/python3.10/site-packages/salt/engines/__pycache__/napalm_syslog.cpython-310.pyc
Back
o ;j<. � @ s� d Z ddlZddlm mZ ddlZddlZddlm Z zddl Z ddlZ dZW n e y4 dZY nw e�e�ZdZdd� Zd d � Zddd�Zeed�Z ddd�ZdS )a7 NAPALM syslog engine ==================== .. versionadded:: 2017.7.0 An engine that takes syslog messages structured in OpenConfig_ or IETF format and fires Salt events. .. _OpenConfig: http://www.openconfig.net/ As there can be many messages pushed into the event bus, the user is able to filter based on the object structure. Requirements ------------ - `napalm-logs`_ .. _`napalm-logs`: https://github.com/napalm-automation/napalm-logs This engine transfers objects from the napalm-logs library into the event bus. The top dictionary has the following keys: - ``ip`` - ``host`` - ``timestamp`` - ``os``: the network OS identified - ``model_name``: the OpenConfig or IETF model name - ``error``: the error name (consult the documentation) - ``message_details``: details extracted from the syslog message - ``open_config``: the OpenConfig model The napalm-logs transfers the messages via widely used transport mechanisms such as: ZeroMQ (default), Kafka, etc. The user can select the right transport using the ``transport`` option in the configuration. :configuration: Example configuration .. code-block:: yaml engines: - napalm_syslog: transport: zmq address: 1.2.3.4 port: 49018 :configuration: Configuration example, excluding messages from IOS-XR devices: .. code-block:: yaml engines: - napalm_syslog: transport: kafka address: 1.2.3.4 port: 49018 os_blacklist: - iosxr Event example: .. code-block:: json { "_stamp": "2017-05-26T10:03:18.653045", "error": "BGP_PREFIX_THRESH_EXCEEDED", "host": "vmx01", "ip": "192.168.140.252", "message_details": { "date": "May 25", "host": "vmx01", "message": "192.168.140.254 (External AS 65001): Configured maximum prefix-limit threshold(22) exceeded for inet-unicast nlri: 28 (instance master)", "pri": "28", "processId": "2957", "processName": "rpd", "tag": "BGP_PREFIX_THRESH_EXCEEDED", "time": "20:50:41" }, "model_name": "openconfig_bgp", "open_config": { "bgp": { "neighbors": { "neighbor": { "192.168.140.254": { "afi_safis": { "afi_safi": { "inet": { "afi_safi_name": "inet", "ipv4_unicast": { "prefix_limit": { "state": { "max_prefixes": 22 } } }, "state": { "prefixes": { "received": 28 } } } } }, "neighbor_address": "192.168.140.254", "state": { "peer_as": 65001 } } } } } }, "os": "junos", "timestamp": "1495741841" } To consume the events and eventually react and deploy a configuration changes on the device(s) firing the event, one is able to identify the minion ID, using one of the following alternatives, but not limited to: - :mod:`Host grains <salt.grains.napalm.host>` to match the event tag - :mod:`Host DNS grain <salt.grains.napalm.host_dns>` to match the IP address in the event data - :mod:`Hostname grains <salt.grains.napalm.hostname>` to match the event tag - :ref:`Define static grains <static-custom-grains>` - :ref:`Write a grains module <writing-grains>` - :ref:`Targeting minions using pillar data <targeting-pillar>` - The user can configure certain information in the Pillar data and then use it to identify minions Master configuration example, to match the event and react: .. code-block:: yaml reactor: - 'napalm/syslog/*/BGP_PREFIX_THRESH_EXCEEDED/*': - salt://increase_prefix_limit_on_thresh_exceeded.sls Which matches the events having the error code ``BGP_PREFIX_THRESH_EXCEEDED`` from any network operating system, from any host and reacts, executing the ``increase_prefix_limit_on_thresh_exceeded.sls`` reactor, found under one of the :conf_master:`file_roots` paths. Reactor example: .. code-block:: yaml increase_prefix_limit_on_thresh_exceeded: local.net.load_template: - tgt: "hostname:{{ data['host'] }}" - tgt_type: grain - kwarg: template_name: salt://increase_prefix_limit.jinja openconfig_structure: {{ data['open_config'] }} The reactor in the example increases the BGP prefix limit when triggered by an event as above. The minion is matched using the ``host`` field from the ``data`` (which is the body of the event), compared to the :mod:`hostname grain <salt.grains.napalm.hostname>` field. When the event occurs, the reactor will execute the :mod:`net.load_template <salt.modules.napalm_network.load_template>` function, sending as arguments the template ``salt://increase_prefix_limit.jinja`` defined by the user in their environment and the complete OpenConfig object under the variable name ``openconfig_structure``. Inside the Jinja template, the user can process the object from ``openconfig_structure`` and define the bussiness logic as required. � N)�zmqTFZ napalm_syslogc C s t rtsdS dS )z0 Load only if napalm-logs is installed. )FzQnapalm_syslog could not be loaded. Please install napalm-logs library and ZeroMQ.T)�HAS_NAPALM_LOGSr � r r �N/opt/saltstack/salt/lib/python3.10/site-packages/salt/engines/napalm_syslog.py�__virtual__� s r c K sR t �� }|�t j�}tjj�| �rd|_|� d| � d|� �� |� t jd� |jS )NTztcp://�:� ) r ZContext�socketZSUB�salt�utils�networkZis_ipv6Zipv6�connect� setsockoptZ SUBSCRIBE�recv)�address�port�kwargs�contextr r r r �_zmq� s r r �0.0.0.0�y� c K s. | t vrt�d| � d} t | ||fi |��S )Nz.Invalid transport: %s. Falling back to ZeroMQ.r )�TRANSPORT_FUN_MAP�log�error)�namer r r r r r �_get_transport_recv� s r )r Zzeromq�z� c C s� |s|st �d� dS tjj|||d�} t| ||d�}|s&t jddd� dS d}td d kr0d} t �d� |� }t �d� t �|� |sK| �|�}ntj� |�}zd|d }|sZ|rnt jjj|||d�}|snt � d|� W q0|d }| sv| r�t jjj|| | d�}|s�t � d|� W q0|�d�p�|�d�}|s�|r�t jjj|||d�}|s�t � d|� W q0dj|||d�}W n ty� } zt jddd� t �|� W Y d}~q0d}~ww t �d|� t �|� |r�t�ttd ��||� ntd ||� q1)a� 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. z2Please use a certificate, or disable the security.N)r r )r r r zUnable to start the engineT)�exc_infoFZ__role�masterz+Waiting for napalm-logs to send anything...zReceived from napalm-logs:�os)Z whitelistZ blacklistz*Ignoring NOS %s as per whitelist/blacklistr z,Ignoring error %s as per whitelist/blacklist�host�ipz4Ignoring messages from %s as per whitelist/blacklistz!napalm/syslog/{os}/{error}/{host})r r r z)Missing keys from the napalm-logs object:zSending event %sZsock_dirz event.send)r �critical�napalm_logsr Z ClientAuthr Z__opts__�debugZdecryptZunserializer ZstringutilsZcheck_whitelist_blacklist�info�get�format�KeyError�warning�eventZget_master_eventZ fire_eventZ__salt__)Z transportr r Zauth_addressZ auth_portZdisable_securityZcertificateZos_whitelistZos_blacklistZerror_whitelistZerror_blacklistZhost_whitelistZhost_blacklistZauthZtransport_recv_funr Z raw_objectZdict_objectZevent_osZvalid_osZevent_errorZvalid_errorZ event_hostZ valid_host�tagZkerrr r r �start� s� > � ����� � �� ��r, )r r r ) r r r r r FNNNNNNN)�__doc__�loggingZsalt.utils.eventr r* Zsalt.utils.networkr Zsalt.utils.stringutilsZsalt.utils.zeromqr r# Znapalm_logs.utilsr �ImportError� getLogger�__name__r Z__virtualname__r r r r r, r r r r �<module> sD +� �
| ver. 1.4 |
Github
|
.
| PHP 8.2.30 | Generation time: 0 |
proxy
|
phpinfo
|
Settings