<s> """<STR_LIT>""" <EOL> import diamond . collector <EOL> import socket <EOL> import re <EOL> from diamond . collector import str_to_bool <EOL> class DarnerCollector ( diamond . collector . Collector ) : <EOL> GAUGES = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> def get_default_config_help ( self ) : <EOL> config_help = super ( DarnerCollector , self ) . get_default_config_help ( ) <EOL> config_help . update ( { <EOL> '<STR_LIT>' : "<STR_LIT>" <EOL> + "<STR_LIT>" <EOL> + "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" <EOL> + "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> } ) <EOL> return config_help <EOL> def get_default_config ( self ) : <EOL> """<STR_LIT>""" <EOL> config = super ( DarnerCollector , self ) . get_default_config ( ) <EOL> config . update ( { <EOL> '<STR_LIT:path>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] <EOL> } ) <EOL> return config <EOL> def get_raw_stats ( self , host , port ) : <EOL> data = '<STR_LIT>' <EOL> try : <EOL> if port is None : <EOL> sock = socket . socket ( socket . AF_UNIX , socket . SOCK_STREAM ) <EOL> sock . connect ( host ) <EOL> else : <EOL> sock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> sock . connect ( ( host , int ( port ) ) ) <EOL> sock . send ( '<STR_LIT>' ) <EOL> data = sock . recv ( <NUM_LIT> ) <EOL> except socket . error : <EOL> self . log . exception ( '<STR_LIT>' , <EOL> host , port ) <EOL> return data <EOL> def get_stats ( self , host , port ) : <EOL> ignored = ( '<STR_LIT:time>' , '<STR_LIT:version>' ) <EOL> stats = { } <EOL> queues = { } <EOL> data = self . get_raw_stats ( host , port ) <EOL> for line in data . splitlines ( ) : <EOL> pieces = line . split ( '<STR_LIT:U+0020>' ) <EOL> if pieces [ <NUM_LIT:0> ] != '<STR_LIT>' or pieces [ <NUM_LIT:1> ] in ignored : <EOL> continue <EOL> if re . match ( r'<STR_LIT>' , pieces [ <NUM_LIT:1> ] ) : <EOL> queue_match = re . match ( <EOL> r'<STR_LIT>' , <EOL> pieces [ <NUM_LIT:1> ] ) <EOL> queue_name = queue_match . group ( <NUM_LIT:1> ) . replace ( '<STR_LIT:.>' , '<STR_LIT:_>' ) <EOL> if queue_name not in queues : <EOL> queues [ queue_name ] = { } <EOL> queues [ queue_name ] [ queue_match . group ( <NUM_LIT:2> ) ] = int ( pieces [ <NUM_LIT:2> ] ) <EOL> else : <EOL> stats [ pieces [ <NUM_LIT:1> ] ] = int ( pieces [ <NUM_LIT:2> ] ) <EOL> return stats , queues <EOL> def collect ( self ) : <EOL> hosts = self . config . get ( '<STR_LIT>' ) <EOL> if isinstance ( hosts , basestring ) : <EOL> hosts = [ hosts ] <EOL> for host in hosts : <EOL> matches = re . search ( '<STR_LIT>' , host ) <EOL> alias = matches . group ( <NUM_LIT:2> ) <EOL> hostname = matches . group ( <NUM_LIT:3> ) <EOL> port = matches . group ( <NUM_LIT:5> ) <EOL> if alias is None : <EOL> alias = hostname <EOL> stats , queues = self . get_stats ( hostname , port ) <EOL> if str_to_bool ( self . config [ '<STR_LIT>' ] ) : <EOL> for queue in queues : <EOL> for queue_stat in queues [ queue ] : <EOL> self . publish_gauge ( <EOL> alias + "<STR_LIT>" + queue + "<STR_LIT:.>" + queue_stat , <EOL> queues [ queue ] [ queue_stat ] ) <EOL> desired = self . config . get ( '<STR_LIT>' , stats . keys ( ) ) <EOL> for stat in desired : <EOL> if stat in stats : <EOL> if stat in self . GAUGES : <EOL> self . publish_gauge ( alias + "<STR_LIT:.>" + stat , stats [ stat ] ) <EOL> else : <EOL> self . publish_counter ( alias + "<STR_LIT:.>" + stat , stats [ stat ] ) <EOL> else : <EOL> self . log . error ( "<STR_LIT>" <EOL> "<STR_LIT>" , stat ) </s>
