-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstart.py
executable file
·165 lines (143 loc) · 5.13 KB
/
start.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python
# Based on https://github.com/QualiApps/grafana-docker/blob/master/files/start.py
import requests
from os import environ
from time import sleep
from urlparse import urlunparse
from subprocess import Popen, PIPE
class Grafana(object):
env = environ.get
scheme = "http"
api_path_datasources = "api/datasources"
api_path_gnet = "api/gnet/dashboards/1691"
api_path_import = "api/dashboards/import"
def __init__(self):
'''
Init params
'''
self.params = {
"name": self.env("DS_NAME", "inspectit-influx"),
"type": self.env("DS_TYPE", "influxdb"),
"access": self.env("DS_ACCESS", "proxy"),
"url": self.env("DS_URL", "http://influx:8086"),
"password": self.env("DS_PASS", "root"),
"user": self.env("DS_USER", "root"),
"database": self.env("DS_DB", "inspectit"),
"basicAuth": self.env("DS_AUTH", 'false'),
"basicAuthUser": self.env("DS_AUTH_USER", ""),
"basicAuthPassword": self.env("AUTH_PASS", ""),
"isDefault": self.env("DS_IS_DEFAULT", 'false'),
"jsonData": self.env("DS_JSON_DATA", 'null')
}
# Create grafana api paths
self.gf_url_datasources = urlunparse(
(
self.scheme,
":".join((self.env("GF_HOST", "localhost"), self.env("GF_PORT", "3000"))),
self.api_path_datasources, "", "", ""
)
)
self.gf_url_gnet = urlunparse(
(
self.scheme,
":".join((self.env("GF_HOST", "localhost"), self.env("GF_PORT", "3000"))),
self.api_path_gnet, "", "", ""
)
)
self.gf_url_import = urlunparse(
(
self.scheme,
":".join((self.env("GF_HOST", "localhost"), self.env("GF_PORT", "3000"))),
self.api_path_import, "", "", ""
)
)
# Init requests session
self.auth = self.env("GF_USER", "admin"), self.env("GF_PASS", "admin")
self.sess = requests.Session()
def init_datasource(self):
'''
Upload a datasource
:return bool
'''
response = False
res = self.sess.post(self.gf_url_datasources, data=self.params, auth=self.auth)
if res.status_code == requests.codes.ok:
response = True
return response
def import_dashboard(self):
'''
Import the official inspectIT dashboard
'''
res = self.sess.get(self.gf_url_gnet, auth=self.auth)
print res.status_code
dashboard = res.json()
dashboard["dashboard"] = dashboard.pop("json")
dashboard["overwrite"] = True
dashboard_datasource = {
"name": "DS_INFLUXDB",
"type": "datasource",
"pluginId": self.params["type"],
"value": self.params["name"]
}
dashboard["inputs"] = [dashboard_datasource]
res = self.sess.post(self.gf_url_import, json=dashboard, auth=self.auth)
print res.status_code
def create_influx_database(self):
'''
Workaround for INSPECTIT-2493
'''
res = requests.post("http://influx:8086/query", data={'q':'CREATE DATABASE "inspectit"'})
print res.status_code
print res.text
def start(self):
'''
Start grafana and check api
:return tuple - status, grafana process
'''
status = False
# run grafana
gf_proc = Popen(["/run.sh"], stdout=PIPE)
# wait, until gf api will be available
# trying 5 times
retry = 0
while retry <= 5:
if self._check_gf():
status = True
break
retry += 1
sleep(3)
return status, gf_proc
def _check_gf(self):
'''
Check gf api
:return bool
'''
resp = False
try:
res = self.sess.get(self.gf_url_datasources, auth=self.auth)
resp = True if res and res.status_code == requests.codes.ok else False
except Exception as message:
print "CONNECTION! %s" % message
return resp
if __name__ == "__main__":
gf = Grafana()
try:
exit_code = 0
status, gf_proc = gf.start()
if status:
if gf.init_datasource():
print "*------------SUCCESS! Your datasource was added!------------*"
gf.create_influx_database()
print "*------------SUCCESS! Influx database created!--------------*"
gf.import_dashboard()
print "*------------SUCCESS! Dashboard was added!------------------*"
with gf_proc.stdout:
for line in iter(gf_proc.stdout.readline, b''):
print line,
gf_proc.wait() # wait for the subprocess to exit
exit_code = gf_proc.poll()
except Exception as error:
print "*------------ERROR! %s------------*" % error
exit_code = 1
finally:
exit(exit_code)