Sindbad~EG File Manager

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

3

�uGh�o�"@sddlmZddlZddlZejd,krzyddlmZWnek
rNeZYnXddl	Z	ddl
Zddlm
Z
mZddlZn(ddlZddlZddlZddl
m
Z
mZejd-kr�ddlZnddlZddlZddlZddlZyddlZWnek
�rddlZYnXddlZddlZddlZddlZddlZddl Z ddl!Z!ddl"Z"ddl#Z#ddl$m%Z%y0ddl&m'Z'm(Z(dd	l)m*Z*m+Z+dd
l,m-Z-WnBek
�r�ddl'm'Z'ddl.m(Z(dd
l/m*Z*m+Z+m-Z-YnXdZ0ej1j2ej1j3e4��Z5ej1j6dd�ej1j6dd�ej1j6dd�ej1j6ej1j2e5�d�ej1j6ej1j2e5�d�ej1j6ej1j2e5�d�ej1j3d�ej1j3d�ej1j3d�f	Z7ej8dk�r�ej1j6e5ddd�ej1j6e5ddd�ej1j6e5ddd�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�)�OptionParser)�urlparse�	urlencode)�urlopen�Request)�	HTTPError)r	)r
)rrr
z1.3.1z/etczagent360.inizagent360-custom.inizagent360-token.ini�ntz..�configcCsPtdd�}|j�}|jdd�}djdtddj|�d	|d
|jjdd�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: %sz, zPlugins directory: %sz
Server: %s�agent�server)�Agent�_get_plugins_path�_get_plugins�join�__version__r�get)r�plugins_pathZplugins_enabled�r�:/root/tmp/pip-build-gzoz1_uw/agent360/agent360/agent360.py�infoMs
r�httpscCs�t�}|jdddd�|jddtddd	�|j�\}}|d}td
d�}t|�dkr^|d}ntjjt	d
�}t|�dkr�|d}nd}|j
dkr�d}n|j
}|jdkr�djt��}	nd}	d|kr�|j
d�d}
|j
d�d}npytj�d}Wntk
�rtj�}YnXt|d|jjdd�dt|||||	d��jd�d�j�j�}
t|
�dk�r�td|
�t|d�jd||
f�ntd|
�dS)Nz-tz--tagszComma-separated list of tags)�helpz-az	--automonrz6Enable/disable automatic monitoring of hosted websites)�type�defaultr!T)r�zagent360-token.ini���,�_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__r.Zautomon�_get_domains�split�uname�AttributeError�socket�getfqdnrrrr
�encode�read�decode�print�open�write)�proto�parser�options�argsZuser_idrZtoken_filenamer-r.r/Z	server_idr,rrr�hellobsN






rIcCslg}yNtjddg�}x:|j�j�D]*}d|kr0q"|j�jd�}|j|d�q"WWntk
rfYnX|S)NZ	apachectlz-SZ	namevhost� r)�
subprocess�check_outputrA�
splitlines�stripr:�append�FileNotFoundError)r/�output�line�colsrrr�_get_apache_domains�srTcCs�g}yntjddg�}xZ|j�j�D]J}d|kr0q"|j�jd�}t|�dkr"|djdd�jd	d�}|j|�q"WWnt	k
r�YnX|S)
NZnginxz-TZserver_namerJr%r$�;r&�")
rKrLrArMrNr:r5�replacerOrP)r/rQrRrS�domainrrr�_get_nginx_domains�srYcCsg}yBtjdddg�}tj|�}x"|ddD]}|j|d�q.WWn�tk
r�y6tjdddd	g�}x|j�j�D]}|j|�qxWWnjtk
r�xTtt	t
�t���D]<}d
|kr�q�|jd�r�q�|jd�r�q�|jd
�r�q�|j|�q�WYnXYnX|S)NZwhmapi1z--output=jsonprettyZget_domain_infor)r/rXZplesk�binz--list�.z.localdomainz
.localhostz.local)
rKrL�json�loadsrOrPrArM�list�setrTrY�endswith)r/Zjson_str�responserX�strrrrr9�s.



r9cCsttt���dS)N)rBr5r9rrrr�
count_domains�srccCs0t|t�r&tjj|�}tjj|�dS|jSdS)Nr)�
isinstancerr6r7�basename�splitext�__name__)�pluginrerrr�_plugin_name�s
ric3Cs�tdd�}|j�}|tjkr*tjjd|�|sL|jdd�}tddj|���xZ|D�]P}td|�y,tjdkr�t	j
j|�}ntj
|�\}}}Wn.tk
r�}ztd|�wTWYdd}~XnXzny8tjdkr�t	j
j|�}	|jj|	�ntj||||�}	Wn0tk
�r,}ztd
|�wTWYdd}~XnXWdtjdk�rL|�rL|j�Xy(|	j�j|j�}
ttj|
d
dd��WqTtk
�r�}ztd|�WYdd}~XqTXqTWdS)z�
    Test specified plugins and print their data output after single check.
    If plugins list is empty test all enabled plugins.
    T)rrr)rzCheck all enabled plugins: %sz, z%s:rrzFind error:NzLoad error:)�indent�	sort_keyszExecution error:)rr)rr)rr)rr�sysr7�insertrrBr�version_info�	importlib�util�	find_spec�imp�find_module�	Exception�module_from_spec�loader�exec_module�load_module�close�Plugin�runrr\�dumps)�pluginsrr�plugin_name�spec�fp�pathname�description�e�module�payloadrrr�test_plugins�s@






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:|j�i|_|rdS|j�|j�|j�|j�dS)z0
        Initialize internal strictures
        N)�_config_init�
plugins_cache�
_logging_init�
_plugins_init�_data_worker_init�_dump_config)�selfrrrr�__init__"szAgent.__init__cCs2tjdkr tjj|jjdd��S|jjdd�SdS)Nrrr})r6�namer7�
expandvarsrr)r�rrrr3s
zAgent._get_plugins_pathcCs�ddtjdddtjjtd�ddddddd	d
ddd�}d
ddg}tjdkrTtj	|�}n
t
j	|�}|jt�||_
xF|D]>}|j|�|dkr�|j
j|dd�|d
krt|j
j|dd�qtWdS)z1
        Initialize configuration object
        �<�
�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}rrKr+r�api_hostr*�api_path�log_file�
log_file_mode�max_cached_collectionsr�	executionr)rr�r$g�?NiX)r)�logging�INFOr6r7rr8rlrn�configparser�RawConfigParser�ConfigParserr@�	ini_filesr�_config_section_creater_)r��defaults�sectionsr�sectionrrrr�:s>




zAgent._config_initcCs|jj|�s|jj|�dS)zc
        Create an addition section in the configuration object
        if it's not exists
        N)r�has_section�add_section)r�r�rrrr�cszAgent._config_section_createcCs�|jjdd�}tjdkr0tjj|jjdd��}n|jjdd�}|jjdd�}|dkrVn |dkrdd}n|d	krrd}nd}|d
kr�tj|d�n\ytj|||dd
�WnDt	k
r�}z(tj|d�tj
d|�tj
d�WYdd}~XnXtj
d|�dS)z,
        Initialize logging faculty
        rr�rr�r�r1r��truncaterO�-)�levelz,%(asctime)-15s  %(levelname)s    %(message)s)�filename�filemoder��formatzIOError: %szDrop logging to stderrNzAgent logging_level %i)r1r�)r�getintr6r�r7r�rr��basicConfig�IOErrorr)r�r�r�r�r�rrrr�ks*
zAgent._logging_initc
Csrtjd�|j�}tjtjj|d��}|tjkr>tjjd|�i|_	�x&|D�]}t
|�}|dkrdqL|j|�|jj
|d�rL|jj
|d�r�d|j	|<qLtjdkr�tjj|�}ntj|�\}}}zpy8tjd
kr�tjj|�}	|jj|	�ntj||||�}	Wn2tk
�r*d	}	tjd
ttj�d��YnXWd	tjdk�rJ|�rJ|j�X|	�r^d|j	|	<qLtjd|�qLWd	S)z&
        Discover the plugins
        r�z*.pyrr}rrKrrNzimport_plugin_exception:%szimport_plugin:%s)rr)rr)rr)r�rr�globr6r7rrlrm�schedulerir�r�
getbooleanrnrorprqrrrsrurvrwrxrt�errorrb�exc_infory)
r�r�	filenamesr�r�rr�r�r�r�rrrr��s>




"
zAgent._plugins_initc
Cs(tjtj|ftjtjdd�}tjdtj�|j	�|j
jdd�}t|�}|j
j|d�}||p^d}|j
�xB|jdkr�|d	kr�tjd
tj�|�tj|�|d8}|j
�qjW|jdkr�tjdtj�|j	�tj|j	tj�|j�\}}|jd	ks�|�rtjdtj�||j|�|�r tj|�}	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)rK�Popenrl�
executable�PIPEr��debug�	threading�
currentThread�pidrr�ri�poll�
returncode�time�sleepr�r6�kill�signal�SIGTERM�communicate�pickler])
r��task�processr�r�r�Zticksr�r��retrrr�_subprocess_execution�s2

zAgent._subprocess_executioncCsb�x@|jrtjdtj��Ptjdtj�|jj��y|jj�}Wnt	k
rXPYnXtjdtj�|�t
|�}y|jj|d�}Wnd}YnXt
j
�}t|t�r�|j|�}nry<|jj||jj|g�i�|j|j|d�}|j|j�}Wn4tk
�r&tjd�dttj�d	�i}YnX|jj|||||d
��qW|jjtj��|jj�dS)zW
        Take queued execution requests, execute plugins and queue the results
        z%s:shutdownz%s:exec_queue:%iz
%s:task:%sr�r�)Zagent_cacheZplugin_exception�	exceptionr)�tsr�r�r�r�N)�shutdownr�rr�r�r��execute�qsize�
get_nowaitrrirrr�rdrr�r��updaterzr{rtr�rbrlr��metrics�put�cemetery�hire�release)r�r�r�r�r�r�rhrrr�
_execution�sD


zAgent._executioncCs tjdtj��|jjdd�}|jjdd�}|jjdd�}|jjdd�}|jjdd�}|jjdd	�}|jjdd
�}|jjdd�}g}	g}
d}�x�|r�d
}n|jjdd�}tj�}|jr�tjdtj��Ptj	dtj�|j
j�t|
��xX|j
j��r>y|
j
|j
j��Wq�tk
�r:}
ztjd|
�WYdd}
~
Xq�Xq�W|
�r�tdd�|
D��}tdd�|
D��}tj�}d}|||k�r�tj	d�d}d}n |||k�r�tjd�d}d}|�r�d}dd||fd�}tj	dtj|
ddd��|�o�|�s
tjd�d}�n�zԐy@tjtj�d�}tjd0k�rBtjj||d!d"�}ntj||d!d"�}|	�r�tjd#t|	��xj|	�r�|j d$d%|t!f|	d&|d'�|j"�}|j#�|j$d(k�r�|	d&=tj	d)|j$�nt%d*|j$���qjWtjd+�|j d$d%|t!ft&j't(tj|
�d,�j)��|d'�|j"�}|j#�|j$d(k�r@tj	d)|j$�d}nt%d*|j$��Wn�tk
�r�}
zntjd-|
�|d&k�r�t|	�|k�r�|	d&=tjd.|�tjd/�|	j
t&j't(tj|
�d,�j)���g}
WYdd}
~
XnXWd|j*�X|�r�g}
|tj�|}|d&kr�tj+|�q�WdS)1zA
        Take and collect data, send and clean if needed
        z%sr)r�r�rr�r�rr+r�r�Tr�z%s:shutdownz%s:data_queue:%i:collection:%izData queue error: %sNcss|]}|dVqdS)r�Nr)�.0r�rrr�	<genexpr>'szAgent._data.<locals>.<genexpr>css|]}|dVqdS)r�Nr)r�r�rrrr�(sFz
Max data spanzMax data agezapplication/jsonzApiKey %s:%s)zContent-type�
Authorizationzcollection: %sr%)rjrkz&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�rr�r�rrr�r�r�r�r)r�r5rOr�rtr��min�max�warningr\r|�ssl�create_default_context�certifi�whererlrn�http�client�HTTPSConnection�httplib�requestr�getresponser@�status�
ValueError�bz2�compressrbr?ryr�)r�r�r�Zmax_ageZmax_spanrr+r�r�Zcached_collectionsZ
collectionZinitial_dataZloop_tsr�Zfirst_tsZlast_ts�now�send�cleanr��ctx�
connectionra�sleep_intervalrrr�_datas�$





"
zAgent._datacCs tjd�tj|jd�j�dS)z/
        Initialize data worker thread
        r�)�targetN)r�rr��Threadr�start)r�rrrr�xs
zAgent._data_worker_initcCs<tjdkrtj�}ntj�}|jj|�tjd|j��dS)z,
        Dumps configuration object
        rz
Config: %sN)r)	rlrn�io�StringIOrrDr�r�getvalue)r��bufrrrr�s


zAgent._dump_configrcCs�|j�}g}xztjtjj|d��D]b}t|�}|dkr8q"|j|�|dkrd|jj|d�r�|j	|�q"|dkr"|jj|d�s"|j	|�q"W|S)z0
        Return list with plugins names
        z*.pyr}r�disabled)
rr�r6r7rrir�rr�rO)r�rrr}r�r~rrrr�s
zAgent._get_pluginscCsVtjd|jj��x>y|jj�}Wntk
r8PYnXtjd|�|j�qWdS)zb
        Join with dead workers
        Workaround for https://bugs.python.org/issue37788
        zcemetery:%iz
joining:%sN)r�r�r�r�r�rr)r��threadrrr�_rip�sz
Agent._ripcs�tjd�|jjdd�}tj|jjdd��|_�y��x�|j�t	j	��tj
dtj��xx|jj
�r�|jj�}|d}tj
d|�|jd	�}|r�t��|jj|d�|j|<t|tj�r�|j|d	<|jj|�qXW�fd
d�|jj�D�}x�|D]�}tj
d|�|j|=|jj|�|jjd
��rzy&tj|jd�}|j�tj
d|�Wn0tk
�rv}ztjd|�WYdd}~XnXq�tjd�|jj�dd|jjdd�id��q�Wdt	j	��}|dk�r�t	j |�q6tjd�t	j d�q6WWn�t!k
�r�tjt"j#�d�tjd�|j�d}	x�|	�r�tj$�}
tjd|
�dd�|
D�}	|	�shtjd�t"j%d�d|_&tjdt'|	��x&|	D]}tjd ||�|j(|��q�W�q&WYn2tk
�r�}ztj)d!|�WYdd}~XnXdS)"z.
        Start all the worker threads
        zAgent main looprr�r�r�z
%i threadsr�z
metrics:%sr�csg|]\}}|�kr|�qSrr)r��what�when)r�rr�
<listcomp>�szAgent.run.<locals>.<listcomp>z
scheduling:%sF)rznew_execution_worker_thread:%szCan not start new thread: %sNZthreads_cappedZagent_internalZthreads_capping)r�r�r�g�?rz'not enough time to start worker threadsg�������?z
Shutting downTzRemaining threads: %scSs(g|] }|j�rt|tj�r|�qSr)�isDaemonrdr��_MainThread)r�rrrrr�s
zBye!zWaiting for %i threads to exitzJoining with %s/%fzWorker error: %s)*r�rr�getfloatr��	Semaphorer�r�rr�r��activeCountr�r�r�rr3r�rd�types�
ModuleType�__file__r)r��itemsr��acquirerr�rrtr�r��KeyboardInterruptrlr��	enumerate�exitr�r5rr�)r�r�r�r�rhr�rr�r�wait_forZall_threadsr)r�rr{�s|





 






z	Agent.runN)F)r)rg�
__module__�__qualname__rr�r�r)r�r�r�rr�r�r�r�r�r�rr�r�rrr{rrrrrs&
)!,/r
rcCslttj�dk�r^tjdjd�r8tjddd�tjd<tjddkr^tdjd��tj�n�tjdd	kr�tt��tj�n�tjdd
kr�tt�tj�n�tjddkr�tjd=tjt	��n�tjddkr�tjd=tjt
��nttjdd
k�rtjd=tjt	dd��nJtjddk�r<tjttjdd���n tdtjdtjd�tjd�n
t
�j�dS)Nr$z--r%r!r�!Run without options to run agent.�,Acceptable options (leading -- is optional):�4    help, info, version, hello, insecure-hello, testr�versionrIz
count-domainszinsecure-hellor�)rE�testzInvalid option:)�file)r r!r")r5rl�argv�
startswithrBrrrrrIrcr�r�rr{rrrr�mains:



r(�__main__)r)rr)r )D�
__future__rr�rlrnZ
past.builtinsr�ImportErrorrbr��http.clientr��queuerrrr�r�r�importlib.utilrorrr�r�r�r\Z
simplejsonr�r6r�r�r=rKr�r�r�optparser�urllib.parser	r
�urllib.requestrr�urllib.errorr
�urllib�urllib2rr7�dirname�abspathrr8rr�r�rrIrTrYr9rcrir�rr(rgrrrr�<module>s�







8!0i$


Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists