Пример #1
0
  def service_check(self, env):
    import params

    Logger.info("Ambari Metrics service check was started.")
    env.set_params(params)

    results = execute_in_parallel(self.service_check_for_single_host, params.ams_collector_hosts.split(','), params)

    for host in str(params.ams_collector_hosts).split(","):
      if host in results:
        if results[host].status == SUCCESS:
          Logger.info("Ambari Metrics service check passed on host " + host)
          return
        else:
          Logger.warning(results[host].result)
    raise Fail("All metrics collectors are unavailable.")
def create_ams_datasource():
  import params
  server = Server(protocol = params.ams_grafana_protocol.strip(),
                  host = params.ams_grafana_host.strip(),
                  port = params.ams_grafana_port,
                  user = params.ams_grafana_admin_user,
                  password = params.ams_grafana_admin_pwd)

  """
  Create AMS datasource in Grafana, if exsists make sure the collector url is accurate
  """
  Logger.info("Trying to find working metric collector")
  results = execute_in_parallel(do_ams_collector_post, params.ams_collector_hosts.split(','), params)
  new_datasource_host = ""

  for host in params.ams_collector_hosts.split(','):
    if host in results:
      if results[host].status == SUCCESS:
        new_datasource_host = host
        Logger.info("Found working collector on host %s" % new_datasource_host)
        break
      else:
        Logger.warning(results[host].result)

  if new_datasource_host == "":
    Logger.warning("All metric collectors are unavailable. Will use random collector as datasource host.")
    new_datasource_host = params.metric_collector_host

  Logger.info("New datasource host will be %s" % new_datasource_host)

  ams_datasource_json = Template('metrics_grafana_datasource.json.j2',
                            ams_datasource_name=METRICS_GRAFANA_DATASOURCE_NAME, ams_datasource_host=new_datasource_host).get_content()
  Logger.info("Checking if AMS Grafana datasource already exists")

  response = perform_grafana_get_call(GRAFANA_DATASOURCE_URL, server)
  create_datasource = True

  if response and response.status == 200:
    datasources = response.read()
    datasources_json = json.loads(datasources)
    for i in xrange(0, len(datasources_json)):
      datasource_name = datasources_json[i]["name"]
      if datasource_name == METRICS_GRAFANA_DATASOURCE_NAME:
        create_datasource = False # datasource already exists
        Logger.info("Ambari Metrics Grafana datasource already present. Checking Metrics Collector URL")
        datasource_url = datasources_json[i]["url"]

        update_datasource = False
        if is_unchanged_datasource_url(datasource_url, new_datasource_host):
          Logger.info("Metrics Collector URL validation succeeded.")
        else:
          Logger.info("Metrics Collector URL validation failed.")
          update_datasource = True

        datasource_type = datasources_json[i]["type"]
        new_datasource_def = json.loads(ams_datasource_json)
        new_datasource_type = new_datasource_def["type"]

        if datasource_type == new_datasource_type:
          Logger.info("Grafana datasource type validation succeeded.")
        else:
          Logger.info("Grafana datasource type validation failed. Old type = %s, New type = %s" % (datasource_type, new_datasource_type))
          update_datasource = True

        if update_datasource: # Metrics datasource present, but collector host is wrong or the datasource type is outdated.
          datasource_id = datasources_json[i]["id"]
          Logger.info("Updating datasource, id = %s" % datasource_id)

          (response, data) = perform_grafana_put_call(GRAFANA_DATASOURCE_URL, datasource_id,
                                                      ams_datasource_json, server)

          if response.status == 200:
            Logger.info("Ambari Metrics Grafana data source updated.")

          elif response.status == 500:
            Logger.info("Ambari Metrics Grafana data source update failed. Not retrying.")
            raise Fail("Ambari Metrics Grafana data source update failed. PUT request status: %s %s \n%s" %
                       (response.status, response.reason, data))
          else:
            raise Fail("Ambari Metrics Grafana data source creation failed. "
                       "PUT request status: %s %s \n%s" % (response.status, response.reason, data))
        pass
      pass
    pass
  else:
    Logger.info("Error checking for Ambari Metrics Grafana datasource. Will attempt to create.")

  if not create_datasource:
    return
  else:
    Logger.info("Generating datasource:\n%s" % ams_datasource_json)

    (response, data) = perform_grafana_post_call(GRAFANA_DATASOURCE_URL, ams_datasource_json, server)

    if response.status == 200:
      Logger.info("Ambari Metrics Grafana data source created.")
    elif response.status == 500:
      Logger.info("Ambari Metrics Grafana data source creation failed. Not retrying.")
      raise Fail("Ambari Metrics Grafana data source creation failed. POST request status: %s %s \n%s" %
                 (response.status, response.reason, data))
    else:
      Logger.info("Ambari Metrics Grafana data source creation failed.")
      raise Fail("Ambari Metrics Grafana data source creation failed. POST request status: %s %s \n%s" %
                 (response.status, response.reason, data))
  pass