o
    Ìv›ar"  ã                   @   sˆ   d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	m
Z
 ddlmZ ddlmZ ddlmZ G dd	„ d	e
ƒZG d
d„ deƒZdS )zManage the Glances client.é    N)Ú__version__)ÚFaultÚProtocolErrorÚServerProxyÚ	Transport)Úlogger)ÚGlancesStatsClient)ÚGlancesCursesClientc                   @   s   e Zd ZdZdd„ ZdS )ÚGlancesClientTransportzFThis class overwrite the default XML-RPC transport and manage timeout.c                 C   s
   || _ d S ©N)Útimeout)Úselfr   © r   ú0/usr/lib/python3/dist-packages/glances/client.pyÚset_timeout%   s   
z"GlancesClientTransport.set_timeoutN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r
   !   s    r
   c                   @   sŠ   e Zd ZdZddd„Zedd„ ƒZd d
d„Zedd„ ƒZej	dd„ ƒZdd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )!ÚGlancesClientz.This class creates and manages the TCP client.Né   Fc              
   C   sÌ   || _ || _|j| _d| _|| _|jdkr#d |j|j|j	|j
¡| _n	d |j	|j
¡| _t d | j¡¡ tƒ }| |¡ zt| j|d| _	W d S  tye } z|  d | j|¡¡ W Y d }~d S d }~ww )NÚglancesÚ zhttp://{}:{}@{}:{}zhttp://{}:{}zTry to connect to {})Ú	transportz$Client couldn't create socket {}: {})ÚargsÚconfigÚquietÚ_quietÚ_client_modeÚreturn_to_browserZpasswordÚformatZusernameÚclientZportÚurir   Údebugr
   r   r   Ú	ExceptionÚlog_and_exit)r   r   r   r   r   r   Úer   r   r   Ú__init__-   s&   
ÿ
"€ÿzGlancesClient.__init__c                 C   ó   | j S r   )r   ©r   r   r   r   r   K   s   zGlancesClient.quietr   c                 C   s,   | j st |¡ t d¡ dS t |¡ dS )zLog and exit.é   N)r   r   ÚcriticalÚsysÚexitÚerror)r   Úmsgr   r   r   r%   O   s   
zGlancesClient.log_and_exitc                 C   r(   )zGet the client mode.©r   r)   r   r   r   Úclient_modeW   s   zGlancesClient.client_modec                 C   s
   || _ dS )zpSet the client mode.

        - 'glances' = Glances server (default)
        - 'snmp' = SNMP (fallback)
        Nr0   )r   Úmoder   r   r   r1   \   s   
c              
   C   s^  d}z| j  ¡ }W ne tjy= } z'd| _t d |j|j¡¡ d | j	¡}| j
s.t|ƒ nt |¡ W Y d}~n6d}~w tyn } z&d | j	¡}|jdkrT|d7 }n
|d |j|j¡7 }|  |¡ W Y d}~d	S d}~ww | jd
kr­t d¡d | d¡d kr¢t| j| jd| _| j t | j  ¡ ¡¡ t d t|¡¡ dS |  d t|¡¡ d	S dS )zLogin to a Glances serverNÚsnmpz+Connection to Glances server failed ({} {})z9No Glances server found on {}. Trying fallback to SNMP...zConnection to server {} failedi‘  z (Bad username/password)z ({} {})Fr   Ú.r   ©r   r   z'Client version: {} / Server version: {}zIClient and server not compatible: Client version: {} / Server version: {}T)r!   ZinitÚsocketr.   r1   r   r    ÚerrnoÚstrerrorr"   r   ÚprintÚinfor   ZerrcodeÚerrmsgr%   r   Úsplitr   r   r   ÚstatsZset_pluginsÚjsonÚloadsZgetAllPluginsr#   )r   Zclient_versionÚerrZfallbackmsgr/   r   r   r   Ú_login_glancese   s>   

€


€ø

üÿzGlancesClient._login_glancesc                 C   sD   t  d¡ ddlm} || j| jd| _| j ¡ s |  d¡ dS dS )zLogin to a SNMP serverzTrying to grab stats by SNMP...r   )ÚGlancesStatsClientSNMPr5   z Connection to SNMP server failedFT)	r   r:   Zglances.stats_client_snmprB   r   r   r=   Z
check_snmpr%   )r   rB   r   r   r   Ú_login_snmp‹   s   


zGlancesClient._login_snmpc                 C   st   | j jrd| _n|  ¡ sdS | jdkr|  ¡ sdS t d¡ | j | j	¡ | j
r/t d¡ dS t| j	| j d| _dS )zLogon to the server.r3   Fz.Load limits from the client configuration filez+Quiet mode is ON: Nothing will be displayedr5   T)r   Z
snmp_forcer1   rA   rC   r   r#   r=   Zload_limitsr   r   r:   r	   Úscreenr)   r   r   r   Úloginš   s   


ýzGlancesClient.loginc                 C   sL   | j dkr	|  ¡ S | j dkr|  ¡ S |  ¡  t d | j ¡¡ t d¡ dS )z&Update stats from Glances/SNMP server.r   r3   zUnknown server mode: {}r*   N)	r1   Úupdate_glancesÚupdate_snmpÚendr   r+   r    r,   r-   r)   r   r   r   Úupdate¹   s   

zGlancesClient.updatec                 C   sN   z
t  | j ¡ ¡}W n tjy   Y dS  ty   Y dS w | j |¡ dS )z¦Get stats from Glances server.

        Return the client/server connection status:
        - Connected: Connection OK
        - Disconnected: Connection NOK
        ÚDisconnectedZ	Connected)	r>   r?   r!   ZgetAllr6   r.   r   r=   rI   )r   Zserver_statsr   r   r   rF   Ä   s   þzGlancesClient.update_glancesc                 C   s&   z| j  ¡  W dS  ty   Y dS w )z¯Get stats from SNMP server.

        Return the client/server connection status:
        - SNMP: Connection with SNMP server OK
        - Disconnected: Connection NOK
        rJ   ZSNMP)r=   rI   r$   r)   r   r   r   rG   Ù   s   ûþzGlancesClient.update_snmpc              
   C   sª   |   ¡ st d¡ |  ¡  | jS d}z%|s4|  ¡ }| js'| jj| j|| j	d}| j 
| j¡ |rW | jS W | jS  tyT } zt |¡ |  ¡  W Y d}~| jS d}~ww )zMain client loop.z4The server version is not compatible with the clientFT)Ú	cs_statusr   N)rE   r   r+   rH   r1   rI   r   rD   r=   r   Zexportr$   )r   ZexitkeyrK   r&   r   r   r   Úserve_foreverê   s0   

þõðü
€üzGlancesClient.serve_foreverc                 C   s   | j s
| j ¡  dS dS )zEnd of the client session.N)r   rD   rH   r)   r   r   r   rH     s   ÿzGlancesClient.end)NNr   F)r   )r   r   r   r   r'   Úpropertyr   r%   r1   ÚsetterrA   rC   rE   rI   rF   rG   rL   rH   r   r   r   r   r   )   s$    




&r   )r   r>   r6   r,   r   r   Zglances.compatr   r   r   r   Zglances.loggerr   Zglances.stats_clientr   Zglances.outputs.glances_cursesr	   r
   Úobjectr   r   r   r   r   Ú<module>   s   