summaryrefslogtreecommitdiffstats
path: root/monitor/dashboard/automated_dashboard_datasource.py
blob: abfeca2bca37293f7759c7a318f3fce3cce59810 (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
##############################################################################
# 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 + 'stats_overview.json')
_create_dashboard(ip_address, 3000,
                  MONITOR_DASHBOARD + 'jump_server_status.json')