summaryrefslogtreecommitdiffstats
path: root/monitor/dashboard/automated_dashboard_datasource.py
diff options
context:
space:
mode:
authorYang Yu <Gabriel.yuyang@huawei.com>2018-05-02 16:52:11 +0800
committerYu Yang (Gabriel) <Gabriel.yuyang@huawei.com>2018-05-03 08:19:30 +0000
commit3f3676124f276985a0eb8986f53683a39f353dc2 (patch)
tree226b177ff6e8a227368c2d765174ba9d4f875829 /monitor/dashboard/automated_dashboard_datasource.py
parent14add4c9ee3c3114a92ee6c8cf6a6f677ec386a8 (diff)
Restructure monitoring scripts
Restructure the monitoring scripts and modify the scripts to remove the redundance Change-Id: Ib4a5d7bea2dc455ec03ad61780b6caf06831daa8 Signed-off-by: Yang Yu <Gabriel.yuyang@huawei.com> (cherry picked from commit cb67313c1e14b6eca057bdfc7722bc11506b7a2a)
Diffstat (limited to 'monitor/dashboard/automated_dashboard_datasource.py')
-rw-r--r--monitor/dashboard/automated_dashboard_datasource.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/monitor/dashboard/automated_dashboard_datasource.py b/monitor/dashboard/automated_dashboard_datasource.py
new file mode 100644
index 00000000..48f9710c
--- /dev/null
+++ b/monitor/dashboard/automated_dashboard_datasource.py
@@ -0,0 +1,71 @@
+##############################################################################
+# Copyright (c) 2017 Huawei Tech and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
+import logging
+import socket
+import requests
+from oslo_serialization import jsonutils
+
+
+logger = logging.getLogger(__name__)
+MONITOR_DASHBOARD = "/home/opnfv/bottlenecks/monitor/dashboard/"
+
+
+def _create_dashboard(ip, port, path):
+ url = 'http://admin:admin@{}:{}/api/dashboards/db'.format(ip, port)
+ logger.info("Fetched IP for dashboard creation!")
+ with open(path) as f:
+ data = jsonutils.load(f)
+ try:
+ post(url, {"dashboard": data})
+ logger.info("Trying to post dashboard json!")
+ except Exception:
+ logger.info("Create dashboard failed")
+ raise
+
+
+def _create_data_source(ip, port):
+ url = 'http://admin:admin@{}:{}/api/datasources'.format(ip, port)
+ logger.info("Fetched URL for datasource")
+ data = {
+ "name": "automated-ds",
+ "type": "prometheus",
+ "access": "direct",
+ "url": "http://{}:9090".format(ip),
+ }
+ try:
+ post(url, data)
+ logger.info("Trying to post datasource")
+
+ except Exception:
+ logger.info("Create Datasources failed")
+ raise
+
+
+def post(url, data):
+ data = jsonutils.dump_as_bytes(data)
+ logger.info("In post method for dumping data")
+ headers = {'Content-Type': 'application/json'}
+ try:
+ response = requests.post(url, data=data, headers=headers)
+ result = response.json()
+ logger.debug('The result is: %s', result)
+ logger.info("Trying to post")
+ return result
+ except Exception as e:
+ logger.info("Failed post" + str(e))
+ raise
+
+
+ip_address = socket.gethostbyname(socket.gethostname())
+_create_data_source(ip_address, 3000)
+# _create_dashboard(ip_address, 3000,
+# MONITOR_DASHBOARD + 'prometheus_system_rev1.json')
+_create_dashboard(ip_address, 3000,
+ MONITOR_DASHBOARD + 'prototype_dashboard_collapsible.json')