o
    Ìv›a]*  ã                   @   s”   d Z ddlZddlZddlZddlmZmZmZ ddlm	Z	m
Z
 ddlmZmZ ddlmZ ddlmZ ddlmZ dd	lmZ G d
d„ deƒZdS )z;Manage the Glances client browser (list of Glances server).é    N)ÚFaultÚProtocolErrorÚServerProxy)ÚGlancesClientÚGlancesClientTransport)ÚloggerÚLOG_FILENAME)ÚGlancesPasswordList)ÚGlancesStaticServer)ÚGlancesAutoDiscoverServer)ÚGlancesCursesBrowserc                   @   sb   e Zd ZdZd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d„ ZdS )ÚGlancesClientBrowserzEThis class creates and manages the TCP client browser (servers list).Nc                 C   sJ   || _ || _d | _d | _|  ¡  | j jstƒ | _nd | _t| j d| _	d S )N)Úargs)
r   ÚconfigÚstatic_serverÚpasswordÚloadZdisable_autodiscoverr   Úautodiscover_serverr   Úscreen)Úselfr   r   © r   ú8/usr/lib/python3/dist-packages/glances/client_browser.pyÚ__init__'   s   
zGlancesClientBrowser.__init__c                 C   s    t | jd| _t| jd| _dS )z9Load server and password list from the confiuration file.)r   N)r
   r   r   ÚGlancesPasswordr   ©r   r   r   r   r   :   s   zGlancesClientBrowser.loadc                 C   s8   g }| j jr| j ¡ }| jdur| j ¡ | j ¡  }|S )zmReturn the current server list (list of dict).

        Merge of static + autodiscover servers list.
        N)r   Zbrowserr   Úget_servers_listr   )r   Úretr   r   r   r   B   s   

z%GlancesClientBrowser.get_servers_listc                 C   st   |d dkr0|d dkr | j  |d ¡}|dur | j  |¡|d< d |d |d |d	 |d
 ¡S d |d	 |d
 ¡S )z)Return the URI for the given server dict.r   Ú ÚstatusÚ	PROTECTEDÚnameNzhttp://{}:{}@{}:{}ÚusernameÚipÚportzhttp://{}:{})r   Úget_passwordÚsha256_hashÚformat)r   ÚserverÚclear_passwordr   r   r   Z	__get_uriP   s   ÿzGlancesClientBrowser.__get_uric              
   C   sÊ  |   |¡}tƒ }| d¡ zt||d}W n ty1 } zt d ||¡¡ W Y d}~|S d}~ww z*dt 	| 
¡ ¡d  }d |¡|d< t 	| ¡ ¡d	 |d
< t 	| ¡ ¡d |d< W nT tjttfy€ } zt d ||¡¡ d|d< W Y d}~|S d}~w ty° } z%|jdkr•d|d< d|d< nd|d< t d ||j|j¡¡ W Y d}~|S d}~ww d|d< zt 	| ¡ ¡d }d |¡|d< W |S  tyä } zt d ||¡¡ W Y d}~|S d}~ww )zQ
        Update stats for the given server (picked from the server list)
        é   )Z	transportz,Client browser couldn't create socket {}: {}Néd   Zidlez{:.1f}Úcpu_percentÚpercentZmem_percentZhr_namez&Error while grabbing stats form {}: {}ÚOFFLINEr   i‘  r   r   z!Cannot grab stats from {} ({} {})ÚONLINEZmin5z{:.2f}Ú	load_min5)Ú_GlancesClientBrowser__get_urir   Zset_timeoutr   Ú	Exceptionr   Zwarningr&   ÚjsonÚloadsZgetCpuZgetMemZ	getSystemÚsocketÚerrorr   ÚKeyErrorÚdebugr   ZerrcodeÚerrmsgZgetLoad)r   r'   ZuriÚtÚsÚer+   r/   r   r   r   Z__update_stats^   sZ   


ÿ&€Ù
ÿ€ê

"€êü
ÿ€üz#GlancesClientBrowser.__update_statsc                 C   s„  t  d |¡¡ | jjd |d |d ¡dd |d du rQ| j |d ¡}|du s6|  ¡ | jj d	 d
krC| jjd |d ¡dd}|durQ|  	d| j 
|¡¡ t  d |d ¡¡ | j}|d |_|d |_|d |_|d |_t| j|dd}| ¡ s‘| j d |d t¡¡ |  	d	d¡ n+| ¡ }zt  d |d ¡¡ W n	 tyª   Y nw |dkr¶|  	d	d¡ n|  	d	d¡ d| j_dS )z6
        Connect and display the given server
        zSelected server {}zConnect to {}:{}r    r#   é   )Zdurationr   Nr   r   zPassword needed for {}: T)Zis_inputz'Connect Glances client to the {} serverÚkeyr"   r!   )r   r   Zreturn_to_browserz7Sorry, cannot connect to '{}'
See '{}' for more detailsr-   z,Disconnect Glances client from the {} serverZsnmpZSNMPr.   )r   r7   r&   r   Zdisplay_popupr   r$   r   Úactive_serverÚset_in_selectedr%   Úinfor   Úclientr#   r!   r   r   Zloginr   Úserve_foreverÚ
IndexError)r   r'   r(   Zargs_serverrA   Zconnection_typer   r   r   Z__display_server•   sR   ÿÿÿÿÿ



þþz%GlancesClientBrowser.__display_serverc                 C   sÖ   i }| j jdkr^t d |  ¡ ¡¡ |  ¡ D ]'}|d }| |d¡}|du s,| ¡ dkr=tj	| j
|gd}|||< | ¡  q| j jdu rM| j  |  ¡ ¡ n|  |  ¡ | j j ¡ | j jdks| ¡ D ]}| ¡  qbdS )zMain client loop.Fz*Iter through the following server list: {}r=   N)Útargetr   )r   Zis_endr   r7   r&   r   ÚgetZis_aliveÚ	threadingZThreadÚ#_GlancesClientBrowser__update_statsÚstartr>   ÚupdateÚ%_GlancesClientBrowser__display_serverÚvaluesÚjoin)r   Zthread_listÚvr=   Úthreadr   r   r   Z__serve_foreverØ   s$   €ð
ÿz$GlancesClientBrowser.__serve_foreverc                 C   s   z	|   ¡ W |  ¡  S |  ¡  w )z´Wrapper to the serve_forever function.

        This function will restore the terminal to a sane state
        before re-raising the exception and generating a traceback.
        )Ú$_GlancesClientBrowser__serve_foreverÚendr   r   r   r   rB   õ   s   z"GlancesClientBrowser.serve_foreverc                 C   sT   | j jt| j ¡ ƒkr| j | j jt| j ¡ ƒ ||¡ dS | j | j j||¡ dS )z9Set the (key, value) for the selected server in the list.N)r   r>   Úlenr   r   r   Z
set_server)r   r=   Úvaluer   r   r   r?      s   þz$GlancesClientBrowser.set_in_selectedc                 C   s   | j  ¡  dS )z"End of the client browser session.N)r   rP   r   r   r   r   rP   
  s   zGlancesClientBrowser.end)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r0   rG   rJ   rO   rB   r?   rP   r   r   r   r   r   #   s    
7C
r   )rV   r2   r4   rF   Zglances.compatr   r   r   Zglances.clientr   r   Zglances.loggerr   r   Zglances.password_listr	   r   Zglances.static_listr
   Zglances.autodiscoverr   Z&glances.outputs.glances_curses_browserr   Úobjectr   r   r   r   r   Ú<module>   s   