????

Your IP : 3.19.70.25


Current Path : /lib/python3.6/site-packages/glances/__pycache__/
Upload File :
Current File : //lib/python3.6/site-packages/glances/__pycache__/stats.cpython-36.pyc

3

Eo�c
/�@sldZddlZddlZddlZddlZddlZddlmZddlm	Z	m
Z
mZddlm
Z
Gdd�de�ZdS)zThe stats manager.�N)�logger)�exports_path�plugins_path�sys_path)�Counterc@s�eZdZdZdZd1dd�Zdd�Zdd	�Zd2d
d�Zd3dd
�Z	d4dd�Z
d5dd�Zd6dd�Zd7dd�Z
dd�Zd8dd�Zdd�Zdd�Zd9dd �Zd:d!d"�Zd;d#d$�Zd<d%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�ZdS)=�GlancesStatsz+This class stores, updates and gives stats.�glances_NcCs"||_||_d|_|j|j�dS)NT)�config�args�first_export�load_modules)�selfr	r
�r�/usr/lib/python3.6/stats.py�__init__szGlancesStats.__init__cCs�|jd�rF|td�d�j�}|j|}t|d�r<t|d�St|��nN|jd�r�|td�d�j�}|j|}t|d�r�t|d�St|��nt|��dS)aOverwrite the getattr method in case of attribute is not found.

        The goal is to dynamically generate the following methods:
        - getPlugname(): return Plugname stat in JSON format
        - getViewsPlugname(): return views of the Plugname stat in JSON format
        ZgetViewsNZget_json_views�getZ	get_stats)�
startswith�len�lower�_plugins�hasattr�getattr�AttributeError)r
�itemZplugname�pluginrrr�__getattr__)s









zGlancesStats.__getattr__cCsFtjt�|_|j|d�tjt�|_tjt�|_|j|d�tt	_
dS)z,Wrapper to load: plugins and export modules.)r
N)�collections�defaultdict�dictr�load_plugins�_exports�_exports_all�load_exportsr�sys�path)r
r
rrrrKszGlancesStats.load_modulescCs
|t|j�d
�j�}y(t|dd��}|j||d�|j|<WnZtk
r�}z>tjdj	||��tj
tj��|dk	r�t
|d|d�WYdd}~XnnX|dk	�rt|dd�r�tjd|t|d	|d��t
|d|t|d	|d��nt
|d|t|d|d��dS)z>Load the plugin (script), init it and add to the _plugin dict.�N)r
r	z+Error while initializing the {} plugin ({})Zdisable_FZdisable_allz%s => %sZenable_���r&)r�headerr�
__import__ZPluginr�	ExceptionrZcritical�format�error�	traceback�
format_exc�setattrr�debug)r
Z
plugin_scriptr
r	�namer�errr�_load_plugin_s"
 zGlancesStats._load_plugincCs�t�}xntjt�D]`}|j|j�r|jd�r||jdkr|j�|jtj	j
|�||jd�tj
dj||j���qWtj
dj|j���dS)z)Load all plugins in the 'plugins' folder.z.pyz	plugin.py)r
r	zPlugin {} started in {} secondszActive plugins list: {}N)r�os�listdirrrr'�endswith�resetr2r$�basenamer	rr/r*r�getPluginsList)r
r
Zstart_durationrrrrr~s$zGlancesStats.load_pluginscCs"|dkrdSd}tt�d�}x�tjt�D]�}tjj|�t|�d
�j�}|j	|�r*|j
d�r*||dkr*||dkr*tjj|�dd�|j|<t|j
d	|t|j
d	|d��q*WxT|jD]J}t|j
d	|d�r�t|j|�}|j||jd
�|j|<|j||j|<q�Wtjdj|j���dS)z0Load all export modules in the 'exports' folder.NFrr
r%z.pyz	export.pyz
history.pyZexport_)r
r	zActive exports modules list: {}Tr&r&)�vars�localsr3r4rr$r7rrrr5r!r.r
rr(ZExportr	r rr/r*�getExportsList)r
r
r'Zargs_varrZexport_nameZ
export_modulerrrr"�s&

$zGlancesStats.load_exportsTcs,|r�fdd��jD�Sdd��jD�SdS)z�Return the plugins list.

        if enable is True, only return the active plugins (default)
        if enable is False, return all the plugins

        Return: list of plugin name
        csg|]}�j|j�r|�qSr)rZ
is_enabled)�.0�p)r
rr�
<listcomp>�sz/GlancesStats.getPluginsList.<locals>.<listcomp>cSsg|]}|�qSrr)r<r=rrrr>�sN)r)r
�enabler)r
rr8�szGlancesStats.getPluginsListcCs(|rdd�|jD�Sdd�|jD�SdS)z�Return the exports list.

        if enable is True, only return the active exporters (default)
        if enable is False, return all the exporters

        :return: list of export module names
        cSsg|]}|�qSrr)r<r1rrrr>�sz/GlancesStats.getExportsList.<locals>.<listcomp>cSsg|]}|�qSrr)r<r1rrrr>�sN)r r!)r
r?rrrr;�szGlancesStats.getExportsListcCs$x|jD]}|j|j|�qWdS)z;Load the stats limits (except the one in the exclude list).N)r�load_limits)r
r	r=rrrr@�szGlancesStats.load_limitscCsNxH|jD]>}|j|j�rq|j|j�|j|j�|j|j�qWdS)z#Wrapper method to update the stats.N)rZis_disabled�updateZupdate_stats_historyZupdate_views)r
r=rrrrA�szGlancesStats.updatecCsd|jrtjd�d|_dS|p i}x<|jD]2}tjd|�tj|j|j|fd�}|j�q*WdS)zXExport all the stats.

        Each export module is ran in a dedicated thread.
        zSDo not export stats during the first iteration because some information are missingFz Export stats using the %s module)�targetr
T)rrr/r �	threadingZThreadrA�start)r
Zinput_statsr1Zthreadrrr�export�s
zGlancesStats.exportcs�fdd��jD�S)zReturn all the stats (list).csg|]}�j|j��qSr)r�get_raw)r<r=)r
rrr>�sz'GlancesStats.getAll.<locals>.<listcomp>)r)r
r)r
r�getAll�szGlancesStats.getAllcs�fdd��jD�S)zReturn all the stats (dict).csi|]}�j|j�|�qSr)rrF)r<r=)r
rr�
<dictcomp>�sz-GlancesStats.getAllAsDict.<locals>.<dictcomp>)r)r
r)r
r�getAllAsDict�szGlancesStats.getAllAsDictcs$|dkr�j�}�fdd��jD�S)z�Return all the stats to be exported (list).

        Default behavior is to export all the stat
        if plugin_list is provided, only export stats of given plugin (list)
        Ncsg|]}�j|j��qSr)r�
get_export)r<r=)r
rrr>sz.GlancesStats.getAllExports.<locals>.<listcomp>)r8r)r
�plugin_listr)r
r�
getAllExports�szGlancesStats.getAllExportscs"|dkr�j�}�fdd�|D�S)z�Return all the stats to be exported (list).

        Default behavior is to export all the stat
        if plugin_list is provided, only export stats of given plugin (list)
        Ncsi|]}�j|j�|�qSr)rrJ)r<r=)r
rrrH
sz4GlancesStats.getAllExportsAsDict.<locals>.<dictcomp>)r8)r
rKr)r
r�getAllExportsAsDictsz GlancesStats.getAllExportsAsDictcs"|dkr�j�}�fdd�|D�S)z�Return the plugins limits list.

        Default behavior is to export all the limits
        if plugin_list is provided, only export limits of given plugin (list)
        Ncsg|]}�j|j�qSr)r�limits)r<r=)r
rrr>sz-GlancesStats.getAllLimits.<locals>.<listcomp>)r8)r
rKr)r
r�getAllLimitsszGlancesStats.getAllLimitscs"|dkr�j�}�fdd�|D�S)z�Return all the stats limits (dict).

        Default behavior is to export all the limits
        if plugin_list is provided, only export limits of given plugin (list)
        Ncsi|]}�j|j|�qSr)rrN)r<r=)r
rrrH#sz3GlancesStats.getAllLimitsAsDict.<locals>.<dictcomp>)r8)r
rKr)r
r�getAllLimitsAsDictszGlancesStats.getAllLimitsAsDictcs�fdd��jD�S)zReturn the plugins views.csg|]}�j|j��qSr)r�	get_views)r<r=)r
rrr>'sz,GlancesStats.getAllViews.<locals>.<listcomp>)r)r
r)r
r�getAllViews%szGlancesStats.getAllViewscs�fdd��jD�S)z"Return all the stats views (dict).csi|]}�j|j�|�qSr)rrQ)r<r=)r
rrrH+sz2GlancesStats.getAllViewsAsDict.<locals>.<dictcomp>)r)r
r)r
r�getAllViewsAsDict)szGlancesStats.getAllViewsAsDictcCs|jS)zReturn the plugin list.)r)r
rrr�get_plugin_list-szGlancesStats.get_plugin_listcCs||jkr|j|SdSdS)zReturn the plugin name.N)r)r
Zplugin_namerrr�
get_plugin1s

zGlancesStats.get_plugincCs@x|jD]}|j|j�qWx|jD]}|j|j�q&WdS)zEnd of the Glances stats.N)r �exitr)r
r1r=rrr�end8szGlancesStats.end)NN)NN)N)N)T)T)N)N)N)N)N)N)�__name__�
__module__�__qualname__�__doc__r'rrrr2rr"r8r;r@rArErGrIrLrMrOrPrRrSrTrUrWrrrrrs0
"


#







r)r[rr3r#rCr,Zglances.loggerrZglances.globalsrrrZ
glances.timerr�objectrrrrr�<module>
s