<s> """<STR_LIT>""" <EOL> import urllib2 <EOL> import base64 <EOL> from xml . dom . minidom import parseString <EOL> import diamond . collector <EOL> class MonitCollector ( diamond . collector . Collector ) : <EOL> def get_default_config_help ( self ) : <EOL> config_help = super ( MonitCollector , self ) . get_default_config_help ( ) <EOL> config_help . update ( { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ) <EOL> return config_help <EOL> def get_default_config ( self ) : <EOL> """<STR_LIT>""" <EOL> config = super ( MonitCollector , self ) . get_default_config ( ) <EOL> config . update ( { <EOL> '<STR_LIT:host>' : '<STR_LIT:127.0.0.1>' , <EOL> '<STR_LIT:port>' : <NUM_LIT> , <EOL> '<STR_LIT:user>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:path>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : False , <EOL> } ) <EOL> return config <EOL> def collect ( self ) : <EOL> url = '<STR_LIT>' % ( self . config [ '<STR_LIT:host>' ] , <EOL> int ( self . config [ '<STR_LIT:port>' ] ) ) <EOL> try : <EOL> request = urllib2 . Request ( url ) <EOL> base64string = base64 . encodestring ( '<STR_LIT>' % ( <EOL> self . config [ '<STR_LIT:user>' ] , self . config [ '<STR_LIT>' ] ) ) . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> request . add_header ( "<STR_LIT>" , "<STR_LIT>" % base64string ) <EOL> response = urllib2 . urlopen ( request ) <EOL> except urllib2 . HTTPError , err : <EOL> self . log . error ( "<STR_LIT>" , err , url ) <EOL> return <EOL> metrics = { } <EOL> try : <EOL> dom = parseString ( "<STR_LIT>" . join ( response . readlines ( ) ) ) <EOL> except : <EOL> self . log . error ( "<STR_LIT>" ) <EOL> return <EOL> for service in dom . getElementsByTagName ( '<STR_LIT>' ) : <EOL> if int ( service . getAttribute ( '<STR_LIT:type>' ) ) == <NUM_LIT:3> : <EOL> name = service . getElementsByTagName ( '<STR_LIT:name>' ) [ <NUM_LIT:0> ] . firstChild . data <EOL> if ( service . getElementsByTagName ( <EOL> '<STR_LIT:status>' ) [ <NUM_LIT:0> ] . firstChild . data == '<STR_LIT:0>' <EOL> and service . getElementsByTagName ( <EOL> '<STR_LIT>' ) [ <NUM_LIT:0> ] . firstChild . data == '<STR_LIT:1>' ) : <EOL> try : <EOL> uptime = service . getElementsByTagName ( <EOL> '<STR_LIT>' ) [ <NUM_LIT:0> ] . firstChild . data <EOL> metrics [ "<STR_LIT>" % name ] = uptime <EOL> cpu = service . getElementsByTagName ( <EOL> '<STR_LIT>' ) [ <NUM_LIT:0> ] . getElementsByTagName ( <EOL> '<STR_LIT>' ) [ <NUM_LIT:0> ] . firstChild . data <EOL> metrics [ "<STR_LIT>" % name ] = cpu <EOL> if self . config [ '<STR_LIT>' ] : <EOL> cpu_total = service . getElementsByTagName ( <EOL> '<STR_LIT>' ) [ <NUM_LIT:0> ] . getElementsByTagName ( <EOL> '<STR_LIT>' ) [ <NUM_LIT:0> ] . firstChild . data <EOL> metrics [ "<STR_LIT>" % name ] = cpu_total <EOL> mem = int ( service . getElementsByTagName ( <EOL> '<STR_LIT>' ) [ <NUM_LIT:0> ] . getElementsByTagName ( <EOL> '<STR_LIT>' ) [ <NUM_LIT:0> ] . firstChild . data ) <EOL> for unit in self . config [ '<STR_LIT>' ] : <EOL> metrics [ "<STR_LIT>" % ( name , unit ) ] = ( <EOL> diamond . convertor . binary . convert ( <EOL> value = mem , <EOL> oldUnit = '<STR_LIT>' , <EOL> newUnit = unit ) ) <EOL> metrics [ "<STR_LIT>" % name ] = uptime <EOL> if self . config [ '<STR_LIT>' ] : <EOL> mem_total = int ( service . getElementsByTagName ( <EOL> '<STR_LIT>' ) [ <NUM_LIT:0> ] . getElementsByTagName ( <EOL> '<STR_LIT>' ) [ <NUM_LIT:0> ] . firstChild . data ) <EOL> for unit in self . config [ '<STR_LIT>' ] : <EOL> metrics [ "<STR_LIT>" % ( <EOL> name , unit ) ] = ( <EOL> diamond . convertor . binary . convert ( <EOL> value = mem , <EOL> oldUnit = '<STR_LIT>' , <EOL> newUnit = unit ) ) <EOL> except : <EOL> pass <EOL> for key in metrics : <EOL> self . publish ( key , metrics [ key ] ) </s>
