<s> import re <EOL> import urllib2 <EOL> import cookielib <EOL> from scalyr_agent import ScalyrMonitor , define_config_option , define_log_field <EOL> __monitor__ = __name__ <EOL> define_config_option ( __monitor__ , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> required_option = True , convert_to = str ) <EOL> define_config_option ( __monitor__ , '<STR_LIT:id>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> define_config_option ( __monitor__ , '<STR_LIT:url>' , <EOL> '<STR_LIT>' , required_option = True ) <EOL> define_config_option ( __monitor__ , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> default = <NUM_LIT:10> , convert_to = float , min_value = <NUM_LIT:0> , max_value = <NUM_LIT:30> ) <EOL> define_config_option ( __monitor__ , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , default = "<STR_LIT>" ) <EOL> define_config_option ( __monitor__ , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , default = False ) <EOL> define_config_option ( __monitor__ , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> default = <NUM_LIT:200> , convert_to = int , min_value = <NUM_LIT:0> , max_value = <NUM_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:url>' , '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> define_log_field ( __monitor__ , '<STR_LIT>' , '<STR_LIT>' ) <EOL> define_log_field ( __monitor__ , '<STR_LIT:status>' , '<STR_LIT>' ) <EOL> define_log_field ( __monitor__ , '<STR_LIT>' , '<STR_LIT>' ) <EOL> define_log_field ( __monitor__ , '<STR_LIT:value>' , '<STR_LIT>' ) <EOL> first_line_pattern = re . compile ( '<STR_LIT>' ) <EOL> class NoRedirection ( urllib2 . HTTPErrorProcessor ) : <EOL> def __init__ ( self ) : <EOL> pass <EOL> def http_response ( self , request , response ) : <EOL> return response <EOL> https_response = http_response <EOL> class UrlMonitor ( ScalyrMonitor ) : <EOL> """<STR_LIT>""" <EOL> def _initialize ( self ) : <EOL> self . url = self . _config . get ( "<STR_LIT:url>" ) <EOL> self . timeout = self . _config . get ( "<STR_LIT>" ) <EOL> self . max_characters = self . _config . get ( "<STR_LIT>" ) <EOL> self . log_all_lines = self . _config . get ( "<STR_LIT>" ) <EOL> extract_expression = self . _config . get ( "<STR_LIT>" ) <EOL> if extract_expression : <EOL> self . extractor = re . compile ( extract_expression ) <EOL> if extract_expression . find ( "<STR_LIT:(>" ) < <NUM_LIT:0> : <EOL> raise Exception ( "<STR_LIT>" % extract_expression ) <EOL> else : <EOL> self . extractor = None <EOL> def gather_sample ( self ) : <EOL> try : <EOL> opener = urllib2 . build_opener ( NoRedirection , urllib2 . HTTPCookieProcessor ( cookielib . CookieJar ( ) ) ) <EOL> response = opener . open ( self . url , None , self . timeout ) <EOL> except urllib2 . HTTPError , e : <EOL> self . _logger . error ( "<STR_LIT>" % ( self . url , e ) ) <EOL> return <EOL> except urllib2 . URLError , e : <EOL> self . _logger . error ( "<STR_LIT>" % ( self . url , e ) ) <EOL> return <EOL> response_body = response . read ( ) <EOL> response . close ( ) <EOL> if self . extractor is not None : <EOL> match = self . extractor . search ( response_body ) <EOL> if match is not None : <EOL> response_body = match . group ( <NUM_LIT:1> ) <EOL> if self . log_all_lines : <EOL> s = response_body <EOL> else : <EOL> first_line = first_line_pattern . search ( response_body ) <EOL> s = '<STR_LIT>' <EOL> if first_line is not None : <EOL> s = first_line . group ( ) . strip ( ) <EOL> if len ( s ) > self . max_characters : <EOL> s = s [ : self . max_characters ] + "<STR_LIT>" <EOL> self . _logger . emit_value ( '<STR_LIT>' , s , extra_fields = { '<STR_LIT:url>' : self . url , '<STR_LIT:status>' : response . getcode ( ) , <EOL> '<STR_LIT>' : len ( response_body ) } ) </s>
