<s> """<STR_LIT>""" <EOL> from Handler import Handler <EOL> import socket <EOL> class TSDBHandler ( Handler ) : <EOL> """<STR_LIT>""" <EOL> RETRY = <NUM_LIT:3> <EOL> def __init__ ( self , config = None ) : <EOL> """<STR_LIT>""" <EOL> Handler . __init__ ( self , config ) <EOL> self . socket = None <EOL> self . host = self . config [ '<STR_LIT:host>' ] <EOL> self . port = int ( self . config [ '<STR_LIT:port>' ] ) <EOL> self . timeout = int ( self . config [ '<STR_LIT>' ] ) <EOL> self . metric_format = str ( self . config [ '<STR_LIT>' ] ) <EOL> self . tags = str ( self . config [ '<STR_LIT>' ] ) <EOL> self . _connect ( ) <EOL> def get_default_config_help ( self ) : <EOL> """<STR_LIT>""" <EOL> config = super ( TSDBHandler , self ) . get_default_config_help ( ) <EOL> config . update ( { <EOL> '<STR_LIT:host>' : '<STR_LIT>' , <EOL> '<STR_LIT:port>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ) <EOL> return config <EOL> def get_default_config ( self ) : <EOL> """<STR_LIT>""" <EOL> config = super ( TSDBHandler , self ) . get_default_config ( ) <EOL> config . update ( { <EOL> '<STR_LIT:host>' : '<STR_LIT>' , <EOL> '<STR_LIT:port>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT:5> , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ) <EOL> return config <EOL> def __del__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _close ( ) <EOL> def process ( self , metric ) : <EOL> """<STR_LIT>""" <EOL> metric_str = self . metric_format . format ( <EOL> Collector = metric . getCollectorPath ( ) , <EOL> Path = metric . path , <EOL> Metric = metric . getMetricPath ( ) , <EOL> host = metric . host , <EOL> timestamp = metric . timestamp , <EOL> value = metric . value , <EOL> tags = self . tags <EOL> ) <EOL> self . _send ( "<STR_LIT>" + str ( metric_str ) + "<STR_LIT:\n>" ) <EOL> def _send ( self , data ) : <EOL> """<STR_LIT>""" <EOL> retry = self . RETRY <EOL> while retry > <NUM_LIT:0> : <EOL> if not self . socket : <EOL> self . log . error ( "<STR_LIT>" ) <EOL> self . _connect ( ) <EOL> retry -= <NUM_LIT:1> <EOL> continue <EOL> try : <EOL> self . socket . sendall ( data ) <EOL> break <EOL> except socket . error , e : <EOL> self . log . error ( "<STR_LIT>" , e ) <EOL> self . _close ( ) <EOL> retry -= <NUM_LIT:1> <EOL> continue <EOL> def _connect ( self ) : <EOL> """<STR_LIT>""" <EOL> self . socket = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> if socket is None : <EOL> self . log . error ( "<STR_LIT>" ) <EOL> self . _close ( ) <EOL> return <EOL> self . socket . settimeout ( self . timeout ) <EOL> try : <EOL> self . socket . connect ( ( self . host , self . port ) ) <EOL> self . log . debug ( "<STR_LIT>" , <EOL> self . host , self . port ) <EOL> except Exception , ex : <EOL> self . log . error ( "<STR_LIT>" , <EOL> self . host , self . port , ex ) <EOL> self . _close ( ) <EOL> return <EOL> def _close ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . socket is not None : <EOL> self . socket . close ( ) <EOL> self . socket = None </s>
