<s> """<STR_LIT>""" <EOL> import re <EOL> import httplib <EOL> import urlparse <EOL> import diamond . collector <EOL> class HttpdCollector ( diamond . collector . Collector ) : <EOL> def process_config ( self ) : <EOL> super ( HttpdCollector , self ) . process_config ( ) <EOL> if '<STR_LIT:url>' in self . config : <EOL> self . config [ '<STR_LIT>' ] . append ( self . config [ '<STR_LIT:url>' ] ) <EOL> self . urls = { } <EOL> if isinstance ( self . config [ '<STR_LIT>' ] , basestring ) : <EOL> self . config [ '<STR_LIT>' ] = self . config [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002C>' ) <EOL> for url in self . config [ '<STR_LIT>' ] : <EOL> if len ( url ) == <NUM_LIT:0> : <EOL> continue <EOL> if '<STR_LIT:U+0020>' in url : <EOL> parts = url . split ( '<STR_LIT:U+0020>' ) <EOL> self . urls [ parts [ <NUM_LIT:0> ] ] = parts [ <NUM_LIT:1> ] <EOL> else : <EOL> self . urls [ '<STR_LIT>' ] = url <EOL> def get_default_config_help ( self ) : <EOL> config_help = super ( HttpdCollector , self ) . get_default_config_help ( ) <EOL> config_help . update ( { <EOL> '<STR_LIT>' : "<STR_LIT>" <EOL> + "<STR_LIT>" <EOL> + "<STR_LIT>" , <EOL> } ) <EOL> return config_help <EOL> def get_default_config ( self ) : <EOL> """<STR_LIT>""" <EOL> config = super ( HttpdCollector , self ) . get_default_config ( ) <EOL> config . update ( { <EOL> '<STR_LIT:path>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] <EOL> } ) <EOL> return config <EOL> def collect ( self ) : <EOL> for nickname in self . urls . keys ( ) : <EOL> url = self . urls [ nickname ] <EOL> try : <EOL> while True : <EOL> parts = urlparse . urlparse ( url ) <EOL> endpoint = parts [ <NUM_LIT:1> ] . split ( '<STR_LIT::>' ) <EOL> if len ( endpoint ) > <NUM_LIT:1> : <EOL> service_host = endpoint [ <NUM_LIT:0> ] <EOL> service_port = int ( endpoint [ <NUM_LIT:1> ] ) <EOL> else : <EOL> service_host = endpoint [ <NUM_LIT:0> ] <EOL> service_port = <NUM_LIT> <EOL> connection = httplib . HTTPConnection ( service_host , <EOL> service_port ) <EOL> url = "<STR_LIT>" % ( parts [ <NUM_LIT:2> ] , parts [ <NUM_LIT:4> ] ) <EOL> connection . request ( "<STR_LIT:GET>" , url ) <EOL> response = connection . getresponse ( ) <EOL> data = response . read ( ) <EOL> headers = dict ( response . getheaders ( ) ) <EOL> if ( '<STR_LIT:location>' not in headers <EOL> or headers [ '<STR_LIT:location>' ] == url ) : <EOL> connection . close ( ) <EOL> break <EOL> url = headers [ '<STR_LIT:location>' ] <EOL> connection . close ( ) <EOL> except Exception , e : <EOL> self . log . error ( <EOL> "<STR_LIT>" , <EOL> service_host , str ( service_port ) , url , e ) <EOL> continue <EOL> exp = re . compile ( '<STR_LIT>' ) <EOL> for line in data . split ( '<STR_LIT:\n>' ) : <EOL> if line : <EOL> m = exp . match ( line ) <EOL> if m : <EOL> k = m . group ( <NUM_LIT:1> ) <EOL> v = m . group ( <NUM_LIT:2> ) <EOL> if k == '<STR_LIT>' : <EOL> continue <EOL> if k == '<STR_LIT>' : <EOL> for sb_kv in self . _parseScoreboard ( v ) : <EOL> self . _publish ( nickname , sb_kv [ <NUM_LIT:0> ] , sb_kv [ <NUM_LIT:1> ] ) <EOL> else : <EOL> self . _publish ( nickname , k , v ) <EOL> def _publish ( self , nickname , key , value ) : <EOL> metrics = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> metrics_precision = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if key in metrics : <EOL> presicion_metric = False <EOL> metric_name = "<STR_LIT:%s>" % re . sub ( '<STR_LIT>' , '<STR_LIT>' , key ) <EOL> if metric_name in metrics_precision : <EOL> presicion_metric = <NUM_LIT:1> <EOL> if len ( nickname ) > <NUM_LIT:0> : <EOL> metric_name = nickname + '<STR_LIT:.>' + metric_name <EOL> if presicion_metric : <EOL> metric_value = "<STR_LIT>" % float ( value ) <EOL> self . publish ( metric_name , metric_value , precision = <NUM_LIT:5> ) <EOL> else : <EOL> metric_value = "<STR_LIT>" % float ( value ) <EOL> self . publish ( metric_name , metric_value ) <EOL> def _parseScoreboard ( self , sb ) : <EOL> ret = [ ] <EOL> ret . append ( ( '<STR_LIT>' , sb . count ( '<STR_LIT:_>' ) ) ) <EOL> ret . append ( ( '<STR_LIT>' , sb . count ( '<STR_LIT:R>' ) ) ) <EOL> ret . append ( ( '<STR_LIT>' , sb . count ( '<STR_LIT>' ) ) ) <EOL> ret . append ( ( '<STR_LIT>' , sb . count ( '<STR_LIT>' ) ) ) <EOL> ret . append ( ( '<STR_LIT>' , sb . count ( '<STR_LIT:D>' ) ) ) <EOL> ret . append ( ( '<STR_LIT>' , sb . count ( '<STR_LIT:C>' ) ) ) <EOL> ret . append ( ( '<STR_LIT>' , sb . count ( '<STR_LIT:L>' ) ) ) <EOL> ret . append ( ( '<STR_LIT>' , sb . count ( '<STR_LIT>' ) ) ) <EOL> ret . append ( ( '<STR_LIT>' , sb . count ( '<STR_LIT:I>' ) ) ) <EOL> return ret </s>
