HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/states/__pycache__/netsnmp.cpython-310.pyc
o

�N�g#-�@s�dZddlZddlZddlZe�e�ZdZddddd�Z	dd�Z
d	d
�Zdd�Zd
d�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd%d#d$�ZdS)&aO
Network SNMP
============

Manage the SNMP configuration on network devices.

:codeauthor: Mircea Ulinic <ping@mirceaulinic.net>
:maturity:   new
:depends:    napalm
:platform:   unix

Dependencies
------------

- :mod:`napalm snmp management module (salt.modules.napalm_snmp) <salt.modules.napalm_snmp>`

.. versionadded:: 2016.11.0
�NZnetsnmp�ro�rw)z	read-only�readonlyz
read-write�writecCstjj�ttt�S)z_
    NAPALM library must be installed for this module to work and run in a (proxy) minion.
    )�salt�utilsZnapalmZvirtual�__opts__�__virtualname__�__file__�rr�G/opt/saltstack/salt/lib/python3.10/site-packages/salt/states/netsnmp.py�__virtual__2sr
cCstjj�tjj�|��S)zC
    Forced the datatype to dict, in case OrderedDict is used.
    )rr�json�loads�dumps)�configrrr�_ordered_dict_to_dict>srcCs|�|�|S)za
    Completed the values of the expected config for the edge cases with the default values.
    )�update)r�defaultsrrr�_expand_configFs
rcC�t|t�o
t|�dkS)z
    Valid dictionary?
    r)�
isinstance�dict�len)Zdicrrr�_valid_dictO�rcCr)z
    Valid str?
    r)r�strr)�valuerrr�
_valid_strWrrcCsddiS)z4
    Returns the default values of a community.
    �moderrrrrr�_community_defaults_sr cCsZdD]}t||�q|�d���}|jd<|tvr!t�|�|d<|ddvr+d|d<|S)z#
    Clears community details.
    )Zaclrr)rrr)�	_str_elem�get�lower�_COMMUNITY_MODE_MAP)Zcommunity_details�key�_moderrr�_clear_community_detailsgsr'cCs$|�|d�}t|�r|||<dSdS)z[
    Re-adds the value of a specific key in the dict, only in case of valid str value.
    �N)�popr)rr%�_valuerrrr!zs�r!cCs�t|�sdS|�d�}i}|sdSt|�rt�||<nEt|t�rF|D]"}t|�r-t�||<t|�rD|��D]\}}t|�rCt|�||<q5q"nt|�r^|��D]\}}t|�r\t|�||<qNndSt|�sfdS||d<dD]}t||�qldS)zC
    Checks the desired config and clears interesting details.
    )Tr(�	community)Fz"Must specify at least a community.)Fz4Please specify a community or a list of communities.)Fz*Please specify at least a valid community!��location�contact�
chassis_id)	rr"rr r�list�itemsr'r!)r�
_communityZ_community_tmpZ_commZ
_comm_nameZ
_comm_detailsr%rrr�
_check_config�sB


�����r3cCs
td�S)z4
    Retrieves the SNMP config from the device.
    zsnmp.config)�__salt__rrrr�_retrieve_device_config�s
r5cCs ||vri||<||||<dS)z<
    DRY to build diff parts (added, removed, updated).
    Nr)�diffZdiff_keyr%rrrr�_create_diff_action�sr7cCsZ||�s
t|d||�dS||�r||�st|d||�dS||�s+t|d||�dSdS)z%
    Builds the diff dictionary.
    �added�removed�updatedN)r7)r6�funr%�prev�currrrr�_create_diff�s�r>c	Cs|i}dD]}|�|�|�|�krt|t||�|�|�|��qdD]}|�|�|�|�kr;t|t||�|�|�|��q!|S)zU
    Computes the differences between the existing and the expected SNMP config.
    r,)r+)r"r>rr)�existing�expectedr6r%rrr�
_compute_diff�s��rAcCs�d}g}d}dD]P}|�|i�}|sq|�dd�}|�dd�}|�di�}|�dd�}	|d	kr1d
}td|��||||	dd
�}
|oE|
�d�}|
�d�sX|
�d�rX|�|
�d��q||rcd�|�d�Sdd�S)z\
    Calls the configuration template to apply the configuration changes on the device.
    TZ
update_config)r8r:r9r-r(r.r+r/r9Z
remove_configzsnmp.F)r-r.r+r/Zcommit�result�comment�
�rBrC)r"r4�append�join)�changesZcfgredZreasonsr;r%Z_updated_changesZ	_locationZ_contactr2Z_chassis_idZ_retrrr�
_configure�s2��rIcCsfd}d}i}||||d�}t|�}t|�}t||�}t|t�s%d|d<|St|�\}}	|s7dj|	d�|d<|St�}
|
�d�sLd	j|
�d�d
�|d<|S|
�di�}||kr`|�dd
d��|St	||�}|�|�|�d|i�t
dd
ur�|�ddd��|Sd}
d
}|r�t|�}|�d�r�d
}
n
d}dj|�d�d
�|}|
r�td�\}}|�||d��|S)a9
    Configures the SNMP on the device as specified in the SLS file.

    SLS Example:

    .. code-block:: yaml

        snmp_example:
            netsnmp.managed:
                 - config:
                    location: Honolulu, HI, US
                 - defaults:
                    contact: noc@cloudflare.com

    Output example (for the SLS above, e.g. called snmp.sls under /router/):

    .. code-block:: bash

        $ sudo salt edge01.hnl01 state.sls router.snmp test=True
        edge01.hnl01:
        ----------
                  ID: snmp_example
            Function: snmp.managed
              Result: None
             Comment: Testing mode: configuration was not changed!
             Started: 13:29:06.872363
            Duration: 920.466 ms
             Changes:
                      ----------
                      added:
                          ----------
                          chassis_id:
                              None
                          contact:
                              noc@cloudflare.com
                          location:
                              Honolulu, HI, US

        Summary for edge01.hnl01
        ------------
        Succeeded: 1 (unchanged=1, changed=1)
        Failed:    0
        ------------
        Total states run:     1
        Total run time: 920.466 ms
    Fr()�namerHrBrCz#User provided an empty SNMP config!rCz-Please provide a valid configuration: {error})�errorrBz5Cannot retrieve SNMP config from the device: {reason})�reason�outz"SNMP already configured as needed.T)rCrBrH�testNz,Testing mode: configuration was not changed!rEz&Cannot push new SNMP config: 
{reason}znet.config_control)
rrrrr3�formatr5r"rrArrIr4)rJrrrBrCrH�retZexpected_configZvalid�messageZ_device_configZ
device_configr6Zexpected_config_changeZ_configuredrrr�managedsf0


�

�

�
���rR)NN)�__doc__�loggingZsalt.utils.jsonrZsalt.utils.napalm�	getLogger�__name__�logr	r$r
rrrrr r'r!r3r5r7r>rArIrRrrrr�<module>s4
�	
1

&