o
    va                     @   sX   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
Z
G dd de	ZdS )	zOpenTSDB interface class.    N)Number)range)logger)GlancesExportc                       s>   e Zd ZdZd fdd	Zdd Zdd Z fd	d
Z  ZS )Exportz.This class manages the OpenTSDB export module.Nc                    sh   t t| j||d d| _d| _| jdddgddgd| _| js%td	 | jdu r-d
| _| 	 | _
dS )zInit the OpenTSDB export IF.)configargsNZopentsdbhostportprefixtags)Zmandatoriesoptions   Zglances)superr   __init__r   r   Z	load_confexport_enablesysexitinitclient)selfr   r   	__class__ B/usr/lib/python3/dist-packages/glances/exports/glances_opentsdb.pyr   $   s   

zExport.__init__c              
   C   sr   | j sdS ztj| jt| jdd}W |S  ty8 } ztd| j| j|f  t	
d W Y d}~|S d}~ww )z+Init the connection to the OpenTSDB server.NT)r
   Z
check_hostz,Cannot connect to OpenTSDB server %s:%s (%s)r   )r   potsdbZClientr	   intr
   	Exceptionr   Zcriticalr   r   )r   Zdber   r   r   r   =   s   zExport.initc           	      C   s   t t|D ]F}t|| tsqd| j||| }|| }| | j}z| jj	||fi | W q t
yL } ztd||f  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 %s to OpenTSDB (%s)NzExport {} stats to OpenTSDB)r   len
isinstancer   formatr   Z
parse_tagsr   r   sendr   r   errordebug)	r   namecolumnsZpointsiZ	stat_nameZ
stat_valuer   r   r   r   r   exportL   s   zExport.exportc                    s   | j   tt|   dS )z!Close the OpenTSDB export module.N)r   waitr   r   r   )r   r   r   r   r   Z   s   
zExport.exit)NN)	__name__
__module____qualname____doc__r   r   r(   r   __classcell__r   r   r   r   r       s    r   )r-   r   Znumbersr   Zglances.compatr   Zglances.loggerr   Zglances.exports.glances_exportr   r   r   r   r   r   r   <module>   s   