HEX
Server: LiteSpeed
System: Linux baran.dnsprotection.me 4.18.0-553.83.1.lve.el8.x86_64 #1 SMP Wed Nov 12 10:04:12 UTC 2025 x86_64
User: ddir12 (3618)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: //lib/python3.6/site-packages/glances/plugins/__pycache__/glances_docker.cpython-36.opt-1.pyc
3

Eo�c�|�@s&dZddlZddlZddlZddlmZddlmZmZm	Z	m
Z
ddlmZddl
mZddlmZmZddlmZyddlZdd	lmZmZWn6ek
r�Zzd
Zejdje��WYddZ[XnXdZd
ddd�gZddddgZddddddd�Z Gdd�de�Z!Gdd�dej"�Z#d d!�Z$dS)"zDocker plugin.�N)�deepcopy)�iterkeys�
itervalues�	nativestr�pretty_date)�logger)�
GlancesPlugin)�
sort_stats�glances_processes)�getTimeSinceLastUpdate)�parser�tzTz=Error loading Docker deps Lib. Docker plugin is disabled ({})F�cpu_percentzContainer CPU consumption in %�%)�name�descriptionZy_unit�cpu�io�memory�networkzdisk IOzCPU consumptionzmemory consumptionZuptimezcontainer name�None)�io_countersr�memory_usage�	cpu_timesrNcs�eZdZdZd%�fdd�	Z�fdd�Zdd�Zd	d
�Zdd�Zd
d�Z	e
je
jdd���Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Z�fdd�Zd&dd �Zd!d"�Zd#d$�Z�ZS)'�Pluginz\Glances Docker plugin.

    stats is a dict: {'version': {...}, 'containers': [{}, {}]}
    Ncsbtt|�j||td�||_||_d|_|j�|_i|_	i|_
i|_d|_|j
�|jjd�dS)zInit the plugin.)�args�config�items_history_listTNr)�superr�__init__rrrZ
display_curse�connect�
docker_client�thread_list�network_old�io_old�sort_key�updateZ
refresh_timer�set)�selfrr)�	__class__��$/usr/lib/python3.6/glances_docker.pyrNs
zPlugin.__init__cs.xt|j�D]}|j�qWtt|�j�dS)z+Overwrite the exit method to close threads.N)rr"�stoprr�exit)r(�t)r)r*r+r-tszPlugin.exitcCsdS)zReturn the key of the list.rr*)r(r*r*r+�get_key{szPlugin.get_keycCstyt|jd�}Wn6tk
rH}ztjdj|��g}WYdd}~XnXx$|D]}xtD]}|j|�qZWqPW|S)z~Overwrite the default export method.

        - Only exports containers
        - The key is the first container name
        �
containersz&docker plugin - Docker export error {}N)r�stats�KeyErrorr�debug�format�export_exclude_list�pop)r(�ret�e�	container�ir*r*r+�
get_exports

zPlugin.get_exportcCsHytj�}Wn6tk
rB}ztjdj|��d}WYdd}~XnX|S)zConnect to the Docker server.z.docker plugin - Can not connect to Docker ({})N)�dockerZfrom_env�	Exceptionr�errorr4)r(r7r8r*r*r+r �szPlugin.connectcCs.|jd�}t|�dkrdS|dj�dkSdS)z�Return the all tag of the Glances/Docker configuration file.

        # By default, Glances only display running containers
        # Set the following key to True to display all containers
        all=True
        �allrF�trueN)Zget_conf_value�len�lower)r(Zall_tagr*r*r+�_all_tag�s
zPlugin._all_tagc	#Csr|j�}ts|jdkr|jS|jdk�rPy|jj�|d<WnRtk
r�}z6tjdj	|j
|��d|jkrr|jd=g|jd<|jSd}~XnXy|jjj|j
�d�p�g}Wn@tk
r�}z$tjdj	|j
|��g|jd<|jSd}~XnXxR|D]J}|j|jkr�tjdj	|j
|jdd	���t|�}||j|j<|j�q�Wtt|j��td
d�|D��}x@|D]8}tjdj	|j
|dd	���|j|j�|j|=�qfWg|d<�x�|D�]�}|jt|j���sΐq�i}|j�|d
<t|j�|d<|j|d<|jj|d<|jdd|d<g|d<|jdjdd��rL|dj|jdjdg��|jdjdd��r||dj|jdjdg��|d�s�d|d<|dd/k�r�|j|j|j|jj�|d<|djdd�|d<|j |j|j|jj�|d<|djdd�|d<|djdd�dk	�r*|d|dd8<|j!|j|j|jj�|d <|d jd!d�|d"<|d jd#d�|d$<|j"|j|j|jj�|d%<|d%jd&d�|d'<|d%jd(d�|d)<t#t$j%|jdd*�j&t'j(��j)dd+��|d,<nXi|d<d|d<i|d<d|d-<i|d <d|d"<d|d$<i|d%<d|d'<d|d)<d|d,<|dj*|��q�Wn|jd.k�r\t+|�\|_,|_|jS)0z+Update Docker stats using the input method.NZlocal�versionz*{} plugin - Cannot get Docker version ({})r0)r?z+{} plugin - Cannot get containers list ({})z*{} plugin - Create thread for container {}�cSsg|]
}|j�qSr*)�id)�.0�cr*r*r+�
<listcomp>�sz!Plugin.update.<locals>.<listcomp>z,{} plugin - Stop thread for old container {}�keyrZIdZImageZState�Status�CommandZConfigZ
EntrypointZCmd�running�pausedr�totalrr�usager�cacher�iorZio_r�iowZio_wr�rxZ
network_rx�txZ
network_txZ	StartedAt)Ztzinfo�Uptime�memory_percentZsnmp)rMrN)-Zget_init_value�import_error_tagr!r1Zinput_methodrDr=rr>r4Zplugin_namer0�listrCrFr"r3�ThreadDockerGrabber�startr'rr,Z
is_displayrrr/ZimageZtagsZattrs�get�extend�get_docker_cpu�get_docker_memory�
get_docker_io�get_docker_networkrr�parseZ
astimezoner
Ztzlocal�replace�append�sort_docker_statsr%)	r(r1r8r0r9r.Zabsent_containers�container_idZcontainer_statsr*r*r+r&�s�




 



z
Plugin.updatec	CsFddi}y||dd|dddd�}|dd|dddd�}|djd	d
�|d<|dd
kr�t|dddpzg�|d<Wn>tk
r�}z"tjd
j||��tj|�WYd
d
}~Xn�Xy<|d|d}|d|d}|||dd|d<Wn@tk
�r@}z"tjdj||��tj|�WYd
d
}~XnX|S)z�Return the container CPU usage.

        Input: id is the full container id
               all_stats is the output of the stats method of the Docker API
        Output: a dict {'total': 1.49}
        rOg�	cpu_statsZsystem_cpu_usageZ	cpu_usageZtotal_usage)�systemrOZprecpu_statsZonline_cpusNZnb_coreZpercpu_usagez;docker plugin - Cannot grab CPU usage for container {} ({})rhgY@z>docker plugin - Cannot compute CPU usage for container {} ({}))r\rAr2rr3r4�	TypeError)	r(rf�	all_statsrgrZprecpur8Z	cpu_deltaZsystem_cpu_deltar*r*r+r^Cs(

 zPlugin.get_docker_cpucCs�i}y�|dd|d<|dd|d<d|ddkrL|ddd|d<n.d|ddkrr|ddd|d<nd|d<|ddjdd�|d<|djd	d�|d	<WnBttfk
r�}z"tjd
j||��tj|�WYdd}~XnX|S)z�Return the container MEMORY.

        Input: id is the full container id
               all_stats is the output of the stats method of the Docker API
        Output: a dict {'rss': 1015808, 'cache': 356352,  'usage': ..., 'max_usage': ...}
        �memory_statsrP�limitZrssr1Z	total_rssNrQZ	max_usagez;docker plugin - Cannot grab MEM usage for container {} ({}))r\r2rirr3r4)r(rfrjrkr8r*r*r+r_lszPlugin.get_docker_memorycCsi}y|d}Wn<tk
rL}z tjdj||��tj|�|Sd}~XnXy$|dd|d<|dd|d<Wn>tk
r�}z"tjd	j||��tj|�WYdd}~Xn^Xtd
j|��|d<||jk�r|d|j|d|d<|d|j|d|d
<||j|<|S)azReturn the container network usage using the Docker API (v1.0 or higher).

        Input: id is the full container id
        Output: a dict {'time_since_update': 3000, 'rx': 10, 'tx': 65}.
        with:
            time_since_update: number of seconds elapsed between the latest grab
            rx: Number of bytes received
            tx: Number of bytes transmitted
        Znetworksz;docker plugin - Cannot grab NET usage for container {} ({})NZeth0Zrx_bytesZ
cumulative_rxZtx_bytesZ
cumulative_txzIdocker plugin - Cannot grab network interface usage for container {} ({})z
docker_net_{}�time_since_updaterTrU)r2rr3r4rr#)r(rfrjZnetwork_newZ	net_statsr8r*r*r+ra�s(

zPlugin.get_docker_networkcCs,i}y|d}Wn<tk
rL}z tjdj||��tj|�|Sd}~XnXy@|d}dd�|D�dd|d	<d
d�|D�dd|d<Wn<ttttfk
r�}ztjdj||��WYdd}~Xn^Xtdj|��|d
<||jk�r|d	|j|d	|d<|d|j|d|d<||j|<|S)aqReturn the container IO usage using the Docker API (v1.0 or higher).

        Input: id is the full container id
        Output: a dict {'time_since_update': 3000, 'ior': 10, 'iow': 65}.
        with:
            time_since_update: number of seconds elapsed between the latest grab
            ior: Number of bytes read
            iow: Number of bytes written
        Zblkio_statsz@docker plugin - Cannot grab block IO usage for container {} ({})N�io_service_bytes_recursivecSs g|]}|dj�dkr|�qS)�op�read)rB)rGr:r*r*r+rI�sz(Plugin.get_docker_io.<locals>.<listcomp>r�valueZcumulative_iorcSs g|]}|dj�dkr|�qS)ro�write)rB)rGr:r*r*r+rI�sZcumulative_iowzdocker_io_{}rmrRrS)	r2rr3r4ri�
IndexError�AttributeErrorrr$)r(rfrjZio_newZio_statsr8rnr*r*r+r`�s&
$
zPlugin.get_docker_iocCstjtjd�S)z:Return the user ticks by reading the environment variable.�
SC_CLK_TCK)�os�sysconf�
sysconf_names)r(r*r*r+�get_user_ticks�szPlugin.get_user_tickscCs
|jdS)zZReturn stats for the action.

        Docker will return self.stats['containers']
        r0)r1)r(r*r*r+�get_stats_action�szPlugin.get_stats_actioncsFtt|�j�d|jkrdS�x"|jdD�]}iid�|j||j�<d|kr�d|dkr�|j|dd|dd|dd�}|d	kr�|j|dddd
�}||j||j�dd<d|ko�d
|dkr*|j|dd
|dd|dd|dd�}|d	k�r$|j|dd
|dddd�}||j||j�dd<q*WdS)zUpdate stats views.r0F)r�memrrOrZ_cpu)�header�
action_key�DEFAULT)r|�
decorationrrPrlZ_mem)�maximumr|r}r{)r�r|T)rr�update_viewsr1Zviewsr/Z	get_alert)r(r:Zalert)r)r*r+r��s"
$*
 zPlugin.update_viewsc>Csg}|js0d|jks0t|jd�dks0|j�r4|Sdjd�}|j|j|d��djt|jd��}|j|j|��djt|j�}|j|j|��|j|j��|j|j��t	|j
dk	r�|j
jd	d
dd�ndtt|jdd
d�d�d��}djd|d�}|j|j||jdk�rdnd��djd�}|j|j|��djd�}|j|j|��djd�}|j|j||jdk�rxdnd��djd�}|j|j||jdk�r�dnd��djd �}|j|j|��djd!�}|j|j|��d"jd#�}|j|j|��djd$�}|j|j|��d"jd%�}|j|j|��d&jd'�}|j|j|���x�|jdD�]�}|j|j��|j|j|j
||d(���|j|d�}dj|ddd)��}|j|j||��|d�r�dj|d�}n
djd*�}|j|j||��yd+j|d,d-�}Wn tk
�r&djd*�}YnX|j|j||j|dd,d.d/���ydj|j|d0d1��}Wn tk
�r�djd*�}YnX|j|j||j|dd2d.d/���ydj|j|d0d3��}Wn tk
�r�djd*�}YnX|j|j|��d4}y4|jt|d5d6|d5d7��|}	dj|	�}Wn tk
�rPdjd*�}YnX|j|j|��y4|jt|d5d8|d5d7��|}	d"j|	�}Wn tk
�r�d"jd*�}YnX|j|j|��|j�r�d9}
d:}nd;}
d<}y8|jt|d=d>|d=d7|
��|}	dj|	�}Wn tk
�r:djd*�}YnX|j|j|��y8|jt|d=d?|d=d7|
��|}	d"j|	�}Wn tk
�r�d"jd*�}YnX|j|j|��|d'dk	�r�djd@j|d'��}n
djd*�}|j|j|dAdB���qVW|S)Cz2Return the dict to display in the curse interface.r0rz{}Z
CONTAINERSZTITLEz {}z
 sorted by {}Nr<Z
max_name_size�)�defaultcSst|d�S)Nr)rA)�xr*r*r+�<lambda>0sz"Plugin.msg_curse.<locals>.<lambda>)rJrz {:{width}}�Name)�widthZSORTr~z{:>10}rKrVz{:>6}zCPU%rz{:>7}ZMEMrz/{:<7}�MAXzIOR/sz {:<7}zIOW/szRx/szTx/sz {:8}rL)r9�	max_width�
�_z{:>6.1f}rrOr)�itemrJZoptionrrPr{rl�BrrRrmrS����brrTrU� T)Z
splittable)r1rAZis_disabledr4rdZcurse_add_line�sort_for_humanr%Zcurse_new_line�minrZ
get_int_value�max�	_msg_name�container_alertr2Z	get_viewsZ	auto_unit�intZbyte�join)r(rr�r7�msgZname_max_widthr9�statusZunitrqZto_bitr*r*r+�	msg_curses�,
 "


"
"







""&&  
zPlugin.msg_cursecCs|dd|�}dj||d�S)zBuild the container name.rNz {:{width}})r�)r4)r(r9r�rr*r*r+r��szPlugin._msg_namecCs,|dkrdS|dkrdS|dkr$dSdSdS)	zAnalyse the container status.rMZOKZexitedZWARNINGZdeadZCRITICALZCAREFULNr*)r(r�r*r*r+r��szPlugin.container_alert)NN)NN)�__name__�
__module__�__qualname__�__doc__rr-r/r;r rCrZ_check_decoratorZ_log_result_decoratorr&r^r_rar`ryrzr�r�r�r��
__classcell__r*r*)r)r+rHs*&

)/0"
	rcsTeZdZdZ�fdd�Zdd�Zedd��Zejdd��Zdd
d�Z	dd
�Z
�ZS)rZzD
    Specific thread to grab docker stats.

    stats is a dict
    csJtt|�j�tj�|_||_|jdd�|_i|_	t
jdj|jj
��dS)zLInit the class.

        container: instance of Docker-py Container
        T)�decodez.docker plugin - Create thread for container {}N)rrZr�	threadingZEvent�_stopper�
_containerr1�
_stats_stream�_statsrr3r4r)r(r9)r)r*r+r�s
zThreadDockerGrabber.__init__cCsny.x(|jD]}||_tjd�|j�r
Pq
WWn:tk
rh}ztjdj|��|j	�WYdd}~XnXdS)z_Grab the stats.

        Infinite loop, should be stopped by calling the stop() method
        g�������?z0docker plugin - Exception thrown during run ({})N)
r�r��timeZsleep�stoppedr=rr3r4r,)r(r:r8r*r*r+�run�s

zThreadDockerGrabber.runcCs|jS)z
Stats getter.)r�)r(r*r*r+r1�szThreadDockerGrabber.statscCs
||_dS)z
Stats setter.N)r�)r(rqr*r*r+r1�sNcCs"tjdj|jj��|jj�dS)zStop the thread.z-docker plugin - Close thread for container {}N)rr3r4r�rr�r')r(Ztimeoutr*r*r+r,�szThreadDockerGrabber.stopcCs
|jj�S)z%Return True is the thread is stopped.)r�Zis_set)r(r*r*r+r��szThreadDockerGrabber.stopped)N)r�r�r�r�rr��propertyr1�setterr,r�r�r*r*)r)r+rZ�s
rZcCsJtj}d}|dkrd}d}n|d
kr(d}t|d||tjdkd	�||fS)NrrWr�usernamerrr0r)Z	sorted_byZsorted_by_secondary�reverse)r�rr)r
r%�sort_stats_processes)r1Zsort_byZsort_by_secondaryr*r*r+re�sre)%r�rvr�r��copyrZglances.compatrrrrZglances.loggerrZglances.plugins.glances_pluginrZglances.processesr	r�r
Z
glances.timerrr<Zdateutilrr
r=r8rXZwarningr4rr5r�rZThreadrZrer*r*r*r+�<module>
s>"k9