o
    Ìv›aå  ã                   @   s`   d dl Z d dlmZ zd dlmZ W n ey%   e d¡ e  d¡ Y nw G dd„ deƒZ	dS )é    N)Úlogger)Úcmdgenz;PySNMP library not found. To install it: pip install pysnmpé   c                       sN   e Zd ZdZ		d‡ fdd	„	Zd
d„ Zdd„ Zdd„ Zdd„ Zdd„ Z	‡  Z
S )ÚGlancesSNMPClientz,SNMP client class (based on pysnmp library).Ú	localhosté¡   Ú2cÚpublicÚprivateÚ c                    s@   t t| ƒ ¡  t ¡ | _|| _|| _|| _|| _	|| _
|| _d S ©N)Úsuperr   Ú__init__r   ZCommandGeneratorÚcmdGenÚversionÚhostÚportÚ	communityÚuserÚauth)Úselfr   r   r   r   r   r   ©Ú	__class__© ú./usr/lib/python3/dist-packages/glances/snmp.pyr   $   s   

zGlancesSNMPClient.__init__c                 C   sn   i }|D ]0\}}t |ƒdkrd|| ¡ < q| ¡ || ¡ < || ¡   d¡r4|| ¡  dd… || ¡ < q|S )zBuild the results.r   zb'r   éÿÿÿÿ)ÚstrZprettyPrintÚ
startswith)r   ÚvarBindsÚretÚnameÚvalr   r   r   Z__buid_result3   s   €zGlancesSNMPClient.__buid_resultc                 C   s   i }|r|s|   |¡}|S )zPut results in table.)Ú_GlancesSNMPClient__buid_result)r   ÚerrorIndicationÚerrorStatusÚ
errorIndexr   r   r   r   r   Ú__get_result__@   s   
z GlancesSNMPClient.__get_result__c                 G   sˆ   | j dkr"| jjt | j| j¡t | j| j	f¡g|¢R Ž \}}}}n| jjt 
| j¡t | j| j	f¡g|¢R Ž \}}}}|  ||||¡S )zSNMP simple request (list of OID).

        One request per OID list.

        * oid: oid list
        > Return a dict
        Ú3)r   r   ÚgetCmdr   ÚUsmUserDatar   r   ÚUdpTransportTargetr   r   ÚCommunityDatar   r&   )r   Úoidr#   r$   r%   r   r   r   r   Ú
get_by_oidG   s   
þý
þýzGlancesSNMPClient.get_by_oidc                 C   s*   g }|r|s|D ]
}|  |  |¡¡ q|S r   )Úappendr"   )r   r#   r$   r%   ÚvarBindTabler   ZvarBindTableRowr   r   r   Ú__bulk_result__]   s
   z!GlancesSNMPClient.__bulk_result__c           	      G   s¢   | j  d¡r$| jjt | j| j¡t | j	| j
f¡||g|¢R Ž \}}}}| j  d¡rG| jjt | j¡t | j	| j
f¡||g|¢R Ž \}}}}ng S |  ||||¡S )aö  SNMP getbulk request.

        In contrast to snmpwalk, this information will typically be gathered in
        a single transaction with the agent, rather than one transaction per
        variable found.

        * non_repeaters: This specifies the number of supplied variables that
          should not be iterated over.
        * max_repetitions: This specifies the maximum number of iterations over
          the repeating variables.
        * oid: oid list
        > Return a list of dicts
        r'   Ú2)r   r   r   r(   r   r)   r   r   r*   r   r   ZbulkCmdr+   r   r0   )	r   Znon_repeatersZmax_repetitionsr,   r#   r$   r%   r   r/   r   r   r   Úgetbulk_by_oidd   s(   üû
üû	z GlancesSNMPClient.getbulk_by_oid)r   r   r   r	   r
   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r"   r&   r-   r0   r2   Ú__classcell__r   r   r   r   r       s    ÿr   )
ÚsysZglances.loggerr   Zpysnmp.entity.rfc3413.onelinerr   ÚImportErrorZcriticalÚexitÚobjectr   r   r   r   r   Ú<module>   s   
þ