コード例 #1
0
ファイル: rabbitmq.py プロジェクト: 499/shoal
    def on_message(self, unused_channel, basic_deliver, properties, body):
        """Retreives information from data, and then updates each squid load in
           shoal if the public/private ip matches. Shoal's key will update with
           the load if there's a key in Shoal. geo_data will update or create a
           new SquidNode if the time since the last timestamp is less than the
           inactive time and a public/private ip exists"""
        logging.debug("Received message: %s", body)
        external_ip = public_ip = private_ip = None
        curr = time()

        # extracts information from body of AMQP message
        try:
            data = json.loads(body)
        except ValueError as e:
            logging.error("Message body could not be decoded. Message: {1}".format(body))
            self.acknowledge_message(basic_deliver.delivery_tag)
            return

        try:
            key = data['uuid']
            hostname = data['hostname']
            time_sent = data['timestamp']
            load = data['load']
            squid_port = data['squid_port']
        except KeyError as e:
            logging.error("Message received was not the proper format (missing:{0}), discarding...".format(e))
            self.acknowledge_message(basic_deliver.delivery_tag)
            return
        try:
            public_ip = data['public_ip']
        except KeyError:
            pass
        try:
            external_ip = data['external_ip']
        except KeyError:
            pass
        try:
            private_ip = data['private_ip']
        except KeyError:
            pass

        data["last_active"] = time()
        # if there's a key in shoal, shoal's key will update with the load
        if key in self.shoal:
            self.shoal[key].update({"load": load,
                                    "last_active": time()})
        # if the difference in time since the last timestamp is less than the inactive time
        # and there exists a public or private ip, then add the geo_data of its location
        elif (curr - time_sent < self.inactive) and (public_ip or private_ip):
            geo_data = utilities.get_geolocation(self._settings['general']['geolitecity_path'], public_ip)
            if not geo_data:
                geo_data = utilities.get_geolocation(self._settings['general']['geolitecity_path'], external_ip)
            if not geo_data:
                logging.error("Unable to generate geo location data, discarding message")
                self.acknowledge_message(basic_deliver.delivery_tag)
                return
            else:
                data['geo_data'] = geo_data
                self.shoal[data["uuid"]] = data
コード例 #2
0
ファイル: shoal.py プロジェクト: robprior/shoal
    def on_message(self, unused_channel, basic_deliver, properties, body):
        external_ip = public_ip = private_ip = None
        curr = time()

        try:
            data = json.loads(body)
        except ValueError as e:
            logging.error("Message body could not be decoded. Message: {1}".format(body))
            self.acknowledge_message(basic_deliver.delivery_tag)
            return
        try:
            key = data['uuid']
            hostname = data['hostname']
            time_sent = data['timestamp']
            load = data['load']
            squid_port = data['squid_port']
        except KeyError as e:
            logging.error("Message received was not the proper format (missing:{0}), discarding...".format(e))
            self.acknowledge_message(basic_deliver.delivery_tag)
            return
        try:
            external_ip = data['external_ip']
        except KeyError:
            pass
        try:
            public_ip = data['public_ip']
        except KeyError:
            pass
        try:
            private_ip = data['private_ip']
        except KeyError:
            pass
        for squid in self.shoal.values():
           if squid.public_ip == public_ip or squid.private_ip == private_ip:
              squid.update(load)
              self.acknowledge_message(basic_deliver.delivery_tag)
              return
        if key in self.shoal:
            self.shoal[key].update(load)
        elif (curr - time_sent < self.INACTIVE) and (public_ip or private_ip):
            geo_data = utilities.get_geolocation(public_ip)
            if not geo_data:
                geo_data = utilities.get_geolocation(external_ip)
            if not geo_data:
                logging.error("Unable to generate geo location data, discarding message")
            else:
                new_squid = SquidNode(key, hostname, squid_port, public_ip, private_ip, external_ip, load, geo_data, time_sent)
                self.shoal[key] = new_squid

        self.acknowledge_message(basic_deliver.delivery_tag)
コード例 #3
0
ファイル: shoal.py プロジェクト: colsond/shoal
    def on_message(self, unused_channel, basic_deliver, properties, body):
        """
        Retreives information from data, and then updates each squid load in 
        shoal if the public/private ip matches. Shoal's key will update with
        the load if there's a key in Shoal. geo_data will update or create a
        new SquidNode if the time since the last timestamp is less than the
        inactive time and a public/private ip exists
        """
        external_ip = public_ip = private_ip = None
        #assume global access unless otherwise indicated
        globalaccess = domainaccess = True
        curr = time()
    
        # extracts information from data from body
        try:
            data = json.loads(body)
        except ValueError as e:
            logging.error("Message body could not be decoded. Message: {1}".format(body))
            self.acknowledge_message(basic_deliver.delivery_tag)
            return
        try:
            key = data['uuid']
            hostname = data['hostname']
            time_sent = data['timestamp']
            load = data['load']
            squid_port = data['squid_port']

        except KeyError as e:
            logging.error("Message received was not the proper format (missing:{0}), discarding...".format(e))
            self.acknowledge_message(basic_deliver.delivery_tag)
            return
        try:
            external_ip = data['external_ip']
        except KeyError:
            pass
        try:
            public_ip = data['public_ip']
        except KeyError:
            pass
        try:
            private_ip = data['private_ip']
        except KeyError:
            pass
        try:
            verified = data['verified']
        except KeyError:
            verified=config.squid_verified_default
        try:
            maxload=data['max_load']
        except KeyError:
            maxload= config.squid_max_load
        try:
            if 'True' in data['global_access']:
                globalaccess = True
            else:
                globalaccess = False
        except KeyError:
            pass
        try:
            if 'True' in data['domain_access']:
                domainaccess = True
            else:
                domainaccess = False
        except KeyError:
            pass

        # if there's a key in shoal, shoal's key will update with the load
        if key in self.shoal:
            self.shoal[key].update(load)
        # if the difference in time since the last timestamp is less than the inactive time
        # and there exists a public or private ip, then the geo_data will update its location
        # or create a new SquidNode for shoal if the geo_data doesn't exist
        elif (curr - time_sent < self.INACTIVE) and (public_ip or private_ip):
            geo_data = utilities.get_geolocation(public_ip)
            if not geo_data:
                geo_data = utilities.get_geolocation(external_ip)
            if not geo_data:
                logging.error("Unable to generate geo location data, discarding message")
            else:
                new_squid = SquidNode(key, hostname, squid_port, public_ip, private_ip, external_ip, load, geo_data, verified, globalaccess, domainaccess, maxload, time_sent)
                self.shoal[key] = new_squid
                utilities.verify_new_squid(public_ip)

        self.acknowledge_message(basic_deliver.delivery_tag)
コード例 #4
0
ファイル: shoal.py プロジェクト: alexjlam/shoal
    def on_message(self, unused_channel, basic_deliver, properties, body):
        """
        Retreives information from data, and then updates each squid load in 
        shoal if the public/private ip matches. Shoal's key will update with
        the load if there's a key in Shoal. geo_data will update or create a
        new SquidNode if the time since the last timestamp is less than the
        inactive time and a public/private ip exists
        """
        external_ip = public_ip = private_ip = None
        curr = time()
    
        # extracts information from data from body
        try:
            data = json.loads(body)
        except ValueError as e:
            logging.error("Message body could not be decoded. Message: {1}".format(body))
            self.acknowledge_message(basic_deliver.delivery_tag)
            return
        try:
            key = data['uuid']
            hostname = data['hostname']
            time_sent = data['timestamp']
            load = data['load']
            squid_port = data['squid_port']
        except KeyError as e:
            logging.error("Message received was not the proper format (missing:{0}), discarding...".format(e))
            self.acknowledge_message(basic_deliver.delivery_tag)
            return
        try:
            external_ip = data['external_ip']
        except KeyError:
            pass
        try:
            public_ip = data['public_ip']
        except KeyError:
            pass
        try:
            private_ip = data['private_ip']
        except KeyError:
            pass
    
        # for each squid in shoal, if public or private ip matches,
        # load for the squid will update and send a acknowledgment message
        for squid in self.shoal.values():
           if squid.public_ip == public_ip or squid.private_ip == private_ip:
              squid.update(load)
              self.acknowledge_message(basic_deliver.delivery_tag)
              return
    
        # if there's a key in shoal, shoal's key will update with the load
        if key in self.shoal:
            self.shoal[key].update(load)
        # if the difference in time since the last timestamp is less than the inactive time
        # and there exists a public or private ip, then the geo_data will update its location
        # or create a new SquidNode for shoal if the geo_data doesn't exist
        elif (curr - time_sent < self.INACTIVE) and (public_ip or private_ip):
            geo_data = utilities.get_geolocation(public_ip)
            if not geo_data:
                geo_data = utilities.get_geolocation(external_ip)
            if not geo_data:
                logging.error("Unable to generate geo location data, discarding message")
            else:
                new_squid = SquidNode(key, hostname, squid_port, public_ip, private_ip, external_ip, load, geo_data, time_sent)
                self.shoal[key] = new_squid

        self.acknowledge_message(basic_deliver.delivery_tag)
コード例 #5
0
ファイル: shoal.py プロジェクト: hep-gc/shoal
    def on_message(self, unused_channel, basic_deliver, properties, body):
        """
        Retreives information from data, and then updates each squid load in
        shoal if the public/private ip matches. Shoal's key will update with
        the load if there's a key in Shoal. geo_data will update or create a
        new SquidNode if the time since the last timestamp is less than the
        inactive time and a public/private ip exists
        """
        external_ip = public_ip = private_ip = None
        #assume global access unless otherwise indicated
        globalaccess = domainaccess = True
        drift_detected = False
        drift_time = 0
        curr = time()

        # extracts information from data from body
        try:
            data = json.loads(body)
        except ValueError:
            logging.error("Message body could not be decoded. Message: %s", body[1])
            self.acknowledge_message(basic_deliver.delivery_tag)
            return
        try:
            key = data['uuid']
            hostname = data['hostname']
            time_sent = data['timestamp']
            load = data['load']
            squid_port = data['squid_port']

        except KeyError as exc:
            logging.error(
                "Message received was not the proper format (missing:%s), discarding...", exc)
            self.acknowledge_message(basic_deliver.delivery_tag)
            return
        try:
            external_ip = data['external_ip']
        except KeyError:
            pass
        try:
            public_ip = data['public_ip']
        except KeyError:
            public_ip = external_ip
        try:
            private_ip = data['private_ip']
        except KeyError:
            pass
        try:
            verified = data['verified']
        except KeyError:
            verified = config.squid_verified_default
        try:
            maxload = data['max_load']
        except KeyError:
            maxload = config.squid_max_load
        try:
            globalaccess = bool('True' in data['global_access'])
        except KeyError:
            pass
        try:
            domainaccess = bool('True' in data['domain_access'])
        except KeyError:
            pass

        # attempt to detect misconfigured clocks and clock drifts,
        # allows for a 10 second grace period
        if curr - time_sent > 10:
            logging.error(
                "Potential clock drift dectected: %s second descrepency on %s",
                (curr-time_sent),
                public_ip)
            drift_detected = True
        elif curr - time_sent < -10:
            logging.error(
                "Recived message from %s seconds in the future from %s",
                (-1*(curr-time_sent)),
                public_ip)
            drift_detected = True
        # this else is redundant because the var is initally set to
        # false but this works well as a failsafe
        else:
            drift_detected = False
        drift_time = (curr-time_sent)

        # if there's a key in shoal, shoal's key will update with the load and drift detection
        if key in self.shoal:
            self.shoal[key].update(load, drift_detected, drift_time)
        # if the difference in time since the last timestamp is less than the inactive time
        # and there exists a public or private ip, then the geo_data will update its location
        # or create a new SquidNode for shoal if the geo_data doesn't exist
        elif (curr - time_sent < self.INACTIVE) and (public_ip or private_ip):
            geo_data = utilities.get_geolocation(public_ip)
            if not geo_data:
                geo_data = utilities.get_geolocation(external_ip)
            if not geo_data:
                logging.error("Unable to generate geo location data, discarding message")
            else:
                new_squid = SquidNode(
                    key,
                    hostname,
                    squid_port,
                    public_ip,
                    private_ip,
                    external_ip,
                    load,
                    geo_data,
                    verified,
                    globalaccess,
                    domainaccess,
                    drift_detected,
                    drift_time,
                    maxload,
                    time_sent)
                self.shoal[key] = new_squid
        self.acknowledge_message(basic_deliver.delivery_tag)
コード例 #6
0
ファイル: shoal.py プロジェクト: dhrjklt/shoal
    def on_message(self, unused_channel, basic_deliver, properties, body):
        """
        Retreives information from data, and then updates each squid load in 
        shoal if the public/private ip matches. Shoal's key will update with
        the load if there's a key in Shoal. geo_data will update or create a
        new SquidNode if the time since the last timestamp is less than the
        inactive time and a public/private ip exists
        """
        external_ip = public_ip = private_ip = None
        #assume global access unless otherwise indicated
        globalaccess = domainaccess = True
        drift_detected = False
        drift_time = 0
        curr = time()
    
        # extracts information from data from body
        try:
            data = json.loads(body)
        except ValueError as e:
            logging.error("Message body could not be decoded. Message: {1}".format(body))
            self.acknowledge_message(basic_deliver.delivery_tag)
            return
        try:
            key = data['uuid']
            hostname = data['hostname']
            time_sent = data['timestamp']
            load = data['load']
            squid_port = data['squid_port']

        except KeyError as e:
            logging.error("Message received was not the proper format (missing:{0}), discarding...".format(e))
            self.acknowledge_message(basic_deliver.delivery_tag)
            return
        try:
            external_ip = data['external_ip']
        except KeyError:
            pass
        try:
            public_ip = data['public_ip']
        except KeyError:
            public_ip=external_ip
        try:
            private_ip = data['private_ip']
        except KeyError:
            pass
        try:
            verified = data['verified']
        except KeyError:
            verified=config.squid_verified_default
        try:
            maxload=data['max_load']
        except KeyError:
            maxload= config.squid_max_load
        try:
            if 'True' in data['global_access']:
                globalaccess = True
            else:
                globalaccess = False
        except KeyError:
            pass
        try:
            if 'True' in data['domain_access']:
                domainaccess = True
            else:
                domainaccess = False
        except KeyError:
            pass

        #attempt to detect misconfigured clocks and clock drifts, allows for a 10 second grace period
        if (curr -time_sent > 10):
            logging.error("Potential clock drift dectected: %s second descrepency on %s" % ((curr-time_sent), public_ip))
            drift_detected = True
        elif(curr - time_sent < -10):
            logging.error("Recived message from %s seconds in the future from %s" % ((-1*(curr-time_sent)), public_ip))
            drift_detected = True
        #this else is redundant because the var is initally set to false but this works well as a failsafe
        else:
            drift_detected = False
        drift_time = (curr-time_sent)

        # if there's a key in shoal, shoal's key will update with the load and drift detection
        if key in self.shoal:
            self.shoal[key].update(load, drift_detected, drift_time)
        # if the difference in time since the last timestamp is less than the inactive time
        # and there exists a public or private ip, then the geo_data will update its location
        # or create a new SquidNode for shoal if the geo_data doesn't exist
        elif (curr - time_sent < self.INACTIVE) and (public_ip or private_ip):
            geo_data = utilities.get_geolocation(public_ip)
            if not geo_data:
                geo_data = utilities.get_geolocation(external_ip)
            if not geo_data:
                logging.error("Unable to generate geo location data, discarding message")
            else:
                new_squid = SquidNode(key, hostname, squid_port, public_ip, private_ip, external_ip, load, geo_data, verified, globalaccess, domainaccess, drift_detected, drift_time, maxload, time_sent)
                self.shoal[key] = new_squid
        self.acknowledge_message(basic_deliver.delivery_tag)