<s> import httplib <EOL> import urllib2 <EOL> import socket <EOL> import urlparse <EOL> import base64 <EOL> import re <EOL> from scalyr_agent import ScalyrMonitor , UnsupportedSystem , define_config_option , define_metric , define_log_field <EOL> httpSourceAddress = "<STR_LIT:127.0.0.1>" <EOL> __monitor__ = __name__ <EOL> define_config_option ( __monitor__ , '<STR_LIT>' , <EOL> '<STR_LIT>' , required_option = True ) <EOL> define_config_option ( __monitor__ , '<STR_LIT:id>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> convert_to = str ) <EOL> define_config_option ( __monitor__ , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> convert_to = str , <EOL> required_option = True ) <EOL> define_config_option ( __monitor__ , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> convert_to = str ) <EOL> define_config_option ( __monitor__ , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> convert_to = str ) <EOL> define_config_option ( __monitor__ , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % httpSourceAddress , default = httpSourceAddress ) <EOL> define_metric ( __monitor__ , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> , extra_fields = { '<STR_LIT:type>' : '<STR_LIT>' } , cumulative = False , category = '<STR_LIT>' ) <EOL> define_metric ( __monitor__ , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> , extra_fields = { '<STR_LIT:type>' : '<STR_LIT>' } , cumulative = False , category = '<STR_LIT>' ) <EOL> define_metric ( __monitor__ , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> , extra_fields = { '<STR_LIT:type>' : '<STR_LIT>' } , cumulative = False , category = '<STR_LIT>' ) <EOL> define_metric ( __monitor__ , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> , extra_fields = { '<STR_LIT:type>' : '<STR_LIT>' } , cumulative = False , category = '<STR_LIT>' ) <EOL> define_metric ( __monitor__ , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> , extra_fields = { '<STR_LIT:type>' : '<STR_LIT>' } , cumulative = False , category = '<STR_LIT>' ) <EOL> define_metric ( __monitor__ , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> , extra_fields = { '<STR_LIT:type>' : '<STR_LIT>' } , cumulative = False , category = '<STR_LIT>' ) <EOL> define_metric ( __monitor__ , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> , cumulative = False , category = '<STR_LIT>' ) <EOL> define_metric ( __monitor__ , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> , cumulative = False , category = '<STR_LIT>' ) <EOL> define_metric ( __monitor__ , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> , cumulative = True , category = '<STR_LIT>' ) <EOL> define_metric ( __monitor__ , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> , cumulative = True , category = '<STR_LIT>' ) <EOL> define_metric ( __monitor__ , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> , extra_fields = { '<STR_LIT:type>' : '<STR_LIT>' } , cumulative = True , category = '<STR_LIT>' ) <EOL> define_metric ( __monitor__ , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> , extra_fields = { '<STR_LIT:type>' : '<STR_LIT>' } , cumulative = True , category = '<STR_LIT>' ) <EOL> memory_pools = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> for i in memory_pools . keys ( ) : <EOL> define_metric ( __monitor__ , '<STR_LIT>' , <EOL> '<STR_LIT>' % memory_pools [ i ] <EOL> , extra_fields = { '<STR_LIT>' : i } , cumulative = False , category = '<STR_LIT>' ) <EOL> define_metric ( __monitor__ , '<STR_LIT>' , <EOL> '<STR_LIT>' % memory_pools [ i ] <EOL> , extra_fields = { '<STR_LIT>' : i } , cumulative = False , category = '<STR_LIT>' ) <EOL> define_metric ( __monitor__ , '<STR_LIT>' , <EOL> '<STR_LIT>' % memory_pools [ i ] <EOL> , extra_fields = { '<STR_LIT>' : i } , cumulative = False , category = '<STR_LIT>' ) <EOL> define_metric ( __monitor__ , '<STR_LIT>' , <EOL> '<STR_LIT>' % memory_pools [ i ] <EOL> , extra_fields = { '<STR_LIT>' : i } , cumulative = False , category = '<STR_LIT>' ) <EOL> define_log_field ( __monitor__ , '<STR_LIT>' , '<STR_LIT>' ) <EOL> define_log_field ( __monitor__ , '<STR_LIT>' , '<STR_LIT>' ) <EOL> define_log_field ( __monitor__ , '<STR_LIT>' , '<STR_LIT>' ) <EOL> define_log_field ( __monitor__ , '<STR_LIT:value>' , '<STR_LIT>' ) <EOL> class BindableHTTPConnection ( httplib . HTTPConnection ) : <EOL> def connect ( self ) : <EOL> """<STR_LIT>""" <EOL> self . sock = socket . socket ( ) <EOL> self . sock . bind ( ( self . source_ip , <NUM_LIT:0> ) ) <EOL> if isinstance ( self . timeout , float ) : <EOL> self . sock . settimeout ( self . timeout ) <EOL> self . sock . connect ( ( self . host , self . port ) ) <EOL> def BindableHTTPConnectionFactory ( source_ip ) : <EOL> def _get ( host , port = None , strict = None , timeout = <NUM_LIT:0> ) : <EOL> bhc = BindableHTTPConnection ( <EOL> host , <EOL> port = port , <EOL> strict = strict , <EOL> timeout = timeout ) <EOL> bhc . source_ip = source_ip <EOL> return bhc <EOL> return _get <EOL> class BindableHTTPHandler ( urllib2 . HTTPHandler ) : <EOL> def http_open ( self , req ) : <EOL> return self . do_open ( <EOL> BindableHTTPConnectionFactory ( httpSourceAddress ) , req ) <EOL> def _convert_to_megabytes ( value ) : <EOL> parts = value . split ( "<STR_LIT:U+0020>" ) <EOL> if len ( parts ) < <NUM_LIT:2> : <EOL> return None <EOL> scale_multiplier = { <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT:1.0> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> <EOL> } <EOL> if parts [ <NUM_LIT:1> ] . lower ( ) not in scale_multiplier . keys ( ) : <EOL> return None <EOL> multiplier = scale_multiplier [ parts [ <NUM_LIT:1> ] . lower ( ) ] <EOL> try : <EOL> if '<STR_LIT:.>' in parts [ <NUM_LIT:0> ] : <EOL> val = float ( parts [ <NUM_LIT:0> ] ) <EOL> else : <EOL> val = float ( int ( parts [ <NUM_LIT:0> ] ) ) <EOL> except Exception as e : <EOL> return None <EOL> return val * multiplier <EOL> def _convert_to_milliseconds ( value ) : <EOL> parts = value . split ( "<STR_LIT:U+0020>" ) <EOL> if len ( parts ) < <NUM_LIT:2> : <EOL> return None <EOL> scale_multiplier = { <EOL> "<STR_LIT:s>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT:1.0> , <EOL> } <EOL> if parts [ <NUM_LIT:1> ] . lower ( ) not in scale_multiplier . keys ( ) : <EOL> return None <EOL> multiplier = scale_multiplier [ parts [ <NUM_LIT:1> ] . lower ( ) ] <EOL> try : <EOL> if '<STR_LIT:.>' in parts [ <NUM_LIT:0> ] : <EOL> val = float ( parts [ <NUM_LIT:0> ] ) <EOL> else : <EOL> val = float ( int ( parts [ <NUM_LIT:0> ] ) ) <EOL> except Exception as e : <EOL> return None <EOL> return val * multiplier <EOL> class TomcatMonitor ( ScalyrMonitor ) : <EOL> """<STR_LIT>""" <EOL> def _initialize ( self ) : <EOL> """<STR_LIT>""" <EOL> global httpSourceAddress <EOL> self . _monitor_url = None <EOL> self . _monitor_user = None <EOL> self . _monitor_password = None <EOL> if "<STR_LIT>" in self . _config : <EOL> self . _monitor_url = self . _config [ "<STR_LIT>" ] <EOL> if "<STR_LIT>" in self . _config : <EOL> self . _monitor_user = self . _config [ "<STR_LIT>" ] <EOL> if "<STR_LIT>" in self . _config : <EOL> self . _monitor_password = self . _config [ "<STR_LIT>" ] <EOL> self . _sourceaddress = self . _config . get ( '<STR_LIT>' , <EOL> default = httpSourceAddress ) <EOL> httpSourceAddress = self . _sourceaddress <EOL> if "<STR_LIT>" not in self . _config : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> credentials_needed = False <EOL> if "<STR_LIT>" in self . _config and "<STR_LIT>" not in self . _config : <EOL> credentials_needed = True <EOL> elif "<STR_LIT>" not in self . _config and "<STR_LIT>" in self . _config : <EOL> credentials_needed = True <EOL> if credentials_needed : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def _get_status ( self , url ) : <EOL> data = None <EOL> try : <EOL> testurl = urlparse . urlparse ( url ) <EOL> except Exception as e : <EOL> print ( <EOL> "<STR_LIT>" % <EOL> url ) <EOL> return data <EOL> try : <EOL> request = urllib2 . Request ( self . _monitor_url ) <EOL> if self . _monitor_user != None : <EOL> b64cred = base64 . encodestring ( '<STR_LIT>' % ( self . _monitor_user , self . _monitor_password ) ) . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> request . add_header ( "<STR_LIT>" , "<STR_LIT>" % b64cred ) <EOL> opener = urllib2 . build_opener ( BindableHTTPHandler ) <EOL> handle = opener . open ( request ) <EOL> data = handle . read ( ) <EOL> except urllib2 . HTTPError as err : <EOL> message = "<STR_LIT>" , err . code <EOL> if err . code == <NUM_LIT> : <EOL> message = "<STR_LIT>" <EOL> elif err . code == <NUM_LIT> : <EOL> message = "<STR_LIT>" <EOL> elif err . code >= <NUM_LIT> or err . code < <NUM_LIT> : <EOL> message = "<STR_LIT>" , err . code <EOL> self . _logger . error ( message ) <EOL> data = None <EOL> except urllib2 . URLError as err : <EOL> message = "<STR_LIT>" , err <EOL> if err . reason . errno == <NUM_LIT> : <EOL> message = "<STR_LIT>" % url . netloc <EOL> self . _logger . error ( message ) <EOL> data = None <EOL> except Exception as e : <EOL> self . _logger . error ( <EOL> "<STR_LIT>" % <EOL> e ) <EOL> data = None <EOL> return data <EOL> def _parse_general_status ( self , status ) : <EOL> result = { } <EOL> start = status . find ( "<STR_LIT>" ) <EOL> if start > - <NUM_LIT:1> : <EOL> m = re . match ( r"<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , status [ start + <NUM_LIT:12> : ] ) <EOL> if m != None : <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , _convert_to_megabytes ( m . group ( <NUM_LIT:1> ) ) , '<STR_LIT:type>' , '<STR_LIT>' ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , _convert_to_megabytes ( m . group ( <NUM_LIT:2> ) ) , '<STR_LIT:type>' , '<STR_LIT>' ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , _convert_to_megabytes ( m . group ( <NUM_LIT:3> ) ) , '<STR_LIT:type>' , '<STR_LIT>' ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , int ( m . group ( <NUM_LIT:4> ) ) , '<STR_LIT:type>' , '<STR_LIT>' ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , int ( m . group ( <NUM_LIT:5> ) ) , '<STR_LIT:type>' , '<STR_LIT>' ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , int ( m . group ( <NUM_LIT:6> ) ) , '<STR_LIT:type>' , '<STR_LIT>' ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , _convert_to_milliseconds ( m . group ( <NUM_LIT:7> ) ) ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , _convert_to_milliseconds ( m . group ( <NUM_LIT:8> ) ) ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , int ( m . group ( <NUM_LIT:9> ) ) ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , int ( m . group ( <NUM_LIT:10> ) ) ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , _convert_to_megabytes ( m . group ( <NUM_LIT:11> ) ) , '<STR_LIT:type>' , '<STR_LIT>' ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , _convert_to_megabytes ( m . group ( <NUM_LIT:12> ) ) , '<STR_LIT:type>' , '<STR_LIT>' ] <EOL> return result <EOL> def _parse_heap_status ( self , status ) : <EOL> result = { } <EOL> start = status . find ( "<STR_LIT>" ) <EOL> if start > - <NUM_LIT:1> : <EOL> m = re . match ( r"<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> status [ start + <NUM_LIT:12> : ] ) <EOL> if m != None : <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , _convert_to_megabytes ( m . group ( <NUM_LIT:2> ) ) , '<STR_LIT>' , '<STR_LIT>' ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , _convert_to_megabytes ( m . group ( <NUM_LIT:3> ) ) , '<STR_LIT>' , '<STR_LIT>' ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , _convert_to_megabytes ( m . group ( <NUM_LIT:4> ) ) , '<STR_LIT>' , '<STR_LIT>' ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , _convert_to_megabytes ( m . group ( <NUM_LIT:5> ) ) , '<STR_LIT>' , '<STR_LIT>' ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , _convert_to_megabytes ( m . group ( <NUM_LIT:7> ) ) , '<STR_LIT>' , '<STR_LIT>' ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , _convert_to_megabytes ( m . group ( <NUM_LIT:8> ) ) , '<STR_LIT>' , '<STR_LIT>' ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , _convert_to_megabytes ( m . group ( <NUM_LIT:9> ) ) , '<STR_LIT>' , '<STR_LIT>' ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , _convert_to_megabytes ( m . group ( <NUM_LIT:10> ) ) , '<STR_LIT>' , '<STR_LIT>' ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , _convert_to_megabytes ( m . group ( <NUM_LIT:12> ) ) , '<STR_LIT>' , '<STR_LIT>' ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , _convert_to_megabytes ( m . group ( <NUM_LIT> ) ) , '<STR_LIT>' , '<STR_LIT>' ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , _convert_to_megabytes ( m . group ( <NUM_LIT> ) ) , '<STR_LIT>' , '<STR_LIT>' ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , _convert_to_megabytes ( m . group ( <NUM_LIT:15> ) ) , '<STR_LIT>' , '<STR_LIT>' ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , _convert_to_megabytes ( m . group ( <NUM_LIT> ) ) , '<STR_LIT>' , '<STR_LIT>' ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , _convert_to_megabytes ( m . group ( <NUM_LIT> ) ) , '<STR_LIT>' , '<STR_LIT>' ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , _convert_to_megabytes ( m . group ( <NUM_LIT> ) ) , '<STR_LIT>' , '<STR_LIT>' ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , _convert_to_megabytes ( m . group ( <NUM_LIT:20> ) ) , '<STR_LIT>' , '<STR_LIT>' ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , _convert_to_megabytes ( m . group ( <NUM_LIT> ) ) , '<STR_LIT>' , '<STR_LIT>' ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , _convert_to_megabytes ( m . group ( <NUM_LIT> ) ) , '<STR_LIT>' , '<STR_LIT>' ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , _convert_to_megabytes ( m . group ( <NUM_LIT> ) ) , '<STR_LIT>' , '<STR_LIT>' ] <EOL> result [ "<STR_LIT>" ] = [ '<STR_LIT>' , _convert_to_megabytes ( m . group ( <NUM_LIT> ) ) , '<STR_LIT>' , '<STR_LIT>' ] <EOL> return result <EOL> def gather_sample ( self ) : <EOL> """<STR_LIT>""" <EOL> def get_value_as_str ( value ) : <EOL> if type ( value ) is int : <EOL> return "<STR_LIT>" % value <EOL> elif type ( value ) is float : <EOL> return "<STR_LIT>" % value <EOL> elif type ( value ) is str : <EOL> return "<STR_LIT>" % value <EOL> else : <EOL> return "<STR_LIT>" % value <EOL> status = self . _get_status ( self . _monitor_url ) <EOL> if status != None : <EOL> stats = self . _parse_general_status ( status ) <EOL> heap = self . _parse_heap_status ( status ) <EOL> if stats != None : <EOL> for key in stats . keys ( ) : <EOL> extra = None <EOL> if len ( stats [ key ] ) == <NUM_LIT:4> : <EOL> extra = { stats [ key ] [ <NUM_LIT:2> ] : stats [ key ] [ <NUM_LIT:3> ] } <EOL> self . _logger . emit_value ( "<STR_LIT>" % stats [ key ] [ <NUM_LIT:0> ] , stats [ key ] [ <NUM_LIT:1> ] , extra ) <EOL> if heap != None : <EOL> for key in heap . keys ( ) : <EOL> extra = { heap [ key ] [ <NUM_LIT:2> ] : heap [ key ] [ <NUM_LIT:3> ] } <EOL> self . _logger . emit_value ( "<STR_LIT>" % heap [ key ] [ <NUM_LIT:0> ] , heap [ key ] [ <NUM_LIT:1> ] , extra ) </s>
