Dashboard and output file code reconstruction
[bottlenecks.git] / testsuites / posca / testcase_dashboard / system_bandwidth.py
1 #!/usr/bin/python
2 ##############################################################################
3 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10 import ConfigParser
11 from elasticsearch import Elasticsearch
12 import json
13 import os
14 import utils.logger as log
15 from utils.parser import Parser as conf_parser
16
17 LOG = log.Logger(__name__).getLogger()
18 config = ConfigParser.ConfigParser()
19 es = Elasticsearch()
20 dashboard_dir = os.path.join(conf_parser.test_dir,
21                              "posca",
22                              "testcase_dashboard")
23
24
25 def dashboard_send_data(runner_config, test_data):
26     global es
27     es_ip = runner_config['dashboard_ip'].split(':')
28     es = Elasticsearch([{'host': es_ip[0]}])
29     res = es.index(index="bottlenecks",
30                    doc_type=test_data["testcase"],
31                    body=test_data["data_body"])
32     if res['created'] == "False":
33         LOG.error("date send to kibana have errors ", test_data["data_body"])
34
35
36 def dashboard_system_bandwidth(runner_config):
37     global es
38     es_ip = runner_config['dashboard_ip'].split(':')
39     es = Elasticsearch([{'host': es_ip[0]}])
40     # Create bottlenecks index
41     with open(dashboard_dir + 'posca_system_bandwidth_index_pattern.json')\
42             as index_pattern:
43         doc = json.load(index_pattern)
44     res = es.index(
45         index=".kibana",
46         doc_type="index-pattern",
47         id="bottlenecks",
48         body=doc)
49     if res['created'] == "True":
50         LOG.info("bottlenecks index-pattern has created")
51     else:
52         LOG.info("bottlenecks index-pattern has existed")
53
54     with open(dashboard_dir + 'posca_system_bandwidth_config.json')\
55             as index_config:
56         doc = json.load(index_config)
57     res = es.index(index=".kibana", doc_type="config", id="4.6.1", body=doc)
58     if res['created'] == "True":
59         LOG.info("bottlenecks config has created")
60     else:
61         LOG.info("bottlenecks config has existed")
62
63     # Configure discover panel
64     with open(dashboard_dir + 'posca_system_bandwidth_discover.json')\
65             as index_discover:
66         doc = json.load(index_discover)
67     res = es.index(
68         index=".kibana",
69         doc_type="search",
70         id="system_bandwidth",
71         body=doc)
72     if res['created'] == "True":
73         LOG.info("system_bandwidth search has created")
74     else:
75         LOG.info("system_bandwidth search has existed")
76
77     # Create testing data in line graph
78     # Create testing data in line graph
79     with open(dashboard_dir + 'posca_system_bandwidth_line_data.json')\
80             as line_data:
81         doc = json.load(line_data)
82     res = es.index(
83         index=".kibana",
84         doc_type="visualization",
85         id="system_bandwidth_line-date",
86         body=doc)
87     if res['created'] == "True":
88         LOG.info("system_bandwidth_line-date visualization has created")
89     else:
90         LOG.info("system_bandwidth_line-date visualization has existed")
91
92     # Create comparison results in line chart
93     with open(dashboard_dir + 'posca_system_bandwidth_line_char.json')\
94             as line_char:
95         doc = json.load(line_char)
96     res = es.index(
97         index=".kibana",
98         doc_type="visualization",
99         id="system_bandwidth_line-char",
100         body=doc)
101     if res['created'] == "True":
102         LOG.info("system_bandwidth_line-char visualization has created")
103     else:
104         LOG.info("system_bandwidth_line-char visualization has existed")
105
106     # Create monitoring data in table
107     with open(dashboard_dir + 'posca_system_bandwidth_terms_data.json')\
108             as terms_char:
109         doc = json.load(terms_char)
110     res = es.index(index=".kibana", doc_type="visualization",
111                    id="system_bandwidth_terms_data", body=doc)
112     if res['created'] == "True":
113         LOG.info("system_bandwidth_terms_data visualization has created")
114     else:
115         LOG.info("system_bandwidth_terms_data visualization has existed")
116
117     # Create dashboard
118     with open(dashboard_dir + 'posca_system_bandwidth_dashboard.json')\
119             as dashboard:
120         doc = json.load(dashboard)
121     res = es.index(
122         index=".kibana",
123         doc_type="dashboard",
124         id="system_bandwidth_dashboard",
125         body=doc)
126     if res['created'] == "True":
127         LOG.info("system_bandwidth dashboard has created")
128     else:
129         LOG.info("system_bandwidth dashboard has existed")