????

Your IP : 3.15.190.254


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

3

Eo�c%#�@s�dZddlZddlZddlZddlZddlmZddlmZm	Z	m
Z
mZddlm
Z
ddlmZddlmZddlmZGd	d
�d
e�ZGdd�de�ZdS)
zManage the Glances client.�N)�__version__)�Fault�
ProtocolError�ServerProxy�	Transport)�logger)�GlancesStatsClient)�GlancesCursesClient)�Counterc@seZdZdZdd�ZdS)�GlancesClientTransportzFThis class overwrite the default XML-RPC transport and manage timeout.cCs
||_dS)N)�timeout)�selfr�r�/usr/lib/python3.6/client.py�set_timeoutsz"GlancesClientTransport.set_timeoutN)�__name__�
__module__�__qualname__�__doc__rrrrrrsrc@s�eZdZdZddd�Zedd��Zd d
d�Zedd
��Zej	dd
��Zdd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�ZdS)!�
GlancesClientz.This class creates and manages the TCP client.N�FcCs�||_||_|j|_|j|_d|_||_|jdkrNdj	|j
|j|j|j�|_
ndj	|j|j�|_
tjdj	|j
��t�}|j|�yt|j
|d�|_Wn6tk
r�}z|jdj	|j
|��WYdd}~XnXdS)N�glances�zhttp://{}:{}@{}:{}zhttp://{}:{}zTry to connect to {})�	transportz$Client couldn't create socket {}: {})�args�config�quiet�_quiet�time�refresh_time�_client_mode�return_to_browserZpassword�formatZusername�clientZport�urir�debugrrr�	Exception�log_and_exit)r
rrrr!r�errr�__init__%s 

zGlancesClient.__init__cCs|jS)N)r)r
rrrrCszGlancesClient.quietrcCs*|jstj|�tjd�n
tj|�dS)z
Log and exit.�N)r!r�critical�sys�exit�error)r
�msgrrrr'Gs
zGlancesClient.log_and_exitcCs|jS)zGet the client mode.)r )r
rrr�client_modeOszGlancesClient.client_modecCs
||_dS)zpSet the client mode.

        - 'glances' = Glances server (default)
        - 'snmp' = SNMP (fallback)
        N)r )r
�moderrrr0TscCsXd}y|jj�}Wn�tjk
rx}zHd|_tjdj|j|j��dj|j	�}|j
s^t|�n
tj|�WYdd}~Xn^t
k
r�}zBdj|j	�}|jdkr�|d7}n|dj|j|j�7}|j|�d	Sd}~XnX|jd
k�rTtjd�d|jd�dk�r>t|j|jd
�|_|jjtj|jj���tjdjt|��n|jdjt|��d	SdS)zLogin to a Glances serverN�snmpz+Connection to Glances server failed ({} {})z9No Glances server found on {}. Trying fallback to SNMP...zConnection to server {} failedi�z (Bad username/password)z ({} {})Fr�.r)rrz'Client version: {} / Server version: {}zIClient and server not compatible: Client version: {} / Server version: {}T)r#Zinit�socketr.r0rr"�errno�strerrorr$r!�print�inforZerrcode�errmsgr'r�splitrrr�statsZset_plugins�ujson�loadsZ
getAllPluginsr%)r
Zclient_version�errZ
fall_back_msgr/rrr�_login_glances]s6




zGlancesClient._login_glancescCsDtjd�ddlm}||j|jd�|_|jj�s@|jd�dSdS)zLogin to a SNMP serverzTrying to grab stats by SNMP...r)�GlancesStatsClientSNMP)rrz Connection to SNMP server failedFT)	rr8Zglances.stats_client_snmpr@rrr;Z
check_snmpr')r
r@rrr�_login_snmp�s


zGlancesClient._login_snmpcCsr|jjrd|_n|j�sdS|jdkr2|j�s2dStjd�|jj|j	�|j
r\tjd�nt|j	|jd�|_
dS)zLogon to the server.r2Fz.Load limits from the client configuration filez+Quiet mode is ON: Nothing will be displayed)rrT)rZ
snmp_forcer0r?rArr%r;Zload_limitsrrr8r	�screen)r
rrr�login�s

zGlancesClient.logincCsL|jdkr|j�S|jdkr$|j�S|j�tjdj|j��tjd�dS)z&Update stats from Glances/SNMP server.rr2zUnknown server mode: {}r*N)	r0�update_glances�update_snmp�endrr+r"r,r-)r
rrr�update�s

zGlancesClient.updatecCsRytj|jj��}Wn(tjk
r*dStk
r<dSX|jj|�dSdS)z�Get stats from Glances server.

        Return the client/server connection status:
        - Connected: Connection OK
        - Disconnected: Connection NOK
        �DisconnectedZ	ConnectedN)	r<r=r#ZgetAllr4r.rr;rG)r
Zserver_statsrrrrD�szGlancesClient.update_glancescCs,y|jj�Wntk
r"dSXdSdS)z�Get stats from SNMP server.

        Return the client/server connection status:
        - SNMP: Connection with SNMP server OK
        - Disconnected: Connection NOK
        rHZSNMPN)r;rGr&)r
rrrrE�s
zGlancesClient.update_snmpcCs|j�s tjd�|j�|jSd}y�x�dr�|r�t�}|j�}tjdj|j	���t�}|j
j|j
�tjdj|j	���|j|j	�}|dkr�|nd}|j
s�|jj|j
|||jd�}q(tj|�q(WWn6tk
�r}ztj|�|j�WYdd}~XnX|jS)	zMain client loop.z4The server version is not compatible with the clientFTz"Stats updated duration: {} secondsz#Stats exported duration: {} secondsr)Zduration�	cs_statusr!N)rCrr+rFr0r
rGr%r"�getr;ZexportrrrBr!rZsleepr&)r
Zexit_keyZcounterrIZcounter_exportZadapted_refreshr(rrr�
serve_forever�s4

zGlancesClient.serve_forevercCs|js|jj�dS)zEnd of the client session.N)rrBrF)r
rrrrFszGlancesClient.end)NNrF)r)rrrrr)�propertyrr'r0�setterr?rArCrGrDrErKrFrrrrr!s

	*+r)rr<r4r,rrrZglances.compatrrrrZglances.loggerrZglances.stats_clientrZglances.outputs.glances_cursesr	Z
glances.timerr
r�objectrrrrr�<module>
s