Esempio n. 1
0
    def execute(self, listener_stats):
        try:
            if listener_stats:
                stats_base.update_stats_via_driver(listener_stats)
                LOG.info('Updated the listeners statistics')

            listeners, _ = self.listener_repo.get_all(db_apis.get_session())
            listeners_stats, _ = self.listener_stats_repo.get_all(
                db_apis.get_session())

            listeners_ids = [listener.id for listener in listeners]
            listeners_stats_ids = [
                listener_stats.listener_id
                for listener_stats in listeners_stats
            ]

            for listeners_stats_id in listeners_stats_ids:
                if listeners_stats_id not in listeners_ids:
                    self.listener_stats_repo.delete(
                        db_apis.get_session(), listener_id=listeners_stats_id)
                    LOG.info('Delete the statictics entry of listener %s',
                             listeners_stats_id)
        except Exception as e:
            LOG.warning('Failed to update the listener statistics '
                        'due to: {}'.format(str(e)))
Esempio n. 2
0
    def update_listener_statistics(self, statistics):
        """Update listener statistics.

        :param statistics: Statistics for listeners:
              id (string): ID for listener.
              active_connections (int): Number of currently active connections.
              bytes_in (int): Total bytes received.
              bytes_out (int): Total bytes sent.
              request_errors (int): Total requests not fulfilled.
              total_connections (int): The total connections handled.
        :type statistics: dict
        :raises: UpdateStatisticsError
        :returns: None
        """
        listener_stats = statistics.get(lib_consts.LISTENERS, [])
        stats_objects = []
        for stat in listener_stats:
            try:
                stats_obj = data_models.ListenerStatistics(
                    listener_id=stat['id'],
                    bytes_in=stat['bytes_in'],
                    bytes_out=stat['bytes_out'],
                    active_connections=stat['active_connections'],
                    total_connections=stat['total_connections'],
                    request_errors=stat['request_errors'],
                    received_time=time.time())
                stats_objects.append(stats_obj)
            except Exception as e:
                return {
                    lib_consts.STATUS_CODE: lib_consts.DRVR_STATUS_CODE_FAILED,
                    lib_consts.FAULT_STRING: str(e),
                    lib_consts.STATS_OBJECT: lib_consts.LISTENERS
                }

        # Provider drivers other than the amphora driver do not have
        # an amphora ID, use the listener ID again here to meet the
        # constraint requirement.
        try:
            if stats_objects:
                stats_base.update_stats_via_driver(stats_objects)
        except Exception as e:
            return {
                lib_consts.STATUS_CODE: lib_consts.DRVR_STATUS_CODE_FAILED,
                lib_consts.FAULT_STRING: str(e),
                lib_consts.STATS_OBJECT: lib_consts.LISTENERS
            }
        return {lib_consts.STATUS_CODE: lib_consts.DRVR_STATUS_CODE_OK}
Esempio n. 3
0
    def test_update_stats(self, mock_stats_logger, mock_stats_db):
        stats_base._STATS_HANDLERS = None

        # Test with update success
        stats_base.update_stats_via_driver([self.listener_stats], deltas=True)

        mock_stats_db().update_stats.assert_called_once_with(
            [self.listener_stats], deltas=True)
        mock_stats_logger().update_stats.assert_called_once_with(
            [self.listener_stats], deltas=True)

        # Test with update failure (should still run both drivers)
        mock_stats_db.reset_mock()
        mock_stats_logger.reset_mock()
        mock_stats_db().update_stats.side_effect = Exception
        mock_stats_logger().update_stats.side_effect = Exception
        stats_base.update_stats_via_driver([self.listener_stats])

        mock_stats_db().update_stats.assert_called_once_with(
            [self.listener_stats], deltas=False)
        mock_stats_logger().update_stats.assert_called_once_with(
            [self.listener_stats], deltas=False)
Esempio n. 4
0
def update_stats(health_message):
    """Parses the health message then passes it to the stats driver(s)

    :param health_message: The health message containing the listener stats
    :type health_message: dict

    Example V1 message::

        health = {
            "id": "<amphora_id>",
            "listeners": {
                "<listener_id>": {
                    "status": "OPEN",
                    "stats": {
                        "ereq": 0,
                        "conns": 0,
                        "totconns": 0,
                        "rx": 0,
                        "tx": 0,
                    },
                    "pools": {
                        "<pool_id>": {
                            "status": "UP",
                            "members": {"<member_id>": "ONLINE"}
                        }
                    }
                }
            }
        }

    Example V2 message::

        {"id": "<amphora_id>",
         "seq": 67,
         "listeners": {
           "<listener_id>": {
             "status": "OPEN",
             "stats": {
               "tx": 0,
               "rx": 0,
               "conns": 0,
               "totconns": 0,
               "ereq": 0
             }
           }
         },
         "pools": {
             "<pool_id>:<listener_id>": {
               "status": "UP",
               "members": {
                 "<member_id>": "no check"
               }
             }
         },
         "ver": 2
         "recv_time": time.time()
        }

    Example V3 message::

        Same as V2 message, except values are deltas rather than absolutes.
    """
    version = health_message.get("ver", 2)

    deltas = False
    if version >= 3:
        deltas = True

    amphora_id = health_message.get('id')
    listeners = health_message.get('listeners', {})
    listener_stats = []
    for listener_id, listener in listeners.items():
        listener_dict = listener.get('stats')
        stats_model = data_models.ListenerStatistics(
            listener_id=listener_id,
            amphora_id=amphora_id,
            bytes_in=listener_dict.get('rx'),
            bytes_out=listener_dict.get('tx'),
            active_connections=listener_dict.get('conns'),
            total_connections=listener_dict.get('totconns'),
            request_errors=listener_dict.get('ereq'),
            received_time=health_message.get('recv_time'))
        LOG.debug("Listener %s / Amphora %s stats: %s", listener_id,
                  amphora_id, stats_model.get_stats())
        listener_stats.append(stats_model)
    stats_base.update_stats_via_driver(listener_stats, deltas=deltas)