(self)
Connect to the graphite server
Connect to the graphite server
Connect to the graphite server
def _connect(self):
        """
        Connect to the graphite server
        """
        if (self.proto == 'udp'):
            stream = socket.SOCK_DGRAM
        else:
            stream = socket.SOCK_STREAM

        if (self.proto[-1] == '4'):
            family = socket.AF_INET
            connection_struct = (self.host, self.port)
        elif (self.proto[-1] == '6'):
            family = socket.AF_INET6
            connection_struct = (self.host, self.port,
                                 self.flow_info, self.scope_id)
        else:
            connection_struct = (self.host, self.port)
            try:
                addrinfo = socket.getaddrinfo(self.host, self.port, 0, stream)
            except socket.gaierror as ex:
                self.log.error("GraphiteHandler: Error looking up graphite host"
                               " '%s' - %s",
                               self.host, ex)
                return
            if (len(addrinfo) > 0):
                family = addrinfo[0][0]
                if (family == socket.AF_INET6):
                    connection_struct = (self.host, self.port,
                                         self.flow_info, self.scope_id)
            else:
                family = socket.AF_INET

        # Create socket
        self.socket = socket.socket(family, stream)
        if self.socket is None:
            # Log Error
            self.log.error("GraphiteHandler: Unable to create socket.")
            # Close Socket
            self._close()
            return
        # Enable keepalives?
        if self.proto != 'udp' and self.keepalive:
            self.log.error("GraphiteHandler: Setting socket keepalives...")
            self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
            self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE,
                                   self.keepaliveinterval)
            self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL,
                                   self.keepaliveinterval)
            self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 3)
        # Set socket timeout
        self.socket.settimeout(self.timeout)
        # Connect to graphite server
        try:
            self.socket.connect(connection_struct)
            # Log
            self.log.debug("GraphiteHandler: Established connection to "
                           "graphite server %s:%d.",
                           self.host, self.port)
            self.last_connect_timestamp = time.time()
        except Exception as ex:
            # Log Error
            self._throttle_error("GraphiteHandler: Failed to connect to "
                                 "%s:%i. %s.", self.host, self.port, ex)
            # Close Socket
            self._close()
            return
def _connect ( self ) : if ( self . proto == 'udp' ) : stream = socket . SOCK_DGRAM else : stream = socket . SOCK_STREAM if ( self . proto [ - 1 ] == '4' ) : family = socket . AF_INET connection_struct = ( self . host , self . port ) elif ( self . proto [ - 1 ] == '6' ) : family = socket . AF_INET6 connection_struct = ( self . host , self . port , self . flow_info , self . scope_id ) else : connection_struct = ( self . host , self . port ) try : addrinfo = socket . getaddrinfo ( self . host , self . port , 0 , stream ) except socket . gaierror as ex : self . log . error ( "GraphiteHandler: Error looking up graphite host" " '%s' - %s" , self . host , ex ) return if ( len ( addrinfo ) > 0 ) : family = addrinfo [ 0 ] [ 0 ] if ( family == socket . AF_INET6 ) : connection_struct = ( self . host , self . port , self . flow_info , self . scope_id ) else : family = socket . AF_INET # Create socket self . socket = socket . socket ( family , stream ) if self . socket is None : # Log Error self . log . error ( "GraphiteHandler: Unable to create socket." ) # Close Socket self . _close ( ) return # Enable keepalives? if self . proto != 'udp' and self . keepalive : self . log . error ( "GraphiteHandler: Setting socket keepalives..." ) self . socket . setsockopt ( socket . SOL_SOCKET , socket . SO_KEEPALIVE , 1 ) self . socket . setsockopt ( socket . IPPROTO_TCP , socket . TCP_KEEPIDLE , self . keepaliveinterval ) self . socket . setsockopt ( socket . IPPROTO_TCP , socket . TCP_KEEPINTVL , self . keepaliveinterval ) self . socket . setsockopt ( socket . IPPROTO_TCP , socket . TCP_KEEPCNT , 3 ) # Set socket timeout self . socket . settimeout ( self . timeout ) # Connect to graphite server try : self . socket . connect ( connection_struct ) # Log self . log . debug ( "GraphiteHandler: Established connection to " "graphite server %s:%d." , self . host , self . port ) self . last_connect_timestamp = time . time ( ) except Exception as ex : # Log Error self . _throttle_error ( "GraphiteHandler: Failed to connect to " "%s:%i. %s." , self . host , self . port , ex ) # Close Socket self . _close ( ) return