def process_web_request(self, request, path, params, fragment):
        options = dict(command_processor.get_global_options())
        options.update(params)
        catalog = spectator_client.get_source_catalog(options)
        data_map = self._get_data_map(catalog, options)

        if self.accepts_content_type(request, 'text/html'):
            content_type = 'text/html'
            by_service = self.service_map_to_html
            by_type = self.type_map_to_html
        else:
            content_type = 'text/plain'
            by_service = self.service_map_to_text
            by_type = self.type_map_to_text

        by = options.get('by', 'service')
        if by == 'service':
            content_data = by_service(data_map, params=params)
        else:
            content_data = by_type(data_map, params=params)

        if content_type == 'text/html':
            body = http_server.build_html_document(content_data,
                                                   title='Current Metrics')
        else:
            body = content_data
        request.respond(200, {'ContentType': content_type}, body)
Example #2
0
    def process_web_request(self, request, path, params, fragment):
        options = dict(command_processor.get_global_options())
        options['services'] = ','.join(options.get('services', '[all]'))
        options.update(params)
        service_list = options.get('services', 'all').split(',')
        options['services'] = service_list
        data_map = self.__get_data_map(options)

        if accepts_html(request):
            content_type = 'text/html'
            by_service = self.service_map_to_html
            by_type = self.type_map_to_html
        else:
            content_type = 'text/plain'
            by_service = self.service_map_to_text
            by_type = self.type_map_to_text

        by = options.get('by', 'service')
        if by == 'service':
            content_data = by_service(data_map, params=params)
        else:
            content_data = by_type(data_map, params=params)

        if content_type == 'text/html':
            body = http_server.build_html_document(content_data,
                                                   title='Current Metrics')
        else:
            body = content_data
        request.respond(200, {'ContentType': content_type}, body)
Example #3
0
  def process_web_request(self, request, path, params, fragment):
    options = dict(command_processor.get_global_options())
    options['services'] = ','.join(options.get('services', '[all]'))
    options.update(params)
    service_list = options.get('services', 'all').split(',')
    options['services'] = service_list
    data_map = self.__get_data_map(options)

    if accepts_html(request):
      content_type = 'text/html'
      by_service = self.service_map_to_html
      by_type = self.type_map_to_html
    else:
      content_type = 'text/plain'
      by_service = self.service_map_to_text
      by_type = self.type_map_to_text

    by = options.get('by', 'service')
    if by == 'service':
      content_data = by_service(data_map, params=params)
    else:
      content_data = by_type(data_map, params=params)

    if content_type == 'text/html':
      body = http_server.build_html_document(
          content_data, title='Current Metrics')
    else:
      body = content_data
    request.respond(200, {'ContentType': content_type}, body)
Example #4
0
  def process_web_request(self, request, path, params, fragment):
    options = dict(get_global_options())
    options.update(params)

    surveyor = StackdriverSurveyor(options)
    descriptor_list = get_descriptor_list(options)

    begin_time = time.time()
    survey = surveyor.build_survey_from_descriptor_list(descriptor_list)
    survey_secs = time.time() - begin_time
    if str(params.get('delete_unused')).lower() == 'true':
      self.delete_unused(options, surveyor, survey)
    info_list = surveyor.survey_to_sorted_list(survey)

    footer = 'Survey Time = %.1f s' % survey_secs
    header = 'Activity during %s ... %s (%s)' % (
        surveyor.start_time, surveyor.end_time,
        SurveyInfo.to_days_since(surveyor.days_since))

    if self.accepts_content_type(request, 'text/html'):
      html = header + '<p>'
      html += self.survey_to_html(surveyor, info_list)
      html += '<br/>' + footer
      html_doc = http_server.build_html_document(
          html, title='Last Custom Descriptor Use')
      request.respond(200, {'ContentType': 'text/html'}, html_doc)
    elif self.accepts_content_type(request, 'application/json'):
      json_doc = json.JSONEncoder(indent=2).encode(info_list)
      request.respond(200, {'ContentType': 'application/json'}, json_doc)
    else:
      text = self.survey_to_text(info_list)
      text += '\n\n' + footer
      request.respond(200, {'ContentType': 'text/plain'}, text)
Example #5
0
  def enabled(self):
    """Determine if stackdriver is enabled so commands are visible.

    This is only applicable to web commands. Commandline commands are always
    available.
    """
    return 'stackdriver' in (get_global_options()
                             .get('monitor', {}).get('metric_store', []))
Example #6
0
  def process_web_request(self, request, path, params, fragment):
    options = dict(command_processor.get_global_options())
    options['services'] = ','.join(options.get('services', '[all]'))
    options.update(params)
    options['services'] = options.get('services', 'all').split(',')

    data_map = self.__get_data_map(options)
    body = json.JSONEncoder(indent=2).encode(data_map)
    request.respond(200, {'ContentType': 'application/json'}, body)
Example #7
0
    def process_web_request(self, request, path, params, fragment):
        options = dict(command_processor.get_global_options())
        options['services'] = ','.join(options.get('services', '[all]'))
        options.update(params)
        options['services'] = options.get('services', 'all').split(',')

        data_map = self.__get_data_map(options)
        body = json.JSONEncoder(indent=2).encode(data_map)
        request.respond(200, {'ContentType': 'application/json'}, body)
 def process_web_request(self, request, path, params, fragment):
     """Implements CommandHandler."""
     options = dict(get_global_options())
     options.update(params)
     type_map, processor = self.__do_clear(options)
     response_code = (httplib.OK if processor.num_ok == len(type_map) else
                      httplib.INTERNAL_SERVER_ERROR)
     headers, body = processor.make_response(
         request, self.accepts_content_type(request, 'text/html'),
         'Deleted', 'Cleared Time Series')
     request.respond(response_code, headers, body)
    def process_web_request(self, request, path, params, fragment):
        options = dict(command_processor.get_global_options())
        options.update(params)
        catalog = spectator_client.get_source_catalog(options)

        type_map, service_tag_map, active_services = (
            self.__get_type_and_tag_map_and_active_services(catalog, options))

        params = strip_non_html_params(options)
        html = self.to_html(type_map, service_tag_map, active_services, params)
        html_doc = http_server.build_html_document(html, title='Metric Usage')
        request.respond(200, {'ContentType': 'text/html'}, html_doc)
Example #10
0
  def process_web_request(self, request, path, params, fragment):
    options = dict(command_processor.get_global_options())
    options['disable_metric_filter'] = True
    options.update(params)
    spectator = self.make_spectator_client(options)
    catalog = spectator_client.get_source_catalog(options)
    service_map = spectator.scan_by_service(catalog, options)

    yaml_dict = self.process_service_map(service_map)
    html = '<pre>%s</pre>' % encode_yaml(yaml_dict)
    html_doc = http_server.build_html_document(
        html, title='Inferred Meter Specs')
    request.respond(200, {'ContentType': 'application/html'}, html_doc)
Example #11
0
 def process_web_request(self, request, path, params, fragment):
   options = dict(command_processor.get_global_options())
   options.update(params)
   catalog = spectator_client.get_source_catalog(options)
   param_services = params.get('services', 'all').split(',')
   if param_services == ['all']:
     restricted_catalog = catalog
   else:
     restricted_catalog = {key: value
                           for key, value in catalog.items()
                           if key in param_services}
   data_map = self._get_data_map(restricted_catalog, options)
   body = json.JSONEncoder(indent=2).encode(data_map)
   request.respond(200, {'ContentType': 'application/json'}, body)
Example #12
0
    def process_web_request(self, request, path, params, fragment):
        options = dict(command_processor.get_global_options())
        options['services'] = ','.join(options.get('services', '[all]'))
        options.update(params)
        service_list = options.get('services', 'all').split(',')
        options['services'] = service_list

        type_map, service_tag_map, active_services = (
            self.__get_type_and_tag_map_and_active_services(options))

        params = strip_non_html_params(options)
        html = self.to_html(type_map, service_tag_map, active_services, params)
        html_doc = http_server.build_html_document(html, title='Metric Usage')
        request.respond(200, {'ContentType': 'text/html'}, html_doc)
Example #13
0
  def process_web_request(self, request, path, params, fragment):
    options = dict(command_processor.get_global_options())
    options['services'] = ','.join(options.get('services', '[all]'))
    options.update(params)
    service_list = options.get('services', 'all').split(',')
    options['services'] = service_list

    type_map, service_tag_map, active_services = (
        self.__get_type_and_tag_map_and_active_services(options))

    params = strip_non_html_params(options)
    html = self.to_html(type_map, service_tag_map, active_services, params)
    html_doc = http_server.build_html_document(
        html, title='Metric Usage')
    request.respond(200, {'ContentType': 'text/html'}, html_doc)
Example #14
0
  def process_web_request(self, request, path, params, fragment):
    options = dict(get_global_options())
    options.update(params)
    descriptor_list = get_descriptor_list(options)

    if self.accepts_content_type(request, 'text/html'):
      html = self.descriptors_to_html(descriptor_list)
      html_doc = http_server.build_html_document(
          html, title='Custom Descriptors')
      request.respond(200, {'ContentType': 'text/html'}, html_doc)
    elif self.accepts_content_type(request, 'application/json'):
      json_doc = json.JSONEncoder(indent=2).encode(descriptor_list)
      request.respond(200, {'ContentType': 'application/json'}, json_doc)
    else:
      text = self.descriptors_to_text(descriptor_list)
      request.respond(200, {'ContentType': 'text/plain'}, text)
Example #15
0
  def process_web_request(self, request, path, params, fragment):
    options = dict(command_processor.get_global_options())
    options.update(params)
    all_service_endpoints = spectator_client.determine_service_endpoints(
        options)
    param_services = params.get('services', 'all').split(',')
    if param_services == 'all':
      service_endpoints = all_service_endpoints
    else:
      service_endpoints = {key: value
                           for key, value in all_service_endpoints.items()
                           if key in all_service_endpoints.keys()}

    data_map = self._get_data_map(service_endpoints, options)
    body = json.JSONEncoder(indent=2).encode(data_map)
    request.respond(200, {'ContentType': 'application/json'}, body)
    def process_web_request(self, request, path, params, fragment):
        """Implements CommandHandler."""
        if str(params.get('clear_all')).lower() != 'true':
            html = textwrap.dedent("""\
          Clearing descriptors requires query parameter
          <code>clear_all=true</code>.
          <p/>
          <a href="{path}?clear_all=true">Yes, delete everything!</a>
      """)
            html_doc = http_server.build_html_document(
                html, title='Missing Parameters')
            request.respond(400, {'ContentType': 'text/html'}, html_doc)
            return

        options = dict(get_global_options())
        options.update(params)
        descriptor_list, processor = self.__do_clear(options)
        response_code = (httplib.OK if processor.num_ok == len(descriptor_list)
                         else httplib.INTERNAL_SERVER_ERROR)
        headers, body = processor.make_response(
            request, self.accepts_content_type(request, 'text/html'),
            'Deleted', 'Cleared Time Series')
        request.respond(response_code, headers, body)
    def process_web_request(self, request, path, params, fragment):
        """Implements CommandHandler."""
        options = dict(get_global_options())
        options.update(params)

        if str(params.get('clear_all')).lower() != 'true':
            stackdriver = stackdriver_service.make_service(options)
            audit_results = stackdriver_descriptors.AuditResults(stackdriver)
            descriptor_list = audit_results.descriptor_map.values()
            descriptor_html = '\n<li> '.join(item['type']
                                             for item in descriptor_list)
            html = textwrap.dedent("""\
          Clearing descriptors requires query parameter
          <code>clear_all=true</code>.
          <p/>
          Here are the {count} custom descriptors:
          <ul>
          <li>{descriptors}
          </ul>
          <p/>
          <a href="{path}?clear_all=true">Yes, delete everything!</a>
      """.format(count=len(descriptor_list),
                 descriptors=descriptor_html,
                 path=path))

            html_doc = http_server.build_html_document(
                html, title='Missing Parameters')
            request.respond(400, {'ContentType': 'text/html'}, html_doc)
            return

        audit_results = self.__do_clear(options)
        response_code = (httplib.OK if audit_results.obsoleted_count == 0 else
                         httplib.INTERNAL_SERVER_ERROR)
        headers = {'Content-Type': 'text/plain'}
        body = audit_results_to_output(audit_results,
                                       "No custom descriptors to delete.")
        request.respond(response_code, headers, body)
    def process_web_request(self, request, path, params, fragment):
        """Implements CommandHandler."""
        options = dict(get_global_options())
        stackdriver_options = options['stackdriver']
        mode = params.get('mode', 'none').lower()
        stackdriver_options['manage_descriptors'] = mode
        stackdriver = stackdriver_service.make_service(options)
        manager = stackdriver.descriptor_manager
        audit_results = manager.audit_descriptors(options)

        create_html = ''
        delete_html = ''
        full_html = ''
        if audit_results.num_unresolved_issues > 0:
            if audit_results.missing_count or audit_results.outdated_count:
                create_html = (
                    '<a href="{path}?mode=create">Create/Update ONLY</a>'.
                    format(path=path))

            if audit_results.obsoleted_count:
                delete_html = (
                    '<a href="{path}?mode=delete">Delete ONLY</a>'.format(
                        path=path))

            if create_html and delete_html:
                full_html = (
                    '<a href="{path}?mode=full">Create/Update AND Delete</a>'.
                    format(path=path))
        if not create_html:
            create_html = '<i>No Create/Update Needed</i>'
        if not delete_html:
            delete_html = '<i>No Extra Descriptors</i>'

        text = audit_results_to_output(audit_results,
                                       'Metric Filters not configured.')

        stackdriver_metric_prefix = stackdriver_descriptors.determine_metric_prefix(
            stackdriver_options)
        unchanged_names = audit_results.unchanged_descriptor_names
        if unchanged_names:
            unchanged = '{count} Unchanged Descriptors:\n  - {list}'.format(
                count=len(unchanged_names),
                list='\n  - '.join([
                    name[name.find(stackdriver_metric_prefix):]
                    for name in unchanged_names
                ]))
        else:
            unchanged = ''

        body = textwrap.dedent("""
          <b>Actions</b>
          <p/>
          {create}<br/>{delete}<br/>{full}
          <p/>
          <hr/>
          <b>Audit Results</b>
          <p/>
          <pre>{unchanged}
          {text}
          </pre>
      """.format(unchanged=unchanged,
                 text=text,
                 create=create_html,
                 delete=delete_html,
                 full=full_html))

        response_code = (httplib.OK if audit_results.errors == 0 else
                         httplib.INTERNAL_SERVER_ERROR)
        headers = {'Content-Type': 'text/html'}
        request.respond(response_code, headers, body)