%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python2.7/site-packages/salt/beacons/
Upload File :
Create Path :
Current File : //lib/python2.7/site-packages/salt/beacons/napalm_beacon.pyo

�
���^c@@s�dZddlmZmZddlZddlZddlmZddlZ	ej
e�Zej
d�Zidd6dd	6d
d6dd
6dd6dd6ZdZd�Zd�Zd�Zd�ZdS(u~
Watch NAPALM functions and fire events on specific triggers
===========================================================

.. versionadded:: 2018.3.0


.. note::

    The ``NAPALM`` beacon only works only when running under
    a regular Minion or a Proxy Minion, managed via NAPALM_.
    Check the documentation for the
    :mod:`NAPALM proxy module <salt.proxy.napalm>`.

    .. _NAPALM: http://napalm.readthedocs.io/en/latest/index.html

The configuration accepts a list of Salt functions to be
invoked, and the corresponding output hierarchy that should
be matched against. To invoke a function with certain
arguments, they can be specified using the ``_args`` key, or
``_kwargs`` for more specific key-value arguments.

The match structure follows the output hierarchy of the NAPALM
functions, under the ``out`` key.

For example, the following is normal structure returned by the
:mod:`ntp.stats <salt.modules.napalm_ntp.stats>` execution function:

.. code-block:: json

    {
        "comment": "",
        "result": true,
        "out": [
            {
                "referenceid": ".GPSs.",
                "remote": "172.17.17.1",
                "synchronized": true,
                "reachability": 377,
                "offset": 0.461,
                "when": "860",
                "delay": 143.606,
                "hostpoll": 1024,
                "stratum": 1,
                "jitter": 0.027,
                "type": "-"
            },
            {
                "referenceid": ".INIT.",
                "remote": "172.17.17.2",
                "synchronized": false,
                "reachability": 0,
                "offset": 0.0,
                "when": "-",
                "delay": 0.0,
                "hostpoll": 1024,
                "stratum": 16,
                "jitter": 4000.0,
                "type": "-"
            }
        ]
    }

In order to fire events when the synchronization is lost with
one of the NTP peers, e.g., ``172.17.17.2``, we can match it explicitly as:

.. code-block:: yaml

    ntp.stats:
      remote: 172.17.17.2
      synchronized: false

There is one single nesting level, as the output of ``ntp.stats`` is
just a list of dictionaries, and this beacon will compare each dictionary
from the list with the structure examplified above.

.. note::

    When we want to match on any element at a certain level, we can
    configure ``*`` to match anything.

Considering a more complex structure consisting on multiple nested levels,
e.g., the output of the :mod:`bgp.neighbors <salt.modules.napalm_bgp.neighbors>`
execution function, to check when any neighbor from the ``global``
routing table is down, the match structure would have the format:

.. code-block:: yaml

    bgp.neighbors:
      global:
        '*':
          up: false

The match structure above will match any BGP neighbor, with
any network (``*`` matches any AS number), under the ``global`` VRF.
In other words, this beacon will push an event on the Salt bus
when there's a BGP neighbor down.

The right operand can also accept mathematical operations
(i.e., ``<``, ``<=``, ``!=``, ``>``, ``>=`` etc.) when comparing
numerical values.

Configuration Example:

.. code-block:: yaml

    beacons:
      napalm:
        - net.interfaces:
            # fire events when any interfaces is down
            '*':
              is_up: false
        - net.interfaces:
            # fire events only when the xe-0/0/0 interface is down
            'xe-0/0/0':
              is_up: false
        - ntp.stats:
            # fire when there's any NTP peer unsynchornized
            synchronized: false
        - ntp.stats:
            # fire only when the synchronization
            # with with the 172.17.17.2 NTP server is lost
            _args:
              - 172.17.17.2
            synchronized: false
        - ntp.stats:
            # fire only when there's a NTP peer with
            # synchronization stratum > 5
            stratum: '> 5'

Event structure example:

.. code-block:: json

    {
        "_stamp": "2017-09-05T09:51:09.377202",
        "args": [],
        "data": {
            "comment": "",
            "out": [
                {
                    "delay": 0.0,
                    "hostpoll": 1024,
                    "jitter": 4000.0,
                    "offset": 0.0,
                    "reachability": 0,
                    "referenceid": ".INIT.",
                    "remote": "172.17.17.1",
                    "stratum": 16,
                    "synchronized": false,
                    "type": "-",
                    "when": "-"
                }
            ],
            "result": true
        },
        "fun": "ntp.stats",
        "id": "edge01.bjm01",
        "kwargs": {},
        "match": {
            "stratum": "> 5"
        }
    }

The event examplified above has been fired when the device
identified by the Minion id ``edge01.bjm01`` has been synchronized
with a NTP server at a stratum level greater than 5.
i(tabsolute_importtunicode_literalsN(tsixu'^(<|>|<=|>=|==|!=)\s*(\d+(\.\d+){0,1})$u__lt__u<u__gt__u>u__ge__u>=u__le__u<=u__eq__u==u__ne__u!=unapalmcC@stjjjttt�S(uk
    This beacon can only work when running under a regular or a proxy minion, managed through napalm.
    (tsalttutilstnapalmtvirtualt__opts__t__virtualname__t__file__(((s>/usr/lib/python2.7/site-packages/salt/beacons/napalm_beacon.pyt__virtual__�sc
C@s+t|t�r�t|t�r�tjd�x�tj|�D]\}}|dkr-t|t�r�t}x0tj|�D]\}}|t||�O}qxW|St}t|tt	f�r�xi|D]}|t||�O}q�WnEt|t�r&x3tj|�D]\}}|t||�O}qWn|Sq;t|t�r]||krLtSt|||�St|t�r�t}x0tj|�D]\}}|t||�O}q�W|St|||�Sq;Wnft|tt	f�r7t|tt	f�r7tjd�t}x2|D]*}x!|D]}	|t||	�O}qWqW|St|t�r�t|tt	f�r�tjd�t}x!|D]}	|t||	�O}quW|St|t
�r�t|t
�r�tjd||�||kSt|tjtjf�r@t|tjtjf�r@tjd||�t
j||t
j�}
|
r<tStSt|tjtf�r�t|tjtf�r�tjd||�||kSt|tjtf�r't|tjtjf�r'tjd||�tj|�}|r#|jd	�}tt|�t|jd
��t|��StStS(uS
    Compares two objects and return a boolean value
    when there's a match.
    uComparing dict to dictu*uComparing list to listu"Comparing dict to list (of dicts?)uComparing booleans: %s ? %su'Comparing strings (and regex?): %s ? %su!Comparing numeric values: %d ? %du1Comparing a numeric value (%d) with a string (%s)ii(t
isinstancetdicttlogtdebugRt	iteritemstFalset_comparetlistttupletbooltstring_typest	text_typetretmatchtItTruet
integer_typestfloatt_numeric_regextgrouptgetattrt_numeric_operand(
tcur_cmpt
cur_structtcmp_keyt	cmp_valuetfoundt_tcur_struct_valtcur_eletcur_cmp_eletcur_struct_eletmatchedtnumeric_comparet
compare_value((s>/usr/lib/python2.7/site-packages/salt/beacons/napalm_beacon.pyR�s~

*


$



,cC@s�t|t�stdfSxo|D]g}|j�d}|j�d}t|t�shtdj|�fS|tkr tdj|�fSq WtdfS(u,
    Validate the beacon configuration.
    u/Configuration for napalm beacon must be a list.iuMThe match structure for the {} execution function output must be a dictionaryu&Execution function {} is not availabe!u)Valid configuration for the napal beacon!(	RRRtkeystvaluesRtformatt__salt__R(tconfigtmodtfuntfun_cfg((s>/usr/lib/python2.7/site-packages/salt/beacons/napalm_beacon.pytvalidates

c	C@sOtjd�tj|�g}x|D]}|s9q'ni}|j�d}|j�d}|jdg�}|jdi�}tjdjd|d|d|��t|||�}tjd	�tj|�|jd
t�stj	dj|��tj	|�q'n|d}	tjd
�tj|�yt
||	�}
Wn)tk
rr}tj	|dt�q'nXtjdjd|
��|
r'tj
djd|d|��djdtdd|�|d<||d<||d<||d<||d<||d<tjd�tj|�|j|�q'q'Wtjd�tj|�|S(u0
    Watch napalm function and fire events.
    u$Executing napalm beacon with config:iu_argsu_kwargsu(Executing {fun} with {args} and {kwargs}R4targstkwargsuGot the reply from the minion:uresultuError whilst executing {}uoutu
Comparing to:texc_infouResult of comparison: {res}tresuMatched {fun} with {cfg}tcfgu
{os}/{fun}tosuosutagufunuargsukwargsudataumatchuQueueing event:u#NAPALM beacon generated the events:(R
RR.R/tpopR0R1tgetRterrorRt	ExceptionRtinfot
__grains__tappend(R2tretR3teventR4R5R7R8tfun_rettfun_ret_outtfun_cmp_resultterr((s>/usr/lib/python2.7/site-packages/salt/beacons/napalm_beacon.pytbeacon*s^










 








(t__doc__t
__future__RRRtloggingtsalt.extRtsalt.utils.napalmRt	getLoggert__name__R
tcompileRR RR
RR6RJ(((s>/usr/lib/python2.7/site-packages/salt/beacons/napalm_beacon.pyt<module>�s&
		N	

Zerion Mini Shell 1.0