feature(grafana): Grafana uses url, user and password from configuration, check mon... 41/8741/3
authorbravof <fbravo@whitestack.com>
Tue, 31 Mar 2020 21:42:45 +0000 (18:42 -0300)
committerFabián Alexis Bravo Abarca <fbravo@whitestack.com>
Wed, 1 Apr 2020 12:40:18 +0000 (09:40 -0300)
Change-Id: I223e07e599f87ec07fe09c73dd68cd89aecc73bf
Signed-off-by: bravof <fbravo@whitestack.com>
.vscode/settings.json [new file with mode: 0644]
docker/Dockerfile
osm_mon/core/mon.yaml
osm_mon/dashboarder/backends/grafana.py
osm_mon/dashboarder/service.py

diff --git a/.vscode/settings.json b/.vscode/settings.json
new file mode 100644 (file)
index 0000000..c5c5d21
--- /dev/null
@@ -0,0 +1,3 @@
+{
+    "cornflakes.linter.executablePath": "/usr/local/bin/flake8"
+}
\ No newline at end of file
index b21e6e5..5032aad 100644 (file)
@@ -54,6 +54,9 @@ ENV OSMMON_DATABASE_COMMONKEY changeme
 ENV OSMMON_COLLECTOR_INTERVAL 30
 ENV OSMMON_EVALUATOR_INTERVAL 30
 ENV OSMMON_PROMETHEUS_URL http://prometheus:9090
+ENV OSMMON_GRAFANA_URL http://grafana:3000
+ENV OSMMON_GRAFANA_USER admin
+ENV OSMMON_GRAFANA_PASSWORD admin
 
 EXPOSE 8000
 
index c7b08f5..e25760b 100644 (file)
@@ -52,6 +52,8 @@ dashboarder:
 
 grafana:
   url: http://grafana:3000
+  user: admin
+  password: admin
 
 prometheus:
   url: http://prometheus:9090
index 4f1fe52..acc602e 100644 (file)
 # License for the specific language governing permissions and limitations
 # under the License.
 # For those usages not covered by the Apache License, Version 2.0 please
-# contact: glavado@whitestack.com
+# contact: glavado@whitestack.com or fbravo@whitestack.com
 ##
 import logging
 import requests
+import base64
+from osm_mon.core.config import Config
 
 log = logging.getLogger(__name__)
 
-# TODO (lavado): migrate to Class, import config variables to get token
-url = "http://grafana:3000/api/"
-headers = {
-    'content-type': "application/json",
-    'authorization': "Basic YWRtaW46YWRtaW4="
-    }
 
-
-def get_all_dashboard_uids():
-    # Gets only dashboards that were automated by OSM (with tag 'osm_automated')
-    response = requests.request("GET", url + "search?tag=osm_automated", headers=headers)
-    dashboards = response.json()
-    dashboard_uids = []
-    for dashboard in dashboards:
-        dashboard_uids.append(dashboard['uid'])
-    log.debug("Searching for all dashboard uids: %s", dashboard_uids)
-    return dashboard_uids
-
-
-def get_dashboard_status(uid):
-    response = requests.request("GET", url + "dashboards/uid/" + uid, headers=headers)
-    log.debug("Searching for dashboard result: %s", response.text)
-    return response
-
-
-def create_dashboard(uid, name, json_file):
-    with open(json_file) as f:
-        dashboard_data = f.read()
-
-    dashboard_data = dashboard_data.replace('OSM_ID', uid).replace('OSM_NAME', name)
-
-    response = requests.request("POST", url + "dashboards/db/",  data=dashboard_data, headers=headers)
-    log.debug("Creating dashboard result: %s", response.text)
-    return response
-
-
-def delete_dashboard(uid):
-    response = requests.request("DELETE", url + "dashboards/uid/" + uid, headers=headers)
-    log.debug("Delete dashboard result: %s", response.text)
-    return response
+class GrafanaBackend:
+    def __init__(self, config: Config):
+        self.conf = config
+        self.url = config.get('grafana', 'url')
+        grafana_user = config.get("grafana", "user")
+        grafana_password = config.get("grafana", "password")
+        self.headers = {
+            'content-type': "application/json",
+            'authorization': "Basic %s" % base64.b64encode(
+                (grafana_user + ":" + grafana_password).encode("utf-8")).decode()
+        }
+
+    def get_all_dashboard_uids(self):
+        # Gets only dashboards that were automated by OSM (with tag 'osm_automated')
+        response = requests.request("GET", self.url + "/api/search?tag=osm_automated", headers=self.headers)
+        dashboards = response.json()
+        dashboard_uids = []
+        for dashboard in dashboards:
+            dashboard_uids.append(dashboard['uid'])
+        log.debug("Searching for all dashboard uids: %s", dashboard_uids)
+        return dashboard_uids
+
+    def get_dashboard_status(self, uid):
+        response = requests.request("GET", self.url + "/api/dashboards/uid/" + uid, headers=self.headers)
+        log.debug("Searching for dashboard result: %s", response.text)
+        return response
+
+    def create_dashboard(self, uid, name, json_file):
+        with open(json_file) as f:
+            dashboard_data = f.read()
+
+        dashboard_data = dashboard_data.replace('OSM_ID', uid).replace('OSM_NAME', name)
+
+        response = requests.request(
+            "POST", self.url + "/api/dashboards/db/",  data=dashboard_data, headers=self.headers)
+        log.debug("Creating dashboard result: %s", response.text)
+        return response
+
+    def delete_dashboard(self, uid):
+        response = requests.request("DELETE", self.url + "/api/dashboards/uid/" + uid, headers=self.headers)
+        log.debug("Delete dashboard result: %s", response.text)
+        return response
index 8008f7d..9f184d2 100644 (file)
@@ -24,7 +24,7 @@ import logging
 
 from osm_mon.core.common_db import CommonDbClient
 from osm_mon.core.config import Config
-import osm_mon.dashboarder.backends.grafana as grafana
+from osm_mon.dashboarder.backends.grafana import GrafanaBackend
 from osm_mon import __path__ as mon_path
 
 log = logging.getLogger(__name__)
@@ -34,11 +34,12 @@ class DashboarderService:
     def __init__(self, config: Config):
         self.conf = config
         self.common_db = CommonDbClient(self.conf)
+        self.grafana = GrafanaBackend(self.conf)
 
     def create_dashboards(self):
         # TODO lavado: migrate these methods to mongo change streams
         # Lists all dashboards and OSM resources for later comparisons
-        dashboard_uids = grafana.get_all_dashboard_uids()
+        dashboard_uids = self.grafana.get_all_dashboard_uids()
         osm_resource_uids = []
 
         # Reads existing project list and creates a dashboard for each
@@ -50,8 +51,8 @@ class DashboarderService:
             dashboard_path = '{}/dashboarder/templates/project_scoped.json'.format(mon_path[0])
             if project_id not in dashboard_uids:
                 project_name = project['name']
-                grafana.create_dashboard(project_id, project_name,
-                                         dashboard_path)
+                self.grafana.create_dashboard(project_id, project_name,
+                                              dashboard_path)
                 log.debug('Created dashboard for Project: %s', project_id)
             else:
                 log.debug('Dashboard already exists')
@@ -73,8 +74,8 @@ class DashboarderService:
                     if 'monitoring-param' in vnfd:
                         if nsr_id not in dashboard_uids:
                             nsr_name = nsr['name']
-                            grafana.create_dashboard(nsr_id, nsr_name,
-                                                     dashboard_path)
+                            self.grafana.create_dashboard(nsr_id, nsr_name,
+                                                          dashboard_path)
                             log.debug('Created dashboard for NS: %s', nsr_id)
                         else:
                             log.debug('Dashboard already exists')
@@ -88,7 +89,7 @@ class DashboarderService:
         # Delete obsolete dashboards
         for dashboard_uid in dashboard_uids:
             if dashboard_uid not in osm_resource_uids:
-                grafana.delete_dashboard(dashboard_uid)
+                self.grafana.delete_dashboard(dashboard_uid)
                 log.debug('Deleted obsolete dashboard: %s', dashboard_uid)
             else:
                 log.debug('All dashboards in use')