????

Your IP : 18.118.253.134


Current Path : /usr/local/lib/python3.9/site-packages/agent360/__pycache__/
Upload File :
Current File : //usr/local/lib/python3.9/site-packages/agent360/__pycache__/agent360.cpython-39.pyc

a

��g�o�@sddlmZddlZddlZejdkrxzddlmZWneyLeZYn0ddl	Z	ddl
Zddlm
Z
mZddlZn(ddlZddlZddlZddl
m
Z
mZejdkr�ddlZnddlZddlZddlZddlZzddlZWney�ddlZYn0ddlZddlZddlZddlZddlZddl Z ddl!Z!ddl"Z"ddl#Z#ddl$m%Z%z0ddl&m'Z'm(Z(dd	l)m*Z*m+Z+dd
l,m-Z-Wn@e�y�ddl'm'Z'ddl.m(Z(dd
l/m*Z*m+Z+m-Z-Yn0dZ0ej1�2ej1�3e4��Z5ej1�6dd�ej1�6dd�ej1�6dd�ej1�6ej1�2e5�d�ej1�6ej1�2e5�d�ej1�6ej1�2e5�d�ej1�3d�ej1�3d�ej1�3d�f	Z7ej8dk�r�ej1�6e5ddd�ej1�6e5ddd�ej1�6e5ddd�fZ7dd�Z9d,dd�Z:dd�Z;dd�Z<dd �Z=d!d"�Z>d#d$�Z?gfd%d&�Z@Gd'd(�d(�ZAd)d*�ZBeCd+k�reB�dS)-�)�print_functionN��)�
basestring)�Queue�Empty�r�)�OptionParser)�urlparse�	urlencode)�urlopen�Request)�	HTTPError)r)r)r
rrz1.3.1z/etczagent360.inizagent360-custom.ini�agent360-token.ini�ntz..�configc
CsPtdd�}|��}|jdd�}d�dtdd�|�d	|d
|j�dd�f�S)
z�
    Return string with info about agent360:
        - version
        - plugins enabled
        - absolute path to plugin directory
        - server id from configuration file
    T��dry_instance�enabled��state�
zVersion: %szPlugins enabled: %s�, zPlugins directory: %sz
Server: %s�agent�server)�Agent�_get_plugins_path�_get_plugins�join�__version__r�get)r�plugins_pathZplugins_enabled�r#�;/usr/local/lib/python3.9/site-packages/agent360/agent360.py�infoMs
�r%�httpsc	Cs�t�}|jdddd�|jddtddd	�|��\}}|d}td
d�}t|�dkr^|d}ntj�t	d
�}t|�dkr�|d}nd}|j
dur�d}n|j
}|jdkr�d�t��}	nd}	d|vr�|�
d�d}
|�
d�d}nnzt��d}Wnt�y
t��}Yn0t|d|j�dd�dt|||||	d���d�d�����}
t|
�dk�r�td|
�t|d��d||
f�ntd|
�dS)Nz-tz--tagszComma-separated list of tags)�helpz-az	--automonrz6Enable/disable automatic monitoring of hosted websites)�type�defaultr'Tr�r���,�_z://�data�hello_api_hostz/hello)�user�hostname�	unique_id�tags�domainszutf-8)r/�zGot server_id: %s�wz[DEFAULT]
user=%s
server=%s
z Could not retrieve server_id: %s)r
�
add_option�int�
parse_argsr�len�os�pathr�__FILEABSDIRNAME__r4Zautomon�_get_domains�split�uname�AttributeError�socket�getfqdnr
rr!r�encode�read�decode�print�open�write)�proto�parser�options�argsZuser_idrZtoken_filenamer3r4r5Z	server_idr2r#r#r$�hellobsV




���

�rOcCsfg}zJt�ddg�}|����D]*}d|vr.q |���d�}|�|d�q Wnty`Yn0|S)NZ	apachectlz-SZ	namevhost� r)�
subprocess�check_outputrG�
splitlines�stripr@�append�FileNotFoundError)r5�output�line�colsr#r#r$�_get_apache_domains�srZcCs�g}zjt�ddg�}|����D]J}d|vr.q |���d�}t|�dkr |d�dd��d	d�}|�|�q Wnt	y�Yn0|S)
NZnginxz-TZserver_namerPr+r*�;r,�")
rQrRrGrSrTr@r;�replacerUrV)r5rWrXrY�domainr#r#r$�_get_nginx_domains�sr_cCs�g}z<t�gd��}t�|�}|ddD]}|�|d�q*Wn�ty�z.t�gd��}|����D]}|�|�qjWndty�tt	t
�t���D]<}d|vr�q�|�d�r�q�|�d�r�q�|�d	�r�q�|�|�q�Yn0Yn0|S)
N)Zwhmapi1z--output=jsonprettyZget_domain_infor/r5r^)Zplesk�binr^z--list�.z.localdomainz
.localhostz.local)
rQrR�json�loadsrUrVrGrS�list�setrZr_�endswith)r5Zjson_str�responser^�strr#r#r$r?�s.



r?cCsttt���dS)N)rHr;r?r#r#r#r$�
count_domains�sricCs0t|t�r&tj�|�}tj�|�dS|jSdS)Nr)�
isinstancerr<r=�basename�splitext�__name__)�pluginrkr#r#r$�_plugin_name�s
rocCs�tdd�}|��}|tjvr*tj�d|�|sL|jdd�}tdd�|��|D�]�}td|�z,tjd	kr|t	j
�|�}nt�
|�\}}}Wn:ty�}z"td
|�WYd}~qPWYd}~n
d}~00z�z8tjd	kr�t	j
�|�}	|j�|	�nt�||||�}	WnTt�yX}z:td|�WYd}~Wtjd	krP|rP|��qPWYd}~n
d}~00Wtjd	k�r�|�r�|��ntjd	k�r�|�r�|��0z(|	���|j�}
ttj|
d
dd��WqPt�y�}ztd|�WYd}~qPd}~00qPdS)z�
    Test specified plugins and print their data output after single check.
    If plugins list is empty test all enabled plugins.
    TrrrrzCheck all enabled plugins: %srz%s:rzFind error:NzLoad error:r	��indent�	sort_keyszExecution error:)rr�sysr=�insertrrHr�version_info�	importlib�util�	find_spec�imp�find_module�	Exception�module_from_spec�loader�exec_module�load_module�close�Plugin�runrrb�dumps)�pluginsrr"�plugin_name�spec�fp�pathname�description�e�module�payloadr#r#r$�test_plugins�sN




"


�
�
r�c@s�eZdZe�Ze�Ze�Ze�ZdZd dd�Z	dd�Z
dd�Zdd	�Zd
d�Z
dd
�Zdd�Zdd�Zdd�Zdd�Zdd�Zd!dd�Zdd�Zdd�ZdS)"rFcCs:|��i|_|rdS|��|��|��|��dS)z0
        Initialize internal strictures
        N)�_config_init�
plugins_cache�
_logging_init�
_plugins_init�_data_worker_init�_dump_config)�selfrr#r#r$�__init__"szAgent.__init__cCs2tjdkr tj�|j�dd��S|j�dd�SdS)Nrrr�)r<�namer=�
expandvarsrr!�r�r#r#r$r3s
zAgent._get_plugins_pathcCs�ddtjdddtj�td�ddddddd	d
ddd
�}gd�}tjdkrRt�	|�}n
t
�	|�}|�t�||_
|D]>}|�|�|dkr�|j
�|dd�|dkrp|j
�|dd�qpdS)z1
        Initialize configuration object
        �<iX�dr��nor,zingest.monitoring360.iozapi.monitoring360.ioz/v2/server/pollz/var/log/agent360.log�a�
)�
max_data_span�max_data_age�
logging_level�threads�ttl�intervalr�rrQr1r�api_hostr0�api_path�log_file�
log_file_mode�max_cached_collections)r�	executionr/rr/r�r*r��?N)�logging�INFOr<r=rr>rsru�configparser�RawConfigParser�ConfigParserrF�	ini_filesr�_config_section_createre)r��defaults�sectionsr�sectionr#r#r$r�:s<�



zAgent._config_initcCs|j�|�s|j�|�dS)zc
        Create an addition section in the configuration object
        if it's not exists
        N)r�has_section�add_section)r�r�r#r#r$r�cszAgent._config_section_createc
Cs�|j�dd�}tjdkr0tj�|j�dd��}n|j�dd�}|j�dd�}|dvrVn |dkrdd}n|d	krrd
}nd
}|dkr�tj|d�n^ztj|||d
d�WnFt	y�}z.tj|d�t�
d|�t�
d�WYd}~n
d}~00t�
d|�dS)z,
        Initialize logging faculty
        rr�rr�r�)r7r��truncater7rUr��-)�levelz,%(asctime)-15s  %(levelname)s    %(message)s)�filename�filemoder��formatzIOError: %szDrop logging to stderrNzAgent logging_level %i)r�getintr<r�r=r�r!r��basicConfig�IOErrorr%)r�r�r�r�r�r#r#r$r�ks*
 zAgent._logging_initc

Cs�t�d�|��}t�tj�|d��}|tjvr>tj�d|�i|_	|D�]4}t
|�}|dkr`qH|�|�|j�
|d�rH|j�
|d�r�d|j	|<qHtjdkr�tj�|�}nt�|�\}}}z�z8tjdkr�tj�|�}	|j�|	�nt�||||�}	Wn0t�y$d}	t�d	tt��d��Yn0Wtjdk�r`|�r`|��ntjdk�r^|�r^|��0|	�rrd|j	|	<qHt�d
|�qHdS)z&
        Discover the plugins
        r��*.pyrr�rrQrNzimport_plugin_exception:%szimport_plugin:%s)r�r%r�globr<r=rrsrt�scheduleror�r�
getbooleanrurvrwrxryrzr|r}r~rr{�errorrh�exc_infor�)
r�r"�	filenamesr�r�r�r�r�r�r�r#r#r$r��sD





 
�
zAgent._plugins_initc
Cs$tjtj|ftjtjdd�}t�dt��|j	�|j
�dd�}t|�}|j
�|d�}||p^d}|�
�|jdur�|d	kr�t�d
t��|�t�|�|d8}|�
�qh|jdur�t�dt��|j	�t�|j	tj�|��\}}|jd	ks�|�r
t�dt��||j|�|�rt�|�}	nd}	|	S)
z0
        Execute /task/ in a subprocess
        T)�stdout�stderr�universal_newlinesz
%s:process:%ir�r�r�r*Nrz
%s:tick:%iz
%s:kill:%iz%s:%s:%s:%s)rQ�Popenrs�
executable�PIPEr��debug�	threading�
currentThread�pidrr�ro�poll�
returncode�time�sleepr�r<�kill�signal�SIGTERM�communicate�picklerc)
r��task�processr�r�r�Zticksr�r��retr#r#r$�_subprocess_execution�s6�


�zAgent._subprocess_executioncCs^|jrt�dt����q@t�dt��|j���z|j��}Wnt	yXY�q@Yn0t�dt��|�t
|�}z|j�|d�}Wnd}Yn0t
�
�}t|t�r�|�|�}npz<|j�||j�|g�i�|j|j|d�}|�|j�}Wn2t�y$t�d�dtt��d	�i}Yn0|j�|||||d
��q|j�t���|j��dS)zW
        Take queued execution requests, execute plugins and queue the results
        �%s:shutdownz%s:exec_queue:%iz
%s:task:%sr�r�)Zagent_cacheZplugin_exception�	exceptionr)�tsr�r�r�r�N)�shutdownr�r%r�r�r��execute�qsize�
get_nowaitrrorr!r�rjrr�r��updater�r�r{r�rhrsr��metrics�put�cemetery�hire�release)r�r�r�r�r�r�rnr#r#r$�
_execution�sF

�
�
zAgent._executioncCs t�dt���|j�dd�}|j�dd�}|j�dd�}|j�dd�}|j�dd�}|j�dd	�}|j�dd
�}|j�dd�}g}	g}
d}|r�d
}n|j�dd�}t��}|jr�t�dt����qt�	dt��|j
��t|
��|j
���r<z|
�
|j
���Wq�t�y8}
zt�d|
�WYd}
~
q�d}
~
00q�|
�r�tdd�|
D��}tdd�|
D��}t��}d}|||k�r�t�	d�d}d}n |||k�r�t�d�d}d}|�r�d}dd||fd�}t�	dtj|
ddd��|�r�|�st�d�d}�n�zڐz<tjt��d�}tjd k�r>tjj||d!d"�}ntj||d!d"�}|	�r�t�d#t|	��|	�r�|j d$d%|t!f|	d&|d'�|�"�}|�#�|j$d(k�r�|	d&=t�	d)|j$�nt%d*|j$���qdt�d+�|j d$d%|t!ft&�'t(t�|
�d,��)��|d'�|�"�}|�#�|j$d(k�r8t�	d)|j$�d}nt%d*|j$��Wn�t�y�}
ztt�d-|
�|d&k�r�t|	�|k�r�|	d&=t�d.|�t�d/�|	�
t&�'t(t�|
�d,��)���g}
WYd}
~
n
d}
~
00W|�*�n
|�*�0|�r�g}
|t��|}|d&kr�t�+|�q�dS)0zA
        Take and collect data, send and clean if needed
        z%sr/r�r�rr�r�rr1r�r�Tr�r�z%s:data_queue:%i:collection:%izData queue error: %sNcss|]}|dVqdS�r�Nr#��.0r�r#r#r$�	<genexpr>'�zAgent._data.<locals>.<genexpr>css|]}|dVqdSr�r#r�r#r#r$r�(r�Fz
Max data spanzMax data agezapplication/jsonzApiKey %s:%s)zContent-type�
Authorizationzcollection: %sr+rpz&Empty server or user, nowhere to send.)�cafiler�)�context�timeoutzSending cached collections: %i�PUTz
%s?version=%sr)�headers��zSuccessful response: %szUnsuccessful response: %szAll cached collections sentrzFailed to submit collection: %szCReach max_cached_collections (%s): oldest cached collection droppedz,Cache current collection to resend next time),r�r%r�r�rr!r�r�r�r�r/r�r;rUr�r{r��min�max�warningrbr��ssl�create_default_context�certifi�wherersru�http�client�HTTPSConnection�httplib�requestr �getresponserF�status�
ValueError�bz2�compressrhrEr�r�)r�r�r�Zmax_ageZmax_spanrr1r�r�Zcached_collectionsZ
collectionZinitial_dataZloop_tsr�Zfirst_tsZlast_ts�now�send�cleanr��ctx�
connectionrg�sleep_intervalr#r#r$�_datas��&


��
�
�
�
"zAgent._datacCs t�d�tj|jd���dS)z/
        Initialize data worker thread
        r���targetN)r�r%r��Threadr�startr�r#r#r$r�xs
zAgent._data_worker_initcCs<tjdkrt��}nt��}|j�|�t�d|���dS)z,
        Dumps configuration object
        rz
Config: %sN)	rsru�io�StringIOrrJr�r%�getvalue)r��bufr#r#r$r�s


zAgent._dump_configrcCs�|��}g}t�tj�|d��D]b}t|�}|dkr6q |�|�|dkrb|j�|d�r�|�	|�q |dkr |j�|d�s |�	|�q |S)z0
        Return list with plugins names
        r�r�r�disabled)
rr�r<r=rror�rr�rU)r�rr"r�r�r�r#r#r$r�s
zAgent._get_pluginscCsRt�d|j���z|j��}Wnty6YqNYn0t�d|�|��qdS)zb
        Join with dead workers
        Workaround for https://bugs.python.org/issue37788
        zcemetery:%iz
joining:%sN)r�r�r�r�r�rr)r��threadr#r#r$�_rip�s
z
Agent._ripcs�t�d�|j�dd�}t�|j�dd��|_�z�|��t	�	��t�
dt���|j�
�r�|j��}|d}t�
d|�|�d	�}|r�t��|j�|d�|j|<t|tj�r�|j|d	<|j�|�qR�fd
d�|j��D�}|D]�}t�
d|�|j|=|j�|�|j�d
��rrz&tj|jd�}|��t�
d|�Wn2t�yn}zt�d|�WYd}~n
d}~00q�t�d�|j��dd|j�dd�id��q�dt	�	��}|dk�r�t	� |�q2t�d�t	� d�q2Wn�t!�y�t�t"�#�d�t�d�|��d}	|	�r�t�$�}
t�d|
�dd�|
D�}	|	�sXt�d�t"�%d�d|_&t�dt'|	��|	D]}t�d ||�|�(|��qr�qYn4t�y�}zt�)d!|�WYd}~n
d}~00dS)"z.
        Start all the worker threads
        zAgent main looprr�r�r�z
%i threadsr�z
metrics:%sr�csg|]\}}|�kr|�qSr#r#)r��what�when�r
r#r$�
<listcomp>�s�zAgent.run.<locals>.<listcomp>z
scheduling:%sFrznew_execution_worker_thread:%szCan not start new thread: %sNZthreads_cappedZagent_internalZthreads_capping)r�r�r�r�rz'not enough time to start worker threadsg�������?z
Shutting downTzRemaining threads: %scSs$g|]}|��st|tj�s|�qSr#)�isDaemonrjr��_MainThread)r�rr#r#r$r�s�zBye!zWaiting for %i threads to exitzJoining with %s/%fzWorker error: %s)*r�r%r�getfloatr��	Semaphorer�r�rr�r��activeCountr�r�r�r!r9r�rj�types�
ModuleType�__file__r/r��itemsr��acquirerr�rr{r�r��KeyboardInterruptrsr��	enumerate�exitr�r;rr�)r�r�r�r�rnr�rr�r�wait_forZall_threadsr#rr$r��s�
�


�

�$
��



�

z	Agent.runN)F)r)rm�
__module__�__qualname__rr�r�r/r�r�r�rr�r�r�r�r�r�rr�r�rrr�r#r#r#r$rs&
)!,/r
rcCslttj�dk�r^tjd�d�r8tjddd�tjd<tjddkr^td�d��t��n�tjddkr�tt��t��n�tjddkr�tt�t��n�tjdd	kr�tjd=t�t	��n�tjdd
kr�tjd=t�t
��nttjddk�rtjd=t�t	dd
��nJtjddk�r<t�ttjdd���n tdtjdtjd�t�d�n
t
���dS)Nr*z--r+r'r)z!Run without options to run agent.z,Acceptable options (leading -- is optional):z4    help, info, version, hello, insecure-hello, testr%�versionrOz
count-domainszinsecure-hellor)rK�testzInvalid option:)�file)r;rs�argv�
startswithrHrr,r%r rOrir�r�rr�r#r#r#r$�mains4



r5�__main__)r&)D�
__future__rrrsruZ
past.builtinsr�ImportErrorrhr��http.clientr�queuerrrr�rrZimportlib.utilrvryr�r�r�rbZ
simplejsonr�r<r�r�rCrQr�r�r%�optparser
�urllib.parserr�urllib.requestr
r�urllib.errorr�urllib�urllib2r r=�dirname�abspathr'r>rr�r�r%rOrZr_r?riror�rr5rmr#r#r#r$�<module>s�







��
8!0i$