????

Your IP : 3.144.40.238


Current Path : /lib/python3.6/site-packages/glances/__pycache__/
Upload File :
Current File : //lib/python3.6/site-packages/glances/__pycache__/client_browser.cpython-36.opt-1.pyc

3

Eo�cl'�@s�dZddlZddlZddlZddlmZmZmZddlm	Z	m
Z
ddlmZm
Z
ddlmZddlmZddlmZdd	lmZGd
d�de�ZdS)z;Manage the Glances client browser (list of Glances server).�N)�Fault�
ProtocolError�ServerProxy)�
GlancesClient�GlancesClientTransport)�logger�LOG_FILENAME)�GlancesPasswordList)�GlancesStaticServer)�GlancesAutoDiscoverServer)�GlancesCursesBrowserc@sbeZdZdZddd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dS)�GlancesClientBrowserzEThis class creates and manages the TCP client browser (servers list).NcCsJ||_||_d|_d|_|j�|jjs2t�|_nd|_t|jd�|_	dS)N)�args)
r�config�
static_server�password�loadZdisable_autodiscoverr�autodiscover_serverr�screen)�selfrr�r�$/usr/lib/python3.6/client_browser.py�__init__s
zGlancesClientBrowser.__init__cCs t|jd�|_t|jd�|_dS)z:Load server and password list from the configuration file.)rN)r
rr�GlancesPasswordr)rrrrr0szGlancesClientBrowser.loadcCs8g}|jjr4|jj�}|jdk	r4|jj�|jj�}|S)zmReturn the current server list (list of dict).

        Merge of static + autodiscover servers list.
        N)rZbrowserr�get_servers_listr)r�retrrrr8s

z%GlancesClientBrowser.get_servers_listcCsx|ddkr`|ddkr@|jj|d�}|dk	r@|jj|�|d<dj|d|d|d	|d
�Sdj|d	|d
�SdS)z)Return the URI for the given server dict.r��status�	PROTECTED�nameNzhttp://{}:{}@{}:{}�username�ip�portzhttp://{}:{})r�get_password�sha256_hash�format)r�server�clear_passwordrrrZ	__get_uriFs zGlancesClientBrowser.__get_uric Cs�|j|�}t�}|jd�yt||d�}Wn6tk
r`}ztjdj||��WYdd}~X�n`XyTdtj	|j
��d}dj|�|d<tj	|j��d	|d
<tj	|j��d|d<Wn�t
jttfk
r�}z tjdj||��d
|d<WYdd}~Xn�tk
�rZ}zD|jdk�r*d|d<d|d<nd
|d<tjdj||j|j��WYdd}~XnfXd|d<y$tj	|j��d}dj|�|d<Wn6tk
�r�}ztjdj||��WYdd}~XnX|S)z?Update stats for the given server (picked from the server list)�)Z	transportz,Client browser couldn't create socket {}: {}N�dZidlez{:.1f}�cpu_percentZpercentZmem_percentZhr_namez&Error while grabbing stats form {}: {}�OFFLINEri�rrz!Cannot grab stats from {} ({} {})�ONLINEZmin5z{:.2f}�	load_min5)�_GlancesClientBrowser__get_urirZset_timeoutr�	ExceptionrZwarningr%�ujson�loadsZgetCpuZgetMemZ	getSystem�socket�errorr�KeyError�debugrZerrcode�errmsgZgetLoad)rr&Zuri�t�s�er*r-rrrZ__update_statsSs8

&
*$z#GlancesClientBrowser.__update_statscCs�tjdj|��|jjdj|d|d�dd�|ddkr�|jj|d�}|dksl|j�|jjd	d
kr�|jjdj|d�dd
�}|dk	r�|j	d|jj
|��tjdj|d��|j}|d|_
|d|_|d|_|d|_t|j|dd�}|j��s$|jjdj|dt��|j	d	d�n\|j�}ytjdj|d��Wntk
�rZYn&X|dk�rt|j	d	d�n|j	d	d�d|j_dS)z$Connect and display the given serverzSelected server {}zConnect to {}:{}rr"�)ZdurationrNrrzPassword needed for {}: T)Zis_inputz'Connect Glances client to the {} server�keyr!r )rrZreturn_to_browserz7Sorry, cannot connect to '{}'
See '{}' for more detailsr+z,Disconnect Glances client from the {} serverZsnmpZSNMPr,)rr5r%rZ
display_popuprr#r�
active_server�set_in_selectedr$�infor�clientr"r rrZloginr�
serve_forever�
IndexError)rr&r'Zargs_serverr?Zconnection_typerrrZ__display_server�s:  





z%GlancesClientBrowser.__display_servercCs�i}x�|jjs�tjdj|j���xZ|j�D]N}|d}|j|d�}|dksX|j�dkr,tj	|j
|gd�}|||<|j�q,W|jjdkr�|jj
|j��q|j|j�|jj�qWx|j�D]}|j�q�WdS)zMain client loop.z*Iter through the following server list: {}r;NF)�targetr)rZis_endrr5r%r�getZis_alive�	threadingZThread�#_GlancesClientBrowser__update_stats�startr<�update�%_GlancesClientBrowser__display_server�values�join)rZthread_list�vr;ZthreadrrrZ__serve_forever�s
z$GlancesClientBrowser.__serve_foreverc
Csz|j�S|j�XdS)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.
        N)�$_GlancesClientBrowser__serve_forever�end)rrrrr@�sz"GlancesClientBrowser.serve_forevercCsR|jjt|jj��kr:|jj|jjt|jj��||�n|jj|jj||�dS)z9Set the (key, value) for the selected server in the list.N)rr<�lenrrrZ
set_server)rr;�valuerrrr=�sz$GlancesClientBrowser.set_in_selectedcCs|jj�dS)z"End of the client browser session.N)rrM)rrrrrM�szGlancesClientBrowser.end)NN)�__name__�
__module__�__qualname__�__doc__rrrr.rErHrLr@r=rMrrrrr
s

2@
r
)rSr0r2rDZglances.compatrrrZglances.clientrrZglances.loggerrrZglances.password_listr	rZglances.static_listr
Zglances.autodiscoverrZ&glances.outputs.glances_curses_browserr�objectr
rrrr�<module>
s