<s> import socket <EOL> import urllib2 <EOL> from health . healthcheck import HealthCheck <EOL> from util import ReportLevels <EOL> from proxy . portproxy import Proxy <EOL> class TcpCheck ( HealthCheck ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , config ) : <EOL> super ( TcpCheck , self ) . __init__ ( ) <EOL> self . config = config <EOL> def run ( self , container , report ) : <EOL> container_port = self . config . getExtraField ( '<STR_LIT:port>' ) <EOL> container_ip = self . getContainerIPAddress ( container ) <EOL> report ( '<STR_LIT>' + container [ '<STR_LIT>' ] [ <NUM_LIT:0> : <NUM_LIT:12> ] + '<STR_LIT>' + str ( container_port ) , <EOL> level = ReportLevels . EXTRA ) <EOL> try : <EOL> sock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> sock . connect ( ( container_ip , container_port ) ) <EOL> sock . close ( ) <EOL> except Exception as e : <EOL> print e <EOL> return False <EOL> return True <EOL> class HttpRequestCheck ( HealthCheck ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , protocol , config ) : <EOL> super ( HttpRequestCheck , self ) . __init__ ( ) <EOL> self . protocol = protocol <EOL> self . config = config <EOL> def run ( self , container , report ) : <EOL> container_port = self . config . getExtraField ( '<STR_LIT:port>' ) <EOL> container_ip = self . getContainerIPAddress ( container ) <EOL> address = '<STR_LIT>' % ( self . protocol , container_ip , container_port ) <EOL> if self . config . hasExtraField ( '<STR_LIT:path>' ) : <EOL> address += self . config . getExtraField ( '<STR_LIT:path>' ) <EOL> report ( '<STR_LIT>' + container [ '<STR_LIT>' ] [ <NUM_LIT:0> : <NUM_LIT:12> ] + '<STR_LIT>' + address , <EOL> level = ReportLevels . EXTRA ) <EOL> try : <EOL> response = urllib2 . urlopen ( address , timeout = <NUM_LIT:2> ) <EOL> response . read ( ) <EOL> except Exception as exc : <EOL> self . logger . exception ( exc ) <EOL> return False <EOL> return True <EOL> class IncomingConnectionCheck ( HealthCheck ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , config ) : <EOL> super ( IncomingConnectionCheck , self ) . __init__ ( ) <EOL> self . config = config <EOL> def run ( self , container , report ) : <EOL> container_ip = self . getContainerIPAddress ( container ) <EOL> for connection in Proxy . get_connections ( ) : <EOL> if not connection . laddr or not connection . raddr : <EOL> continue <EOL> if connection . raddr [ <NUM_LIT:0> ] == container_ip : <EOL> report ( '<STR_LIT>' % container [ '<STR_LIT>' ] [ <NUM_LIT:0> : <NUM_LIT:12> ] , <EOL> level = ReportLevels . EXTRA ) <EOL> return False <EOL> report ( '<STR_LIT>' % container [ '<STR_LIT>' ] [ <NUM_LIT:0> : <NUM_LIT:12> ] , <EOL> level = ReportLevels . EXTRA ) <EOL> return True </s>
