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: //usr/lib/python3.6/site-packages/glances/exports/__pycache__/glances_mqtt.cpython-36.pyc
3

Eo�cw�@sldZddlZddlZddlZddlmZddlmZddlm	Z	ddl
mZddlj
jZGdd�de�ZdS)	zMQTT interface class.�N)�logger)�
GlancesExport)�
json_dumps)�certscs2eZdZdZd	�fdd�	Zdd�Zdd�Z�ZS)
�Exportz*This class manages the MQTT export module.Ncs�tt|�j||d�d|_d|_d|_d|_|jdddgddd	d
dgd�|_|jsZt	d
�t
j�|_t
|j�ppd|_|jp|d|_|jp�d|_|jo�|jj�dk|_|jp�dj�|_|jdkr�tjd�tj	d�|j�|_|js�t	d�dS)zInit the MQTT export IF.)�config�argsN�true�mqtt�host�password�port�user�topic�tls�topic_structure)ZmandatoriesZoptionszMissing MQTT configi�"Zglances�
per-metric�
per-pluginz<topic_structure must be either 'per-metric' or 'per-plugin'.�z!MQTT client initialization failed)rr)�superr�__init__rrrrZ	load_conf�
export_enable�exit�socketZgethostname�hostname�intr
�lowerrr�critical�sys�init�client)�selfrr)�	__class__��"/usr/lib/python3.6/glances_mqtt.pyrs*




zExport.__init__cCs�|js
dSyXtjd|jdd�}|j|j|jd�|jrF|jt	j
��|j|j|j
d�|j�|Stk
r�}ztjd|j|j
|f�dSd}~XnXdS)z'Init the connection to the MQTT server.NZglances_F)Z	client_idZ
clean_session)Zusernamer)rr
z6Connection to MQTT server %s:%s failed with error: %s )r�pahoZClientrZusername_pw_setrrrZtls_setr�whereZconnectrr
Z
loop_start�	Exceptionrr)r!r �er#r#r$r?szExport.initcs�dtjtj}d}||fdd��|jdkr�x�t||�D]�\}}yL�fdd�|jd�D�}|j|j|g}|j|�d	j	|�}	|j
j|	|�Wq8tk
r�}
zt
jd
|
�WYdd}
~
Xq8Xq8Wn�|jdk�r�y�d	j	|j|j|g�}	tt||��}t�}xx|D]p}
|
jd�}|}xBtt|�d
�D].}|||k�rDt�|||<|||}�q$W||
||t|�d
<�qWt|�}|j
j|	|�Wn2tk
�r�}
zt
jd
|
�WYdd}
~
XnXdS)zWrite the points in MQTT.z_-�_csdj��fdd�|D��S)N�c3s|]}|�kr|n�VqdS)Nr#)�.0�c)�
substitute�	whitelistr#r$�	<genexpr>Vsz5Export.export.<locals>.whitelisted.<locals>.<genexpr>)�join)�sr.r-r#)r-r.r$�whitelistedUsz"Export.export.<locals>.whitelistedz
per-metriccsg|]}�|��qSr#r#)r+�name)r2r#r$�
<listcomp>[sz!Export.export.<locals>.<listcomp>�.�/z(Can not export stats to MQTT server (%s)Nz
per-plugin�)�stringZ
ascii_lettersZdigitsr�zip�splitrr�extendr0r Zpublishr'r�error�dict�range�lenr)r!r3�columnsZpointsZ	WHITELISTZ
SUBSTITUTEZsensor�valueZ	to_exportrr(Z
sensor_valuesZoutput_value�keyZ	split_keyZ
current_level�depthZ
json_valuer#)r2r$�exportOs<


&

z
Export.export)NN)�__name__�
__module__�__qualname__�__doc__rrrD�
__classcell__r#r#)r"r$rs"r)rHrr8rZglances.loggerrZglances.exports.glances_exportrZglances.globalsrZrequestsrZpaho.mqtt.clientr
r r%rr#r#r#r$�<module>
s