????

Your IP : 18.216.78.190


Current Path : /proc/self/root/lib/python3.6/site-packages/glances/plugins/__pycache__/
Upload File :
Current File : //proc/self/root/lib/python3.6/site-packages/glances/plugins/__pycache__/glances_ip.cpython-36.pyc

3

Eo�c�*�@s�dZddlZddlmZddlmZmZmZddlm	Z	ddl
mZmZddl
mZyddlZWn6ek
r�ZzdZe	jd	je��WYddZ[XnXd
ZdddgZGdd�de�ZGdd�de�ZGdd�de�ZdS)z
IP plugin.�N)�loads)�queue�urlopen�urlopen_auth)�logger)�Timer�getTimeSinceLastUpdate)�
GlancesPluginTz.Missing Python Lib ({}), IP plugin is disabledF�https://httpbin.org/ip�origin�"https://api.ipify.org/?format=json�ip�https://ipv4.jsonip.comcs^eZdZdZdZdgZd�fdd�	Zejej	dd���Z
dd	d
�Zdd�Ze
d
d��Z�ZS)�Pluginz,Glances IP Plugin.

    stats is a dict
    i,�FalseNcs�tt|�j||d�d|_d|_|jd|jd�|_|jd|jd�}|dgkrPdnd|_	d|_
|jd	d
gd�d|_|jdd
gd�d|_|jd
d
gd�d|_
|jdd
gd�|_|jd
kp�|jd
kp�|j
d
kp�|jd
k|_d
S)zInit the plugin.)�args�configT�Zpublic_refresh_interval)�default�public_ip_disabled�TrueF�
censys_urlNr�censys_username�censys_password�
censys_fields)�superr�__init__Z
display_curse�public_addressZget_conf_value� _default_public_refresh_interval�public_address_refresh_interval�_default_public_ip_disabledr�public_inforrrr�public_info_disabled)�selfrrr)�	__class__�� /usr/lib/python3.6/glances_ip.pyr2s 


zPlugin.__init__c,Cs�|j�}|jdkot�r�ytj�dtj}Wn4ttfk
rd}ztj	dj
|��iSd}~XnX|d|d<y<tj|d�tjdd}tj|d�tjdd	}Wn4ttfk
r�}ztj	d
j
|��iSd}~Xn$X||d<||d<|j|d�|d
<t
d�}y\|j�rh|jjd�|k�s8||jk�rht�j�|_|j�sht|j|j|j|j�j�|_Wn8ttfk
�r�}ztj	dj
|��WYdd}~XnX|j|d<|j|j�|d<n|jdk�r�||_|jS)zQUpdate IP stats using the input method.

        :return: the stats dict
        Zlocalrz+Cannot grab default gateway IP address ({})NrZgateway�ZaddrZnetmaskz#Cannot grab private IP address ({})�address�mask�	mask_cidrz	public-ipz&Cannot grab public IP information ({})r�public_info_humanZsnmp)Zget_init_valueZinput_method�import_error_tag�	netifacesZgatewaysZAF_INET�KeyError�AttributeErrorr�debug�formatZifaddresses�
ip_to_cidrrr�stats�getr�PublicIpAddressrr"�PublicIpInforrrr!�public_info_for_human)r#r3Z
default_gw�er(r)Ztime_since_updater%r%r&�updateOs@ 
"
z
Plugin.updatecCsBg}|js|j�str|Sd}|j|j|dd��d}|j|j|ddd��d|jkr|dj|jd�}|j|j|dd��d|jkr�d	j|jd�}|j|j|dd��ydj|jd
�}Wnttfk
r�YnhX|jd
�rd}|j|j|ddd��|j|j|dd��|jd�r>|j|jd
j|jd�dd��|S)z2Return the dict to display in the curse interface.z - T)ZoptionalzIP ZTITLEr(z{}r*z/{}rz Pub r+z {})r3Zis_disabledr,�appendZcurse_add_liner1�UnicodeEncodeErrorr.)r#rZ	max_width�ret�msgZmsg_pubr%r%r&�	msg_curse�s0

 zPlugin.msg_cursecCs�|sdSg}x�|jD]�}|jd�}t|�dkrT|d|krT|jdj||d��qt|�dkr|d|kr|d||dkr|jdj||d|d��qWdj|�S)z&Return the data to pack to the client.r�:r'rz{}��/)r�split�lenr:r1�join)r#r!Zfield_result�fZfieldr%r%r&r7�s
,$zPlugin.public_info_for_humancCs$|dkrdStdd�|jd�D��S)zUConvert IP address to CIDR.

        Example: '255.255.255.0' will return 24
        Nrcss |]}tt|��jd�VqdS)�1N)�bin�int�count)�.0�xr%r%r&�	<genexpr>�sz$Plugin.ip_to_cidr.<locals>.<genexpr>�.)�sumrB)r
r%r%r&r2�szPlugin.ip_to_cidr)NN)NN)�__name__�
__module__�__qualname__�__doc__rr rr	Z_check_decoratorZ_log_result_decoratorr9r>r7�staticmethodr2�
__classcell__r%r%)r$r&r)s>
)rc@s,eZdZdZddd�Zdd�Zdd	d
�ZdS)
r5z+Get public IP address from online services.r@cCs
||_dS)zInit the class.N)�timeout)r#rUr%r%r&r�szPublicIpAddress.__init__cCs�tj�}x8tD]0\}}}tj|j||||fd�}d|_|j�qWt|j	�}d}x*|j
�rz|dkrz|j�dkrR|j�}qRW|dkr�dSdj
tdd�|jd�D���S)	zGGet the first public IP address returned by one of the online services.)�targetrTNrz, cSsg|]}|j��qSr%)�strip)rJrKr%r%r&�
<listcomp>�sz'PublicIpAddress.get.<locals>.<listcomp>�,)r�Queue�urls�	threading�Thread�_get_ip_public�daemon�startrrU�finished�qsizer4rD�setrB)r#�q�u�j�k�t�timerr
r%r%r&r4�s
zPublicIpAddress.getFNcCs�yt||jd�j�jd�}Wn>tk
rZ}z"tjdj||��|jd�WYdd}~XnHXy&|sn|j|�n|jt	|�|�Wnt
k
r�|jd�YnXdS)z?Request the url service and put the result in the queue_target.)rUzutf-8z#IP plugin - Cannot open URL {} ({})N)rrU�read�decode�	Exceptionrr0r1�putr�
ValueError)r#�queue_target�urlZjson�key�responser8r%r%r&r^�szPublicIpAddress._get_ip_public)r@)FN)rOrPrQrRrr4r^r%r%r%r&r5�s
r5c@s*eZdZdZd
dd�Zdd�Zdd�Zd	S)r6z5Get public IP information from Censys online service.r@cCs"||_||_||_||_||_dS)zInit the class.N)r
rp�username�passwordrU)r#r
rprsrtrUr%r%r&rs
zPublicIpInfo.__init__cCs�tj�}tj|j||j|j|j|jfd�}d|_	|j
�t|j�}d}x*|j
�rp|dkrp|j�dkrH|j�}qHW|dkr~dS|S)zGReturn the public IP information returned by one of the online service.)rVrTNr)rrZr\r]�_get_ip_public_infor
rprsrtr_r`rrUrarbr4)r#rdrhri�infor%r%r&r4s"
zPublicIpInfo.getc	Cs�dj||�}yt|||�j�}Wn>tk
r^}z"tjdj||��|jd�WYdd}~Xn\Xy|jt|�d�WnBtt	fk
r�}z"tjdj||��|jd�WYdd}~XnXdS)z?Request the url service and put the result in the queue_target.z{}/v2/hosts/{}z#IP plugin - Cannot open URL {} ({})N�resultz0IP plugin - Cannot get result field from {} ({}))
r1rrjrlrr0rmrrnr.)	r#ror
rprsrtZrequest_urlrrr8r%r%r&rusz PublicIpInfo._get_ip_public_infoN)r@)rOrPrQrRrr4rur%r%r%r&r6s
r6)r
Tr)rTr
)rTr
)rRr\ZujsonrZglances.compatrrrZglances.loggerrZ
glances.timerrrZglances.plugins.glances_pluginr	r-�ImportErrorr8r,Zwarningr1r[r�objectr5r6r%r%r%r&�<module>
s&"+-