o
    vaF                     @   sd   d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ G dd	 d	e	Zd
d ZdS )zStatsd interface class.    N)Number)range)logger)GlancesExport)StatsClientc                       s2   e Zd ZdZd	 fdd	Zdd Zdd Z  ZS )
Exportz,This class manages the Statsd export module.Nc                    s`   t t| j||d d| _| jdddgdgd| _| js!td | jdu r)d	| _|  | _	dS )
zInit the Statsd export IF.)configargsNstatsdhostportprefix)Zmandatoriesoptions   Zglances)
superr   __init__r   Z	load_confexport_enablesysexitinitclient)selfr   r	   	__class__ @/usr/lib/python3/dist-packages/glances/exports/glances_statsd.pyr   $   s   

zExport.__init__c                 C   s8   | j sdS td| j| j t| jt| j| jdS )z)Init the connection to the Statsd server.Nz.Stats will be exported to StatsD server: {}:{})r   )	r   r   infoformatr   r   r   intr   )r   r   r   r   r   <   s   zExport.initc                 C   s   t t|D ]:}t|| tsqd||| }|| }z| jt|| W q ty@ } zt	
d|  W Y d}~qd}~ww t	d| dS )z&Export the stats to the Statsd server.z{}.{}z#Can not export stats to Statsd (%s)NzExport {} stats to Statsd)r   len
isinstancer   r   r   Zgauge	normalize	Exceptionr   errordebug)r   namecolumnsZpointsiZ	stat_nameZ
stat_valueer   r   r   exportG   s   zExport.export)NN)__name__
__module____qualname____doc__r   r   r)   __classcell__r   r   r   r   r       s
    r   c                 C   s(   |  dd}| dd}| dd}|S )z(Normalize name for the Statsd convention: % _)replace)r%   retr   r   r   r!   V   s   r!   )r-   r   Znumbersr   Zglances.compatr   Zglances.loggerr   Zglances.exports.glances_exportr   r
   r   r   r!   r   r   r   r   <module>   s   6