예제 #1
0
def get_processes():
  """Computes the response of the '/cluster/resources/processes' endpoint.

  Returns:
    The processes of the context graph.
  """
  processes = []

  gs = app.context_graph_global_state
  try:
    for node in kubernetes.get_nodes(gs):
      node_id = node['id']
      docker_host = node_id
      for container in docker.get_containers(gs, docker_host):
        container_id = container['id']
        processes.extend(docker.get_processes(gs, docker_host, container_id))

  except collector_error.CollectorError as e:
    return flask.jsonify(utilities.make_error(str(e)))
  except:
    msg = 'get_processes() failed with exception %s' % sys.exc_info()[0]
    app.logger.exception(msg)
    return flask.jsonify(utilities.make_error(msg))

  return flask.jsonify(utilities.make_response(processes, 'resources'))
예제 #2
0
def get_images():
  """Computes the response of the '/cluster/resources/images' endpoint.

  Returns:
    The images of the context graph.
  """
  gs = app.context_graph_global_state

  # A dictionary from Image ID to wrapped image objects.
  # If an image appears more than once, keep only its latest value.
  images_dict = {}

  try:
    for node in kubernetes.get_nodes(gs):
      for image in docker.get_images(gs, node['id']):
        images_dict[image['id']] = image

  except collector_error.CollectorError as e:
    return flask.jsonify(utilities.make_error(str(e)))
  except:
    msg = 'kubernetes.get_images() failed with exception %s' % sys.exc_info()[0]
    app.logger.exception(msg)
    return flask.jsonify(utilities.make_error(msg))

  # The images list is sorted by increasing identifiers.
  images_list = [images_dict[key] for key in sorted(images_dict.keys())]
  return flask.jsonify(utilities.make_response(images_list, 'resources'))
예제 #3
0
def get_images():
    """Computes the response of the '/cluster/resources/images' endpoint.

  Returns:
    The images of the context graph.
  """
    gs = app.context_graph_global_state

    # A dictionary from Image ID to wrapped image objects.
    # If an image appears more than once, keep only its latest value.
    images_dict = {}

    try:
        for node in kubernetes.get_nodes(gs):
            for image in docker.get_images(gs, node['id']):
                images_dict[image['id']] = image

    except collector_error.CollectorError as e:
        return flask.jsonify(utilities.make_error(str(e)))
    except:
        msg = 'kubernetes.get_images() failed with exception %s' % sys.exc_info(
        )[0]
        app.logger.exception(msg)
        return flask.jsonify(utilities.make_error(msg))

    # The images list is sorted by increasing identifiers.
    images_list = [images_dict[key] for key in sorted(images_dict.keys())]
    return flask.jsonify(utilities.make_response(images_list, 'resources'))
예제 #4
0
def get_processes():
    """Computes the response of the '/cluster/resources/processes' endpoint.

  Returns:
    The processes of the context graph.
  """
    processes = []

    gs = app.context_graph_global_state
    try:
        for node in kubernetes.get_nodes(gs):
            node_id = node['id']
            docker_host = node_id
            for container in docker.get_containers(gs, docker_host):
                container_id = container['id']
                processes.extend(
                    docker.get_processes(gs, docker_host, container_id))

    except collector_error.CollectorError as e:
        return flask.jsonify(utilities.make_error(str(e)))
    except:
        msg = 'get_processes() failed with exception %s' % sys.exc_info()[0]
        app.logger.exception(msg)
        return flask.jsonify(utilities.make_error(msg))

    return flask.jsonify(utilities.make_response(processes, 'resources'))
예제 #5
0
def compute_graph(gs, output_format):
    """Collects raw information and computes the context graph.

  The number of concurrent calls to compute_graph() is limited by
  the bounded semaphore gs.get_bounded_semaphore().

  Args:
    gs: global state.
    output_format: one of 'graph', 'dot', 'context_graph', or 'resources'.

  Returns:
  The context graph in the specified format.
  """
    with gs.get_bounded_semaphore():
        input_queue = Queue.PriorityQueue()
        output_queue = Queue.Queue()

        # Compute the number of workers threads to create.
        if gs.get_testing():
            nworkers = 1
        elif gs.get_num_workers() > 0:
            # no range restrictions.
            nworkers = gs.get_num_workers()
        else:
            # get_nodes() may trigger an exception. It will be handled by
            # the exception handler in the caller of this routine.
            nworkers = len(kubernetes.get_nodes(gs))
            # The number of workers must be in the range
            # [constants.MIN_CONCURRENT_WORKERS, constants.MAX_CONCURRENT_WORKERS].
            nworkers = utilities.range_limit(nworkers,
                                             constants.MIN_CONCURRENT_WORKERS,
                                             constants.MAX_CONCURRENT_WORKERS)

        # Start worker threads
        gs.logger_info('creating %d worker threads', nworkers)
        worker_threads = []
        for _ in range(nworkers):
            t = threading.Thread(target=worker,
                                 args=(gs, input_queue, output_queue))
            t.daemon = True
            t.start()
            worker_threads.append(t)

        # Compute the graph
        try:
            result = _do_compute_graph(gs, input_queue, output_queue,
                                       output_format)
        finally:
            # Cleanup: signal all worker threads to stop and wait for them to
            # terminate. If we do not stop the threads they may run forever
            # and constitute a memory leak.
            for _ in worker_threads:
                input_queue.put((0, None, None))

            for t in worker_threads:
                t.join()

    return result
예제 #6
0
def compute_graph(gs, output_format):
  """Collects raw information and computes the context graph.

  The number of concurrent calls to compute_graph() is limited by
  the bounded semaphore gs.get_bounded_semaphore().

  Args:
    gs: global state.
    output_format: one of 'graph', 'dot', 'context_graph', or 'resources'.

  Returns:
  The context graph in the specified format.
  """
  with gs.get_bounded_semaphore():
    input_queue = Queue.PriorityQueue()
    output_queue = Queue.Queue()

    # Compute the number of workers threads to create.
    if gs.get_testing():
      nworkers = 1
    elif gs.get_num_workers() > 0:
      # no range restrictions.
      nworkers = gs.get_num_workers()
    else:
      # get_nodes() may trigger an exception. It will be handled by
      # the exception handler in the caller of this routine.
      nworkers = len(kubernetes.get_nodes(gs))
      # The number of workers must be in the range
      # [constants.MIN_CONCURRENT_WORKERS, constants.MAX_CONCURRENT_WORKERS].
      nworkers = utilities.range_limit(
          nworkers,
          constants.MIN_CONCURRENT_WORKERS, constants.MAX_CONCURRENT_WORKERS)

    # Start worker threads
    gs.logger_info('creating %d worker threads', nworkers)
    worker_threads = []
    for _ in range(nworkers):
      t = threading.Thread(target=worker, args=(gs, input_queue, output_queue))
      t.daemon = True
      t.start()
      worker_threads.append(t)

    # Compute the graph
    try:
      result = _do_compute_graph(gs, input_queue, output_queue, output_format)
    finally:
      # Cleanup: signal all worker threads to stop and wait for them to
      # terminate. If we do not stop the threads they may run forever
      # and constitute a memory leak.
      for _ in worker_threads:
        input_queue.put((0, None, None))

      for t in worker_threads:
        t.join()

  return result
예제 #7
0
def get_containers():
  """Computes the response of the '/cluster/resources/containers' endpoint.

  Returns:
    The containers of the context graph.
  """
  containers = []

  gs = app.context_graph_global_state
  try:
    for node in kubernetes.get_nodes(gs):
      # The node_id is the Docker host name.
      docker_host = node['id']
      containers.extend(docker.get_containers_with_metrics(gs, docker_host))

  except collector_error.CollectorError as e:
    return flask.jsonify(utilities.make_error(str(e)))
  except:
    msg = 'get_containers() failed with exception %s' % sys.exc_info()[0]
    app.logger.exception(msg)
    return flask.jsonify(utilities.make_error(msg))

  return flask.jsonify(utilities.make_response(containers, 'resources'))
예제 #8
0
def get_minions():
  """Computes the response of the '/minions_status' endpoint.

  Returns:
  A dictionary from node names to the status of their minion collectors
  or an error message.
  """
  gs = app.context_graph_global_state
  minions_status = {}
  try:
    for node in kubernetes.get_nodes(gs):
      assert utilities.is_wrapped_object(node, 'Node')
      docker_host = node['id']
      minions_status[docker_host] = docker.get_minion_status(gs, docker_host)

  except collector_error.CollectorError as e:
    return flask.jsonify(utilities.make_error(str(e)))
  except:
    msg = 'get_minions_status() failed with exception %s' % sys.exc_info()[0]
    app.logger.exception(msg)
    return flask.jsonify(utilities.make_error(msg))

  return flask.jsonify(utilities.make_response(minions_status, 'minionsStatus'))
예제 #9
0
def get_containers():
    """Computes the response of the '/cluster/resources/containers' endpoint.

  Returns:
    The containers of the context graph.
  """
    containers = []

    gs = app.context_graph_global_state
    try:
        for node in kubernetes.get_nodes(gs):
            # The node_id is the Docker host name.
            docker_host = node['id']
            containers.extend(
                docker.get_containers_with_metrics(gs, docker_host))

    except collector_error.CollectorError as e:
        return flask.jsonify(utilities.make_error(str(e)))
    except:
        msg = 'get_containers() failed with exception %s' % sys.exc_info()[0]
        app.logger.exception(msg)
        return flask.jsonify(utilities.make_error(msg))

    return flask.jsonify(utilities.make_response(containers, 'resources'))