<s> from datetime import datetime <EOL> import os . path <EOL> import re <EOL> import socket <EOL> import ssl <EOL> import time <EOL> from urlparse import urlparse <EOL> import requests <EOL> import tornado <EOL> from checks . network_checks import EventType , NetworkCheck , Status <EOL> from config import _is_affirmative <EOL> from util import headers as agent_headers <EOL> def get_ca_certs_path ( ) : <EOL> """<STR_LIT>""" <EOL> CA_CERTS = [ <EOL> '<STR_LIT>' , <EOL> os . path . join ( os . path . dirname ( tornado . __file__ ) , '<STR_LIT>' ) , <EOL> '<STR_LIT>' , <EOL> ] <EOL> for f in CA_CERTS : <EOL> if os . path . exists ( f ) : <EOL> return f <EOL> return None <EOL> class HTTPCheck ( NetworkCheck ) : <EOL> SOURCE_TYPE_NAME = '<STR_LIT>' <EOL> SC_STATUS = '<STR_LIT>' <EOL> SC_SSL_CERT = '<STR_LIT>' <EOL> def __init__ ( self , name , init_config , agentConfig , instances ) : <EOL> self . ca_certs = init_config . get ( '<STR_LIT>' , get_ca_certs_path ( ) ) <EOL> NetworkCheck . __init__ ( self , name , init_config , agentConfig , instances ) <EOL> def _load_conf ( self , instance ) : <EOL> tags = instance . get ( '<STR_LIT>' , [ ] ) <EOL> username = instance . get ( '<STR_LIT:username>' ) <EOL> password = instance . get ( '<STR_LIT:password>' ) <EOL> http_response_status_code = str ( instance . get ( '<STR_LIT>' , "<STR_LIT>" ) ) <EOL> timeout = int ( instance . get ( '<STR_LIT>' , <NUM_LIT:10> ) ) <EOL> config_headers = instance . get ( '<STR_LIT>' , { } ) <EOL> headers = agent_headers ( self . agentConfig ) <EOL> headers . update ( config_headers ) <EOL> url = instance . get ( '<STR_LIT:url>' ) <EOL> content_match = instance . get ( '<STR_LIT>' ) <EOL> response_time = _is_affirmative ( instance . get ( '<STR_LIT>' , True ) ) <EOL> if not url : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> include_content = _is_affirmative ( instance . get ( '<STR_LIT>' , False ) ) <EOL> ssl = _is_affirmative ( instance . get ( '<STR_LIT>' , True ) ) <EOL> ssl_expire = _is_affirmative ( instance . get ( '<STR_LIT>' , True ) ) <EOL> instance_ca_certs = instance . get ( '<STR_LIT>' , self . ca_certs ) <EOL> return url , username , password , http_response_status_code , timeout , include_content , headers , response_time , content_match , tags , ssl , ssl_expire , instance_ca_certs <EOL> def _check ( self , instance ) : <EOL> addr , username , password , http_response_status_code , timeout , include_content , headers , response_time , content_match , tags , disable_ssl_validation , ssl_expire , instance_ca_certs = self . _load_conf ( instance ) <EOL> start = time . time ( ) <EOL> service_checks = [ ] <EOL> try : <EOL> self . log . debug ( "<STR_LIT>" % addr ) <EOL> if disable_ssl_validation and urlparse ( addr ) [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> self . warning ( "<STR_LIT>" <EOL> % addr ) <EOL> auth = None <EOL> if username is not None and password is not None : <EOL> auth = ( username , password ) <EOL> r = requests . get ( addr , auth = auth , timeout = timeout , headers = headers , <EOL> verify = False if disable_ssl_validation else instance_ca_certs ) <EOL> except ( socket . timeout , requests . exceptions . ConnectionError , requests . exceptions . Timeout ) as e : <EOL> length = int ( ( time . time ( ) - start ) * <NUM_LIT:1000> ) <EOL> self . log . info ( "<STR_LIT>" <EOL> % ( addr , str ( e ) , length ) ) <EOL> service_checks . append ( ( <EOL> self . SC_STATUS , <EOL> Status . DOWN , <EOL> "<STR_LIT>" % ( str ( e ) , length ) <EOL> ) ) <EOL> except socket . error , e : <EOL> length = int ( ( time . time ( ) - start ) * <NUM_LIT:1000> ) <EOL> self . log . info ( "<STR_LIT>" <EOL> % ( addr , repr ( e ) , length ) ) <EOL> service_checks . append ( ( <EOL> self . SC_STATUS , <EOL> Status . DOWN , <EOL> "<STR_LIT>" % ( repr ( e ) , length ) <EOL> ) ) <EOL> except Exception , e : <EOL> length = int ( ( time . time ( ) - start ) * <NUM_LIT:1000> ) <EOL> self . log . error ( "<STR_LIT>" <EOL> % ( str ( e ) , length ) ) <EOL> raise <EOL> if response_time and not service_checks : <EOL> running_time = time . time ( ) - start <EOL> tags_list = list ( tags ) <EOL> tags_list . append ( '<STR_LIT>' % addr ) <EOL> self . gauge ( '<STR_LIT>' , running_time , tags = tags_list ) <EOL> if not ( service_checks or re . match ( http_response_status_code , str ( r . status_code ) ) ) : <EOL> self . log . info ( "<STR_LIT>" <EOL> % ( http_response_status_code , str ( r . status_code ) ) ) <EOL> service_checks . append ( ( <EOL> self . SC_STATUS , <EOL> Status . DOWN , <EOL> "<STR_LIT>" <EOL> % ( http_response_status_code , str ( r . status_code ) ) <EOL> ) ) <EOL> if not service_checks : <EOL> if content_match : <EOL> content = r . content <EOL> if re . search ( content_match , content ) : <EOL> self . log . debug ( "<STR_LIT>" % content_match ) <EOL> service_checks . append ( ( <EOL> self . SC_STATUS , Status . UP , "<STR_LIT>" <EOL> ) ) <EOL> else : <EOL> self . log . info ( "<STR_LIT>" % content_match ) <EOL> self . log . debug ( "<STR_LIT>" % content ) <EOL> service_checks . append ( ( <EOL> self . SC_STATUS , <EOL> Status . DOWN , <EOL> '<STR_LIT>' % content_match <EOL> ) ) <EOL> else : <EOL> self . log . debug ( "<STR_LIT>" % addr ) <EOL> service_checks . append ( ( <EOL> self . SC_STATUS , Status . UP , "<STR_LIT>" <EOL> ) ) <EOL> if ssl_expire and urlparse ( addr ) [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> status , msg = self . check_cert_expiration ( instance , timeout , instance_ca_certs ) <EOL> service_checks . append ( ( <EOL> self . SC_SSL_CERT , status , msg <EOL> ) ) <EOL> return service_checks <EOL> def _create_status_event ( self , sc_name , status , msg , instance ) : <EOL> if sc_name != self . SC_STATUS : <EOL> return <EOL> url = instance . get ( '<STR_LIT:url>' , None ) <EOL> name = instance . get ( '<STR_LIT:name>' , None ) <EOL> nb_failures = self . statuses [ name ] [ sc_name ] . count ( Status . DOWN ) <EOL> nb_tries = len ( self . statuses [ name ] [ sc_name ] ) <EOL> tags = instance . get ( '<STR_LIT>' , [ ] ) <EOL> tags_list = [ ] <EOL> tags_list . extend ( tags ) <EOL> tags_list . append ( '<STR_LIT>' % url ) <EOL> custom_message = instance . get ( '<STR_LIT:message>' , "<STR_LIT>" ) <EOL> if custom_message : <EOL> custom_message += "<STR_LIT>" <EOL> instance_source_type_name = instance . get ( '<STR_LIT>' , None ) <EOL> if instance_source_type_name is None : <EOL> source_type = "<STR_LIT>" % ( NetworkCheck . SOURCE_TYPE_NAME , name ) <EOL> else : <EOL> source_type = "<STR_LIT>" % ( NetworkCheck . SOURCE_TYPE_NAME , instance_source_type_name ) <EOL> notify = instance . get ( '<STR_LIT>' , self . init_config . get ( '<STR_LIT>' , [ ] ) ) <EOL> notify_message = "<STR_LIT>" <EOL> if notify : <EOL> notify_list = [ ] <EOL> for handle in notify : <EOL> notify_list . append ( "<STR_LIT>" % handle . strip ( ) ) <EOL> notify_message = "<STR_LIT:U+0020>" . join ( notify_list ) + "<STR_LIT>" <EOL> if status == Status . DOWN : <EOL> if isinstance ( msg , tuple ) : <EOL> code , reason , content = msg <EOL> if len ( content ) > <NUM_LIT:200> : <EOL> content = content [ : <NUM_LIT> ] + '<STR_LIT>' <EOL> msg = "<STR_LIT>" % ( code , reason , content ) <EOL> msg = msg . rstrip ( ) <EOL> title = "<STR_LIT>" % ( self . hostname , name ) <EOL> alert_type = "<STR_LIT:error>" <EOL> msg = "<STR_LIT>" "<STR_LIT>" % ( notify_message , custom_message , self . hostname , <EOL> name , url , nb_failures , nb_tries , msg ) <EOL> event_type = EventType . DOWN <EOL> else : <EOL> title = "<STR_LIT>" % ( self . hostname , name ) <EOL> alert_type = "<STR_LIT:success>" <EOL> msg = "<STR_LIT>" % ( notify_message , custom_message , self . hostname , name , url ) <EOL> event_type = EventType . UP <EOL> return { <EOL> '<STR_LIT>' : int ( time . time ( ) ) , <EOL> '<STR_LIT>' : event_type , <EOL> '<STR_LIT:host>' : self . hostname , <EOL> '<STR_LIT>' : msg , <EOL> '<STR_LIT>' : title , <EOL> '<STR_LIT>' : alert_type , <EOL> "<STR_LIT>" : source_type , <EOL> "<STR_LIT>" : name , <EOL> "<STR_LIT>" : tags_list <EOL> } <EOL> def report_as_service_check ( self , sc_name , status , instance , msg = None ) : <EOL> instance_name = self . normalize ( instance [ '<STR_LIT:name>' ] ) <EOL> url = instance . get ( '<STR_LIT:url>' , None ) <EOL> sc_tags = [ '<STR_LIT>' . format ( url ) , "<STR_LIT>" . format ( instance_name ) ] <EOL> custom_tags = instance . get ( '<STR_LIT>' , [ ] ) <EOL> tags = sc_tags + custom_tags <EOL> if sc_name == self . SC_STATUS : <EOL> if isinstance ( msg , tuple ) : <EOL> code , reason , content = msg <EOL> if len ( content ) > <NUM_LIT:200> : <EOL> content = content [ : <NUM_LIT> ] + '<STR_LIT>' <EOL> msg = "<STR_LIT>" % ( code , reason , content ) <EOL> msg = msg . rstrip ( ) <EOL> self . service_check ( sc_name , <EOL> NetworkCheck . STATUS_TO_SERVICE_CHECK [ status ] , <EOL> tags = tags , <EOL> message = msg <EOL> ) <EOL> def check_cert_expiration ( self , instance , timeout , instance_ca_certs ) : <EOL> warning_days = int ( instance . get ( '<STR_LIT>' , <NUM_LIT> ) ) <EOL> url = instance . get ( '<STR_LIT:url>' ) <EOL> o = urlparse ( url ) <EOL> host = o . netloc <EOL> port = o . port or <NUM_LIT> <EOL> try : <EOL> sock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> sock . settimeout ( float ( timeout ) ) <EOL> sock . connect ( ( host , port ) ) <EOL> ssl_sock = ssl . wrap_socket ( sock , cert_reqs = ssl . CERT_REQUIRED , <EOL> ca_certs = instance_ca_certs ) <EOL> cert = ssl_sock . getpeercert ( ) <EOL> except Exception as e : <EOL> return Status . DOWN , "<STR_LIT:%s>" % ( str ( e ) ) <EOL> exp_date = datetime . strptime ( cert [ '<STR_LIT>' ] , "<STR_LIT>" ) <EOL> days_left = exp_date - datetime . utcnow ( ) <EOL> if days_left . days < <NUM_LIT:0> : <EOL> return Status . DOWN , "<STR_LIT>" . format ( days_left . days ) <EOL> elif days_left . days < warning_days : <EOL> return Status . WARNING , "<STR_LIT>" . format ( days_left . days ) <EOL> else : <EOL> return Status . UP , "<STR_LIT>" . format ( days_left . days ) </s>
