summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRutuja Surve <rutuja.r.surve@gmail.com>2017-09-21 13:35:34 -0400
committerRutuja Surve <rutuja.r.surve@gmail.com>2017-10-20 05:56:35 -0400
commit32a1ab12db4b85bde421465fec5bfb7e15f9ca38 (patch)
tree9ace949380c7b42a6a9f8a900e17f45a2544a858
parentad12c5707ac1191063af5cd33d54dd30ff649787 (diff)
Datasource and Dashboard automation script
JIRA: BOTTLENECK-180 Automated creation of Prometheus datasource and dashboard for Grafana Change-Id: Id1e0323f74a0587b0fe3c80ba7a39843893f00ec Signed-off-by: Rutuja Surve <rutuja.r.surve@gmail.com>
-rw-r--r--monitor/automated-dashboard-datasource.py69
-rw-r--r--monitor/monitoring.sh3
2 files changed, 71 insertions, 1 deletions
diff --git a/monitor/automated-dashboard-datasource.py b/monitor/automated-dashboard-datasource.py
new file mode 100644
index 0000000..1e1b42f
--- /dev/null
+++ b/monitor/automated-dashboard-datasource.py
@@ -0,0 +1,69 @@
+##############################################################################
+# 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__)
+
+
+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_dashboard(ip_address, 3000, '/var/lib/grafana/' +
+ 'dashboards/' +
+ 'prometheus-system_rev1.json')
+_create_data_source(ip_address, 3000)
diff --git a/monitor/monitoring.sh b/monitor/monitoring.sh
index 8ad388a..a4d404c 100644
--- a/monitor/monitoring.sh
+++ b/monitor/monitoring.sh
@@ -42,7 +42,8 @@ sudo docker run --name bottlenecks-prometheus \
-v ${MONITOR_CONFIG}/prometheus.yaml:/etc/prometheus/prometheus.yml \
prom/prometheus
-$ Grafana
+# Grafana
sudo docker run --name bottlenecks-grafana \
-d -p 3000:3000 \
+ -v ${GRAFANA}/config/grafana.ini:/etc/grafana/grafana.ini \
grafana/grafana