Exemple #1
0
    def get(self):
        if self.request.headers.get(SECRET_HEADER) != options.secret:
            logging.warn("Received bad secret from {client}".format(
                client=self.request.remote_ip))
            self.set_status(HTTP_Codes.HTTP_DENIED, "Bad secret")
            return
        if self.request.body:
            payload = json.loads(self.request.body)
        else:
            payload = {}
        include_lists = payload.get('include_lists')
        max_age = payload.get('max_age', ACCEPTABLE_STATS_AGE)

        if include_lists is not None:
            try:
                include_lists = IncludeLists(include_lists)
            except WrongIncludeLists as err:
                logging.warn("Bad request from {client} ({error})".format(
                    client=self.request.remote_ip, error=err))
                json.dump({'error': str(err)}, self)
                self.set_status(HTTP_Codes.HTTP_BAD_REQUEST,
                                'Wrong include_lists')
                return
        else:
            include_lists = self._default_include_lists

        newer_than = time.mktime(datetime.now().timetuple()) - max_age

        if (not self._default_include_lists
                or include_lists.is_subset_of(self._default_include_lists)):
            # If user didn't specify any non-default fields we can use local cache
            fresh_local_snapshots = {
                node_ip: snapshot
                for node_ip, snapshot in self._cached_snapshots.iteritems()
                if max_age and snapshot.utc_timestamp > newer_than
            }
            if fresh_local_snapshots:
                logging.debug(
                    "Returning cluster stats with {} cached snapshots".format(
                        len(fresh_local_snapshots)))
        else:
            fresh_local_snapshots = {}

        new_snapshots_dict, failures = (
            yield self._current_cluster_stats_source.get_current(
                max_age=max_age,
                include_lists=include_lists,
                exclude_nodes=fresh_local_snapshots.keys()))

        # Put new snapshots to local cache
        self._cached_snapshots.update(new_snapshots_dict)

        # Extend fetched snapshots dict with fresh local snapshots
        new_snapshots_dict.update(fresh_local_snapshots)

        rendered_snapshots = {
            node_ip: stats_to_dict(snapshot, include_lists)
            for node_ip, snapshot in new_snapshots_dict.iteritems()
        }

        json.dump({"stats": rendered_snapshots, "failures": failures}, self)
Exemple #2
0
  def get(self):
    if self.request.headers.get(SECRET_HEADER) != options.secret:
      logger.warn("Received bad secret from {client}"
                   .format(client=self.request.remote_ip))
      self.set_status(HTTP_Codes.HTTP_DENIED, "Bad secret")
      return
    if self.request.body:
      payload = json.loads(self.request.body)
    else:
      payload = {}
    include_lists = payload.get('include_lists')
    max_age = payload.get('max_age', ACCEPTABLE_STATS_AGE)

    if include_lists is not None:
      try:
        include_lists = IncludeLists(include_lists)
      except WrongIncludeLists as err:
        logger.warn("Bad request from {client} ({error})"
                     .format(client=self.request.remote_ip, error=err))
        json.dump({'error': str(err)}, self)
        self.set_status(HTTP_Codes.HTTP_BAD_REQUEST, 'Wrong include_lists')
        return
    else:
      include_lists = self._default_include_lists

    newer_than = time.mktime(datetime.now().timetuple()) - max_age

    if (not self._default_include_lists or
        include_lists.is_subset_of(self._default_include_lists)):
      # If user didn't specify any non-default fields we can use local cache
      fresh_local_snapshots = {
        node_ip: snapshot
        for node_ip, snapshot in self._cached_snapshots.iteritems()
        if max_age and snapshot.utc_timestamp > newer_than
      }
      if fresh_local_snapshots:
        logger.debug("Returning cluster stats with {} cached snapshots"
                      .format(len(fresh_local_snapshots)))
    else:
      fresh_local_snapshots = {}

    new_snapshots_dict, failures = (
      yield self._current_cluster_stats_source.get_current(
        max_age=max_age, include_lists=include_lists,
        exclude_nodes=fresh_local_snapshots.keys()
      )
    )

    # Put new snapshots to local cache
    self._cached_snapshots.update(new_snapshots_dict)

    # Extend fetched snapshots dict with fresh local snapshots
    new_snapshots_dict.update(fresh_local_snapshots)

    rendered_snapshots = {
      node_ip: stats_to_dict(snapshot, include_lists)
      for node_ip, snapshot in new_snapshots_dict.iteritems()
    }

    json.dump({
      "stats": rendered_snapshots,
      "failures": failures
    }, self)
Exemple #3
0
    async def __call__(self, request):
        """ Handles HTTP request.

    Args:
      request: an instance of Request.
    Returns:
      An instance of Response.
    """
        if request.has_body:
            payload = await request.json()
        else:
            payload = {}
        include_lists = payload.get('include_lists')
        max_age = payload.get('max_age', ACCEPTABLE_STATS_AGE)

        if include_lists is not None:
            try:
                include_lists = IncludeLists(include_lists)
            except WrongIncludeLists as err:
                logger.warn("Bad request from {client} ({error})".format(
                    client=request.remote, error=err))
                return web.Response(status=http.HTTPStatus.BAD_REQUEST,
                                    reason='Wrong include_lists',
                                    text=str(err))
        else:
            include_lists = self.default_include_lists

        newer_than = time.mktime(datetime.now().timetuple()) - max_age

        if (not self.default_include_lists
                or include_lists.is_subset_of(self.default_include_lists)):
            # If user didn't specify any non-default fields we can use local cache
            fresh_local_snapshots = {
                node_ip: snapshot
                for node_ip, snapshot in self.cached_snapshots.items()
                if max_age and snapshot.utc_timestamp > newer_than
            }
            if fresh_local_snapshots:
                logger.debug(
                    "Returning cluster stats with {} cached snapshots".format(
                        len(fresh_local_snapshots)))
        else:
            fresh_local_snapshots = {}

        new_snapshots_dict, failures = (await self.stats_source.get_current(
            max_age=max_age,
            include_lists=include_lists,
            exclude_nodes=list(fresh_local_snapshots.keys())))

        # Put new snapshots to local cache
        self.cached_snapshots.update(new_snapshots_dict)

        # Extend fetched snapshots dict with fresh local snapshots
        new_snapshots_dict.update(fresh_local_snapshots)

        rendered_snapshots = {
            node_ip: stats_to_dict(snapshot, include_lists)
            for node_ip, snapshot in new_snapshots_dict.items()
        }

        return web.json_response({
            "stats": rendered_snapshots,
            "failures": failures
        })