Your IP : 216.73.216.86


Current Path : /lib/fm-agent/library/__pycache__/
Upload File :
Current File : //lib/fm-agent/library/__pycache__/aggregator.cpython-36.pyc

3

q��i?�&@sDddlmZyddlZWnek
r6ddljZYnXddlZddlZddlZddlZddl	Z	ddl
Z
yddlZWn ddlj
ZddljZYnXyddlZWn:ek
r�yddlZWnek
r�dZYnXYnXyddlZWndZYnXddlmZmZmZGdd�de�ZGdd�de�ZGdd	�d	e�ZdS)
�)�datetimeN)�get_fqdn�get_platform_uuids�get_mac_addressesc@seZdZdS)�UnauthorizedResponseN)�__name__�
__module__�__qualname__�r
r
�'/usr/lib/fm-agent/library/aggregator.pyr+src@seZdZdS)�ReinstallResponseN)rrr	r
r
r
rr/src@s�eZdZdZddd�Zdd�Zdd�Zid	fd
d�Zdd
�Zdidiifdd�Z	ddd�Z
ddd�Zddd�Zdd�Z
dd�ZdS)�Clientzy
    This is a client used for communicating with an aggregator, either *the*
    Aggregator or an Onsight instance.
    NcCs�tj|jj�|_||_||_||_||_d|_	d|_
d}|dk	rz|jdd�}t|�td�krv|j
�}|dkrrd}nd}|}|r�|r�|j|�|_
dS)	N�T�enabled��0�falseF)rr)�logging�	getLogger�	__class__r�log�customer_key�
server_key�agg_url�version�call_timeout�proxy�get�type�lower�parse_proxy_info)�selfrrrr�proxy_configZproxies_enabledZpevr
r
r�__init__9s$zClient.__init__cCszd}|s|Sy.x(dD] }|j|�}|s(qtj|�}|SWdStk
rttj�d}t|�}|jjdj|��dSXdS)N�https�http�zparse_proxy_info error: {})r$r%)	r�urlparse�	Exception�sys�exc_info�strr�info�format)r!r"Z	proxy_val�schemeZ
parsed_uri�err�errorr
r
rr ]s 


zClient.parse_proxy_infocCs�d}|dkr�dtjkr,tj||tj�d�}q�|jjtj�trvt	td�rv|jjdt	td��tj|||j
tj�d�}q�tjd
kr�tj|||j
d�}q�tj||�}n4tjdkr�dtjkr�tj|||j
d�}ntj||�}|S)N�� zhp-ux)�context�_create_unverified_contextz0ssl has _create_unverified_context attribute: %s)�timeoutr3��r)r5)r1r2)r6r7r)r6r7r)r)�platform�httplib�HTTPSConnection�sslr4r�debug�version_info�hasattrr�HTTPConnection)r!�url�port�
connectionr
r
r�create_connectionos0

zClient.create_connection�POSTcCs�dd�|jjd�D�}tj|�d}�x�|D�]�}yTtj|�}|ddksV|ddkrdtjd|�}|dd.ks||ddkr�td
��Wntk
r�td��YnX|d}d|kr�|jd�\}}	t|	p�d
�}	n|}d
}	d|kr�|d|jd��}|j	j
d|||	�d}
d|j|f}|dk�rb|j|d<d|d<tj
tj�j��|d<|j	jd|�tj|�}
ddd�}|j�r~|j|d<d}
|j�r.tj|jj|jj|jtj�d�}
|j	j
dj|
j|
j��|jj}|jj }|�r |�r dj||�}t!j"t#|d��j$d�}dj|�}|
j%||	d|id �n|
j%||	�n|j&||	�}
�y&|j	jd!|||
|f�|
j'|||
|�|
j(�}t|j)�d"k�r�tj*|j+�j$d��}|j,d#i�j,d$��r�|d#d$}t-|��t|j.��|dk�r�t|j)�d%k�r�t|j.��n$|dk�rt|j)�d&k�rt|j.��tj*|j+�j$d��}t/|�t0k�r`|j,d'�d(k�r`|j,d)�d*k�r`t1d+|�d,���|St-k
�rz�Yq,t1k
�r��Yq,tk
�r�t2j3�d}t4|�}|j	j5d-|�w,Yq,Xq,Wt|��dS)/NcSsg|]}|j�r|j��qSr
)�strip)�.0r@r
r
r�
<listcomp>�szClient.call.<locals>.<listcomp>�,rrr&zhttp://r%r$zInvalid aggregator URLzInvalid aggregator URL format.�:i�z/vzaggregator call %r to %r:%dz/v%s/%srDZ
agent_versionZv3Zmetadata_versionZ
agent_timezData: %rzapplication/json)zContent-type�Accept�
Authorization)r5r3zConnection via proxy {}:{}z{}:{}zutf-8zBasic {}zProxy-Authorization)�headersz%s - %s - %s - %si��commands�pause�����found_serverF�reasonzFortiClient serial mismatchzSync z+ failed due to FortiClient serial conflict.zUnable to connect: %s)r%r$)6r�split�random�shuffler'r(�
ValueError�int�indexrr,r�time�mktimer�now�	timetupler<�json�dumpsrrr9r:�hostnamerArr;r4r-�host�username�password�base64�	b64encode�bytes�decode�
set_tunnelrC�request�getresponse�status�loads�readrrrRr�dictrr)r*r+r0)r!�action�data�methodZagg_urlsr0rZ	url_partsr@rA�params�urirLrB�unameZpswd�credsZ	auth_hashZ
auth_value�respZ
pause_timeZ	resp_datar/r
r
r�call�s�









zClient.callcCs�tjtjtj���}|jd�x�|jjd�D]v}|jd�jd�}d|krV|jd�\}}n|}|jjd�rld}nd}|jj	d	j
||��|j|t|�f�|j
�d
SWWdQRXdS)Ng@rHzhttps://zhttp://rIr$i��Pz*get_local_ip: creating connection to {} {}r)�socket�AF_INET�
SOCK_DGRAM�
settimeoutrrSZremoveprefix�
startswithrr,r-�connectrW�getsockname)r!�clientrr`rAr
r
r�get_local_ips
zClient.get_local_ipFcCsxt|||||||||||	|
|||
|d�}|r>t�}|r>||d<|r`x|j�D]}||||<qLW|rl||d<|jd|�S)N)�results�
outage_events�metadata�countermeasures_metadata�facts�discovered_containers�deleted_containers�force_send_schedules�command_results�register�register_custom_metrics�	incidents�config�ips�auto_topo_scans�dem_enabled�fm_server_identifiers�fortisase_attributes�sync)rmr�keysrv)r!r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r��dem_service_resultsr��payload�platform_uuidsZdsr
r
rr�s6zClient.syncc
CsTd}|jjd�y|jdd|id�Wn|jjd�YnX|jjd�d}|S)	NFz beginning uninstall notification�	uninstall�remove_instance)rozerror connectingznotified successfullyT)rr,rvr0)r!r��successr
r
r�notify_of_uninstallHszClient.notify_of_uninstallc
Cs|jjd�t�}|j||t�d�}t�}|r6||d<|j|�|j|j||��|jjd�|jjtj	|dd��d}d}d}	d}
d}yL|j
d|�}|jj|�|d	}|d
}d|kr�d|dkr�|dd}Wn6tk
�r
t
j�d}|jjd
|�|}
YnXd}	|jjd|�|jd	��p2|jd
��s>d}
d}	d|k�r�|jd��r�d}	|ddk�rld}
n~|ddk�r�d}
nj|ddk�r�d}
nV|ddk�r�d}
nB|ddk�r�d}
n.|djd��r�d|djdd�}
n|d}
|jjd |	|||
�|	|||
|fS)!NzBeginning handshake)rr_r�Z
mac_addressesr�zHandshaking with aggregator:r&)�indentF�	handshakerrQrM�	log_levelzError connecting: %sTz/Connected successfully! Received server key: %rzIError performing handshake with the aggregator, please check connectivityr0Zinvalid_credentialszIncorrect username/passwordZinvalid_customer_keyzUnknown/invalid customer keyZinvalid_server_groupzUnknown server groupZinactive_customerzCustomer inactiveZno_user_found_for_customerz%No active user found for the customerZinvalid_template_idzUnknown template id %szinvalid_template_id:rz8Success: %s, server key: %s, found server: %s, error: %s)rr,rrrr�update�parse_attributesr]r^rvr<r(r)r*r0rr|�replace)
r!r��
properties�
attributesr_ror�rrQr�r0r�r/r
r
rr�Vsr



zClient.handshakecCs||r
|pgd�}|jd|�S)a
        Request to the aggregator that it starts a maintenance for our server.
        It requires the duration of the maintenance in minutes, and
        accepts an optional list of tags to mark metrics that are going to be affected
        by it.
        )�duration�metric_tags�maintenance)rv)r!r�r�r�r
r
rr��szClient.maintenancecCs
|jd�S)z�
        Request to the aggregator that it ends all active maintenances for our server.
        POST call with no parameters.
        �end_maintenance)rv)r!r
r
rr��szClient.end_maintenancecCs d|kr|rd|ipiSiSdS)z�
        Receive a dictionary containing the attributes stored in the manifest file.
        Parse them accordingly and store them in an attribute dictionary. Attributes get parsed
        only if the customer key is present in the properties.
        rr�Nr
)r!r�r�r
r
rr��szClient.parse_attributes)NNN)F)N)N)rrr	�__doc__r#r rCrvr�r�r�r�r�r�r�r
r
r
rr
3s 
#)h""

I

r
)rr9�ImportError�http.clientrrcrrTrxr)rYr'�urllib.requestrh�
urlrequest�urllib.parse�parser]�
simplejsonr;�	inspectorrrrr(rr�objectr
r
r
r
r�<module>s<