????

Your IP : 3.145.105.194


Current Path : /usr/lib/python3.6/site-packages/glances/outputs/__pycache__/
Upload File :
Current File : //usr/lib/python3.6/site-packages/glances/outputs/__pycache__/glances_bottle.cpython-36.opt-1.pyc

3

Eo�cS�@s�dZddlZddlZddlZddlmZddlZddlZddlZddl	m
Z
ddlmZddl
mZddlmZy,ddlmZmZmZmZmZmZmZmZWn(ek
r�ejd	�ejd
�YnXdd�ZGd
d�de�ZGdd�de�Z dS)zWeb interface class.�N)�open)�
json_dumps)�b)�Timer)�logger)�Bottle�static_file�abort�response�request�
auth_basic�template�
TEMPLATE_PATHzABottle module not found. Glances cannot start in web server mode.�cs��fdd�}ddd��|S)z@Compress result with deflate algorithm if the client ask for it.csf�||�}tjdjtjtjdd�tjj�D���dtjjdd�krXdt	jd<�|�}n
dt	jd<|S)	z@Wrapper that take one function and return the compressed result.z%Receive {} {} request with header: {}cSs g|]}dj|tjj|���qS)z{}: {})�formatr�headers�get)�.0�h�r�$/usr/lib/python3.6/glances_bottle.py�
<listcomp>*sz-compress.<locals>.wrapper.<locals>.<listcomp>ZdeflatezAccept-Encoding�zContent-EncodingZidentity)
r�debugrr�methodZurlr�keysrr
)�args�kwargs�ret)�deflate_compress�funcrr�wrapper#s



zcompress.<locals>.wrapper�cSs0tj|tjtjtjtj�}|jt|��|j�S)z/Compress given data using the DEFLATE algorithm)	�zlibZcompressobjZDEFLATEDZ	MAX_WBITSZ
DEF_MEM_LEVELZZ_DEFAULT_STRATEGY�compressr�flush)�dataZcompress_levelZzobjrrrr4sz"compress.<locals>.deflate_compress)r"r)r r!r)rr rr$ s

r$c@sBeZdZdZdZd>dd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zd?dd�Z
dd�Zedd��Zedd��Zedd��Zedd��Zed d!��Zed"d#��Zed$d%��Zed@d'd(��Zed)d*��Zed+d,��ZdAd.d/�Zed0d1��ZedBd2d3��Zed4d5��Zed6d7��Zed8d9��Zed:d;��Z ed<d=��Z!dS)C�
GlancesBottlez)This class manages the Bottle Web server.�3NcCs�||_||_d|_td�|_|j|�dj|jj|jj�|_	t
�|_|jjt
��|jdkrn|jjt|j��|j�tjjtjjtjjt��d�|_tjdtjjtjjtjjt��d��dS)Nrz
http://{}:{}/rz
static/publiczstatic/templates)�configr�statsr�timer�load_configr�bind_address�port�bind_urlr�_appZinstall�
EnableCors�passwordr�
check_auth�_route�os�path�join�dirname�realpath�__file__�STATIC_PATHr�insert)�selfr)rrrr�__init__Fs


 zGlancesBottle.__init__cCs@|dk	r<|jd�r<tjd�|jdddd�}tjdj|��dS)z3Load the outputs section of the configuration file.NZoutputsz0Read number of processes to display in the WebUIZmax_processes_display)�defaultz/Number of processes to display in the WebUI: {})Zhas_sectionrrZ	get_valuer)r=r)�nrrrr,ls
zGlancesBottle.load_configcCs&|jj�r"|jj�t|jj�|_dS)N)r+Zfinishedr*�updaterrZcached_time)r=rrr�
__update__ts

zGlancesBottle.__update__cCs|j�S)N)r0)r=rrr�appzszGlancesBottle.appcCsD||jjkr<ddlm}|||jd�}|j|jj|j|��SdSdS)z2Check if a username/password combination is valid.r)�GlancesPassword)�usernamer)FN)rrEZglances.passwordrDr)Zcheck_passwordr2Zsha256_hash)r=rEr2rD�pwdrrrr3}s
zGlancesBottle.check_authcCsx|jjd|jd|jd�|jjd|jd|jd�|jjd|jd|jd�|jjd|jd|jd�|jjd|jd|jd�|jjd|jd|jd�|jjd	|jd|j	d�|jjd
|jd|j
d�|jjd|jd|jd�|jjd|jd|jd�|jjd
|jd|j
d�|jjd|jd|jd�|jjd|jd|jd�|jjd|jd|jd�|jjd|jd|jd�|jjd|jd|jd�|jjd|jd|jd�|jjd|jd|jd�|jjd|jd|jd�dj|j|j�}tj|�|jj�s^|jjdd|jd�|jjddg|jd�|jjdd|jd�dj|j�}nd}tj|�t|�dS)z
Define route.z/api/%s/statusZGET)r�callbackz/api/%s/configz/api/%s/config/<item>z/api/%s/argsz/api/%s/args/<item>z/api/%s/helpz/api/%s/pluginslistz/api/%s/allz/api/%s/all/limitsz/api/%s/all/viewsz/api/%s/<plugin>z/api/%s/<plugin>/historyz!/api/%s/<plugin>/history/<nb:int>z/api/%s/<plugin>/limitsz/api/%s/<plugin>/viewsz/api/%s/<plugin>/<item>z/api/%s/<plugin>/<item>/historyz(/api/%s/<plugin>/<item>/history/<nb:int>z/api/%s/<plugin>/<item>/<value>z/Glances RESTful API Server started on {}api/{}/�/z/<refresh_time:int>z/<filepath:path>z(Glances Web User Interface started on {}z&The WebUI is disable (--disable-webui)N)r0Zroute�API_VERSION�_api_status�_api_config�_api_config_item�	_api_args�_api_args_item�	_api_help�_api_plugins�_api_all�_api_all_limits�_api_all_views�_api�_api_history�_api_limits�
_api_views�	_api_item�_api_item_history�
_api_valuerr/r�inforZ
disable_webui�_index�	_resource�print)r=Zbindmsgrrrr4�s@


zGlancesBottle._routecCs�||_|jj�|_|jjr,tj|jddd�y$|jj	|jj
|jj|jjd�Wn4t
jk
r�}ztjdj|��WYdd}~XnXdS)zStart the bottle.r�)�newZ	autoraise)�hostr.�quietz*Error: Can not ran Glances Web server ({})N)r*ZgetPluginsList�plugins_listrZopen_web_browser�
webbrowserrr/r0Zrunr-r.r�socket�errorr�criticalr)r=r*�errr�start�s$zGlancesBottle.startcCsdS)zEnd the bottle.Nr)r=rrr�end�szGlancesBottle.endcCs0|dks|dkrt|jj�}|j�td|d�S)z(Bottle callback for index.html (/) file.Nr_z
index.html)�refresh_time)�intrZtimerBr
)r=rkrrrr\�szGlancesBottle._indexcCst||jd�S)z$Bottle callback for resources files.)�root)rr;)r=�filepathrrrr]�szGlancesBottle._resourcecCs
dt_dS)z�Glances API RESTful implementation.

        Return a 200 status code.
        This entry point should be used to check the API health.

        See related issue:  Web server health check endpoint #1988
        ��N)r
Zstatus)r=rrrrJ�s	zGlancesBottle._api_statuscCs\dt_|jjd�j�}yt|�}Wn4tk
rV}ztddt|��WYdd}~XnX|S)zXGlances API RESTful implementation.

        Return the help data or 404 error.
        zapplication/json; charset=utf-8�helpi�zCannot get help view data (%s)N)	r
�content_typer*�
get_pluginZ
get_view_datar�	Exceptionr	�str)r=Z	view_data�plistrhrrrrO�s$zGlancesBottle._api_helpcCsVdt_|j�yt|j�}Wn4tk
rP}ztddt|��WYdd}~XnX|S)aOGlances API RESTFul implementation.

        @api {get} /api/%s/pluginslist Get plugins list
        @apiVersion 2.0
        @apiName pluginslist
        @apiGroup plugin

        @apiSuccess {String[]} Plugins list.

        @apiSuccessExample Success-Response:
            HTTP/1.1 200 OK
            [
               "load",
               "help",
               "ip",
               "memswap",
               "processlist",
               ...
            ]

         @apiError Cannot get plugin list.

         @apiErrorExample Error-Response:
            HTTP/1.1 404 Not Found
        zapplication/json; charset=utf-8i�zCannot get plugin list (%s)N)r
rqrBrrcrsr	rt)r=rurhrrrrP�s$zGlancesBottle._api_pluginscCs�dt_|jjr`tjjtj�d�}yt	|��
}|j
�SQRXWn"tk
r^tjd|�YnX|j
�yt|jj��}Wn4tk
r�}ztddt|��WYdd}~XnX|S)z�Glances API RESTful implementation.

        Return the JSON representation of all the plugins
        HTTP/200 if OK
        HTTP/400 if plugin is not found
        HTTP/404 if others error
        zapplication/json; charset=utf-8zglances-debug.jsonNzDebug file (%s) not foundi�zCannot get stats (%s))r
rqrrr5r6r7�tempfileZ
gettempdirr�read�IOErrorrrBrr*ZgetAllAsDictrsr	rt)r=Zfname�f�statvalrhrrrrQs	
$zGlancesBottle._api_allcCsRdt_yt|jj��}Wn4tk
rL}ztddt|��WYdd}~XnX|S)z�Glances API RESTful implementation.

        Return the JSON representation of all the plugins limits
        HTTP/200 if OK
        HTTP/400 if plugin is not found
        HTTP/404 if others error
        zapplication/json; charset=utf-8i�zCannot get limits (%s)N)r
rqrr*ZgetAllLimitsAsDictrsr	rt)r=�limitsrhrrrrR<s	$zGlancesBottle._api_all_limitscCsRdt_yt|jj��}Wn4tk
rL}ztddt|��WYdd}~XnX|S)z�Glances API RESTful implementation.

        Return the JSON representation of all the plugins views
        HTTP/200 if OK
        HTTP/400 if plugin is not found
        HTTP/404 if others error
        zapplication/json; charset=utf-8i�zCannot get views (%s)N)r
rqrr*ZgetAllViewsAsDictrsr	rt)r=r{rhrrrrSNs	$zGlancesBottle._api_all_viewscCs~dt_||jkr$tdd||jf�|j�y|jj|�j�}Wn8tk
rx}ztdd|t	|�f�WYdd}~XnX|S)z�Glances API RESTful implementation.

        Return the JSON representation of a given plugin
        HTTP/200 if OK
        HTTP/400 if plugin is not found
        HTTP/404 if others error
        zapplication/json; charset=utf-8i�z)Unknown plugin %s (available plugins: %s)i�zCannot get plugin %s (%s)N)
r
rqrcr	rBr*rrZ	get_statsrsrt)r=�pluginrzrhrrrrT`s	
(zGlancesBottle._apircCs�dt_||jkr$tdd||jf�|j�y|jj|�jt|�d�}Wn8t	k
r�}ztdd|t
|�f�WYdd}~XnX|S)z�Glances API RESTful implementation.

        Return the JSON representation of a given plugin history
        Limit to the last nb items (all if nb=0)
        HTTP/200 if OK
        HTTP/400 if plugin is not found
        HTTP/404 if others error
        zapplication/json; charset=utf-8i�z)Unknown plugin %s (available plugins: %s))�nbi�z!Cannot get plugin history %s (%s)N)r
rqrcr	rBr*rr�get_stats_historyrlrsrt)r=r|r}rzrhrrrrUys

(zGlancesBottle._api_historycCstdt_||jkr$tdd||jf�y|jj|�j}Wn8tk
rn}ztdd|t|�f�WYdd}~XnX|S)z�Glances API RESTful implementation.

        Return the JSON limits of a given plugin
        HTTP/200 if OK
        HTTP/400 if plugin is not found
        HTTP/404 if others error
        zapplication/json; charset=utf-8i�z)Unknown plugin %s (available plugins: %s)i�z$Cannot get limits for plugin %s (%s)N)	r
rqrcr	r*rrr{rsrt)r=r|rrhrrrrV�s	
(zGlancesBottle._api_limitscCsvdt_||jkr$tdd||jf�y|jj|�j�}Wn8tk
rp}ztdd|t|�f�WYdd}~XnX|S)z�Glances API RESTful implementation.

        Return the JSON views of a given plugin
        HTTP/200 if OK
        HTTP/400 if plugin is not found
        HTTP/404 if others error
        zapplication/json; charset=utf-8i�z)Unknown plugin %s (available plugins: %s)i�z#Cannot get views for plugin %s (%s)N)	r
rqrcr	r*rrZ	get_viewsrsrt)r=r|rrhrrrrW�s	
(zGlancesBottle._api_viewsFcCs�dt_||jkr$tdd||jf�|j�|dkr�|rT|jj|�j|t|�d�}n|jj|�j	|�}|dkr�tdd||r~dnd	|f�nD|r�d}n|jj|�j
||�}|dkr�tdd
|r�dnd	|||f�|S)z+Father method for _api_item and _api_value.zapplication/json; charset=utf-8i�z)Unknown plugin %s (available plugins: %s)N)r}i�z!Cannot get item %s%s in plugin %szhistory rz&Cannot get item %s(%s=%s) in plugin %s)r
rqrcr	rBr*rrr~rlZget_stats_itemZget_stats_value)r=r|�item�value�historyr}rrrr�_api_itemvalue�s"
zGlancesBottle._api_itemvaluecCs|j||�S)z�Glances API RESTful implementation.

        Return the JSON representation of the couple plugin/item
        HTTP/200 if OK
        HTTP/400 if plugin is not found
        HTTP/404 if others error

        )r�)r=r|rrrrrX�s
zGlancesBottle._api_itemcCs|j||dt|�d�S)z�Glances API RESTful implementation.

        Return the JSON representation of the couple plugin/history of item
        HTTP/200 if OK
        HTTP/400 if plugin is not found
        HTTP/404 if others error

        T)r�r})r�rl)r=r|rr}rrrrY�s
zGlancesBottle._api_item_historycCs|j|||�S)z�Glances API RESTful implementation.

        Return the process stats (dict) for the given item=value
        HTTP/200 if OK
        HTTP/400 if plugin is not found
        HTTP/404 if others error
        )r�)r=r|rr�rrrrZ�s	zGlancesBottle._api_valuecCsRdt_yt|jj��}Wn4tk
rL}ztddt|��WYdd}~XnX|S)z�Glances API RESTful implementation.

        Return the JSON representation of the Glances configuration file
        HTTP/200 if OK
        HTTP/404 if others error
        zapplication/json; charset=utf-8i�zCannot get config (%s)N)r
rqrr)�as_dictrsr	rt)r=�	args_jsonrhrrrrKs$zGlancesBottle._api_configcCspdt_|jj�}||kr&tdd|�yt||�}Wn4tk
rj}ztddt|��WYdd}~XnX|S)z�Glances API RESTful implementation.

        Return the JSON representation of the Glances configuration item
        HTTP/200 if OK
        HTTP/400 if item is not found
        HTTP/404 if others error
        zapplication/json; charset=utf-8i�zUnknown configuration item %si�zCannot get config item (%s)N)r
rqr)r�r	rrsrt)r=rZconfig_dictr�rhrrrrLs	
$zGlancesBottle._api_config_itemcCsRdt_ytt|j��}Wn4tk
rL}ztddt|��WYdd}~XnX|S)z�Glances API RESTful implementation.

        Return the JSON representation of the Glances command line arguments
        HTTP/200 if OK
        HTTP/404 if others error
        zapplication/json; charset=utf-8i�zCannot get args (%s)N)r
rqr�varsrrsr	rt)r=r�rhrrrrM.s$zGlancesBottle._api_argscCsndt_||jkrtdd|�ytt|j�|�}Wn4tk
rh}ztddt|��WYdd}~XnX|S)z�Glances API RESTful implementation.

        Return the JSON representation of the Glances command line arguments item
        HTTP/200 if OK
        HTTP/400 if item is not found
        HTTP/404 if others error
        zapplication/json; charset=utf-8i�zUnknown argument item %si�zCannot get args item (%s)N)r
rqrr	rr�rsrt)r=rr�rhrrrrNAs	
$zGlancesBottle._api_args_item)NN)N)r)NFr)r)"�__name__�
__module__�__qualname__�__doc__rIr>r,rBrCr3r4rirjr\r]r$rJrOrPrQrRrSrTrUrVrWr�rXrYrZrKrLrMrNrrrrr'As@
&
+

&
 r'c@seZdZdZdZdd�ZdS)r1Zenable_corsrcs�fdd�}|S)Ncs6dtjd<dtjd<dtjd<tjdkr2�||�SdS)N�*zAccess-Control-Allow-OriginzGET, POST, PUT, OPTIONSzAccess-Control-Allow-Methodsz<Origin, Accept, Content-Type, X-Requested-With, X-CSRF-TokenzAccess-Control-Allow-HeadersZOPTIONS)r
rrr)rr)�fnrr�_enable_cors^s




z&EnableCors.apply.<locals>._enable_corsr)r=r��contextr�r)r�r�apply]szEnableCors.applyN)r�r�r��nameZapir�rrrrr1Ysr1)!r�r5�sysrv�iorrdr#reZglances.globalsrZglances.compatrZ
glances.timerrZglances.loggerrZbottlerrr	r
rrr
r�ImportErrorrg�exitr$�objectr'r1rrrr�<module>
s.,
!