summaryrefslogtreecommitdiff
path: root/ambari-server/src/main/resources/common-services/AMBARI_METRICS/0.1.0/package/scripts/metrics_grafana_util.py
blob: 5ab40b02d29c4338709ba2b4821ef67c33df476c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python
"""
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

"""
from resource_management.core.logger import Logger
from resource_management.core.base import Fail
from resource_management import Template

import httplib
import time
import socket
import json

def create_ams_datasource():

  import params
  GRAFANA_CONNECT_TRIES = 5
  GRAFANA_CONNECT_TIMEOUT = 15
  GRAFANA_URL = "/api/datasources"
  METRICS_GRAFANA_DATASOURCE_NAME = "AMBARI_METRICS"

  headers = {"Content-type": "application/json"}

  Logger.info("Checking if AMS Grafana datasource already exists")
  Logger.info("Connecting (GET) to %s:%s%s" % (params.hostname,
                                               params.ams_grafana_port,
                                               GRAFANA_URL))

  conn = httplib.HTTPConnection(params.hostname,
                                int(params.ams_grafana_port))

  conn.request("GET", GRAFANA_URL)
  response = conn.getresponse()
  Logger.info("Http response: %s %s" % (response.status, response.reason))

  if(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):

        Logger.info("Ambari Metrics Grafana datasource already present. Checking Metrics Collector URL")
        datasource_url = datasources_json[i]["url"]

        if datasource_url == (params.ams_grafana_protocol + "://"
                                + params.metric_collector_host + ":"
                                + params.metric_collector_port):
          Logger.info("Metrics Collector URL validation succeeded. Skipping datasource creation")
          GRAFANA_CONNECT_TRIES = 0 # No need to create datasource again

        else: # Metrics datasource present, but collector host is wrong.

          Logger.info("Metrics Collector URL validation failed.")
          datasource_id = datasources_json[i]["id"]
          Logger.info("Deleting obselete Metrics datasource.")
          conn = httplib.HTTPConnection(params.hostname, int(params.ams_grafana_port))
          conn.request("DELETE", GRAFANA_URL + "/" + str(datasource_id))
          response = conn.getresponse()
          Logger.info("Http response: %s %s" % (response.status, response.reason))

        break
  else:
    Logger.info("Error checking for Ambari Metrics Grafana datasource. Will attempt to create.")

  if GRAFANA_CONNECT_TRIES > 0:
    Logger.info("Attempting to create Ambari Metrics Grafana datasource")

  for i in xrange(0, GRAFANA_CONNECT_TRIES):
    try:
      ams_datasource_json = Template('metrics_grafana_datasource.json.j2',
                             ams_datasource_name=METRICS_GRAFANA_DATASOURCE_NAME,
                             ams_grafana_protocol=params.ams_grafana_protocol,
                             ams_collector_host=params.metric_collector_host,
                             ams_collector_port=params.metric_collector_port).get_content()

      Logger.info("Generated datasource:\n%s" % ams_datasource_json)

      Logger.info("Connecting (POST) to %s:%s%s" % (params.hostname,
                                                    params.ams_grafana_port,
                                                    GRAFANA_URL))
      conn = httplib.HTTPConnection(params.hostname,
                                    int(params.ams_grafana_port))
      conn.request("POST", GRAFANA_URL, ams_datasource_json, headers)

      response = conn.getresponse()
      Logger.info("Http response: %s %s" % (response.status, response.reason))
    except (httplib.HTTPException, socket.error) as ex:
      if i < GRAFANA_CONNECT_TRIES - 1:
        time.sleep(GRAFANA_CONNECT_TIMEOUT)
        Logger.info("Connection to Grafana failed. Next retry in %s seconds."
                    % (GRAFANA_CONNECT_TIMEOUT))
        continue
      else:
        raise Fail("Ambari Metrics Grafana datasource not created")

    data = response.read()
    Logger.info("Http data: %s" % data)
    conn.close()

    if response.status == 200:
      Logger.info("Ambari Metrics Grafana data source created.")
      break
    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.")
      if i < GRAFANA_CONNECT_TRIES - 1:
        time.sleep(GRAFANA_CONNECT_TIMEOUT)
        Logger.info("Next retry in %s seconds."
                  % (GRAFANA_CONNECT_TIMEOUT))
      else:
        raise Fail("Ambari Metrics Grafana data source creation failed. POST request status: %s %s \n%s" %
                 (response.status, response.reason, data))