forked from percona/pmm-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import-dashboards.py
executable file
·266 lines (204 loc) · 8.11 KB
/
import-dashboards.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env python
# Grafana dashboard importer script.
import base64
import binascii
import hashlib
import json
import os
import random
import shutil
import sqlite3
import string
import subprocess
import sys
import time
import requests
GRAFANA_DB_DIR = sys.argv[1] if len(sys.argv) > 1 else '/var/lib/grafana'
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
DASHBOARD_DIR = SCRIPT_DIR + '/dashboards/'
NEW_VERSION_FILE = SCRIPT_DIR + '/VERSION'
OLD_VERSION_FILE = GRAFANA_DB_DIR + '/PERCONA_DASHBOARDS_VERSION'
HOST = 'http://127.0.0.1:3000'
def grafana_headers(api_key):
"""
Returns HTTP headers for all requests to Grafana.
"""
return {'Authorization': 'Bearer %s' % (api_key,), 'Content-Type': 'application/json'}
def get_api_key():
"""
Generates a new API key and returns its name, representation for API, and representation for DB.
Keep in sync with Grafana implementation:
* https://sourcegraph.com/github.com/grafana/grafana/-/blob/pkg/api/apikey.go
* https://sourcegraph.com/github.com/grafana/grafana/-/blob/pkg/components/apikeygen/apikeygen.go
* https://sourcegraph.com/github.com/grafana/grafana/-/blob/pkg/util/encoding.go
"""
alphanum = string.digits + string.ascii_uppercase + string.ascii_uppercase.lower()
name = 'PMM Import ' + ''.join(random.choice(alphanum) for _ in range(16))
key = ''.join(random.choice(alphanum) for _ in range(32))
api_key = base64.b64encode(json.dumps({'k': key, 'n': name, 'id': 1}))
db_key = binascii.hexlify(hashlib.pbkdf2_hmac('sha256', key, name, 10000, 50))
return (name, api_key, db_key)
def check_dashboards_version():
upgrade = False
with open(NEW_VERSION_FILE, 'r') as f:
new_ver = f.read().strip()
old_ver = 'N/A'
if os.path.exists(OLD_VERSION_FILE):
upgrade = True
with open(OLD_VERSION_FILE, 'r') as f:
old_ver = f.read().strip()
print ' * Dashboards upgrade from version %s to %s.' % (old_ver, new_ver)
if old_ver == new_ver:
print ' * The dashboards are up-to-date (%s).' % (old_ver,)
sys.exit(0)
return upgrade
def start_grafana():
res = None
if os.path.exists('/usr/bin/supervisorctl'):
res = subprocess.call(["/usr/bin/supervisorctl", "start", "grafana"])
else:
res = subprocess.call(["/bin/systemctl", "start", "grafana-server"])
print ' * Grafana start: %r.' % (res,)
def stop_grafana():
res = None
if os.path.exists('/usr/bin/supervisorctl'):
res = subprocess.call(["/usr/bin/supervisorctl", "stop", "grafana"])
else:
res = subprocess.call(["/bin/systemctl", "stop", "grafana-server"])
print ' * Grafana stop: %r.' % (res,)
def wait_for_grafana_start():
sys.stdout.write(' * Waiting for Grafana to start')
sys.stdout.flush()
for _ in xrange(60):
try:
requests.get('%s/api/datasources' % HOST, timeout=0.1)
except (requests.exceptions.ConnectionError, requests.exceptions.ReadTimeout):
sys.stdout.write('.')
sys.stdout.flush()
time.sleep(1)
else:
print
return
print "\n * Grafana is unable to start correctly"
sys.exit(-1)
def add_api_key(name, db_key):
con = sqlite3.connect(GRAFANA_DB_DIR + '/grafana.db')
cur = con.cursor()
cur.execute("REPLACE INTO api_key (org_id, name, key, role, created, updated) "
"VALUES (1, ?, ?, 'Admin', datetime('now'), datetime('now'))", (name, db_key))
con.commit()
con.close()
def delete_api_key(db_key, upgrade):
con = sqlite3.connect(GRAFANA_DB_DIR + '/grafana.db')
cur = con.cursor()
# Set home dashboard.
if not upgrade:
cur.execute("REPLACE INTO star (user_id, dashboard_id) "
"SELECT 1, id from dashboard WHERE slug='cross-server-graphs'")
cur.execute("REPLACE INTO preferences (id, org_id, user_id, version, home_dashboard_id, timezone, theme, created, updated) "
"SELECT 1, 1, 0, 0, id, '', '', datetime('now'), datetime('now') from dashboard WHERE slug='cross-server-graphs'")
# Delete key.
cur.execute("DELETE FROM api_key WHERE key = ?", (db_key,))
con.commit()
con.close()
def fix_cloudwatch_datasource():
"""
Replaces incorrect CloudWatch datasource stored as JSON string with correct JSON object.
"""
con = sqlite3.connect(GRAFANA_DB_DIR + '/grafana.db')
cur = con.cursor()
found = False
cur.execute("SELECT id, json_data FROM data_source WHERE name = 'CloudWatch'")
for row in cur.fetchall():
found = True
old = None
try:
old = json.loads(row[1])
except:
pass
if not isinstance(old, dict):
new = {'authType': 'keys'}
cur.execute("UPDATE data_source SET json_data = ? WHERE id = ?", (json.dumps(new), row[0]))
con.commit()
con.close()
def add_datasources(api_key):
r = requests.get('%s/api/datasources' % (HOST,), headers=grafana_headers(api_key))
print r.status_code, r.content
ds = [x['name'] for x in json.loads(r.content)]
if 'Prometheus' not in ds:
data = json.dumps({'name': 'Prometheus', 'type': 'prometheus', 'url': 'http://127.0.0.1:9090/prometheus/', 'access': 'proxy', 'isDefault': True})
r = requests.post('%s/api/datasources' % HOST, data=data, headers=grafana_headers(api_key))
print r.status_code, r.content
if r.status_code != 200:
print ' * Cannot add Prometheus Data Source'
sys.exit(-1)
if 'CloudWatch' not in ds:
data = json.dumps({'name': 'CloudWatch', 'type': 'cloudwatch', 'jsonData': {'authType': 'keys'}, 'access': 'proxy', 'isDefault': False})
r = requests.post('%s/api/datasources' % HOST, data=data, headers=grafana_headers(api_key))
print r.status_code, r.content
if r.status_code != 200:
print ' * Cannot add CloudWatch Data Source'
sys.exit(-1)
def import_dashboards(api_key):
# Import dashboards with overwrite.
files = []
for f in os.listdir(DASHBOARD_DIR):
if not f.endswith('.json'):
continue
files.append(DASHBOARD_DIR + f)
for file_ in files:
print file_
f = open(file_, 'r')
dash = json.load(f)
f.close()
# Set time range and refresh options.
dash['time']['from'] = 'now-1h'
dash['time']['to'] = 'now'
dash['refresh'] = '1m'
data = json.dumps({'dashboard': dash, 'overwrite': True})
r = requests.post('%s/api/dashboards/db' % HOST, data=data, headers=grafana_headers(api_key))
if r.status_code != 200:
print r.status_code, r.content
print ' * Cannot add %s Dashboard' % file_
sys.exit(-1)
def copy_apps():
for app in ['pmm-app']:
source_dir = '/usr/share/percona-dashboards/' + app
dest_dir = '/var/lib/grafana/plugins/' + app
if os.path.isdir(source_dir):
print app
shutil.rmtree(dest_dir, True)
shutil.copytree(source_dir, dest_dir)
def import_apps(api_key):
for app in ['pmm-app']:
print app
data = json.dumps({'enabled': True})
r = requests.post('%s/api/plugins/%s/settings' % (HOST, app), data=data, headers=grafana_headers(api_key))
if r.status_code != 200:
print r.status_code, r.content
print ' * Cannot add %s app' % app
sys.exit(-1)
def main():
upgrade = check_dashboards_version()
name, api_key, db_key = get_api_key()
# modify database when Grafana is stopped to avoid a data race
stop_grafana()
try:
copy_apps()
add_api_key(name, db_key)
fix_cloudwatch_datasource()
finally:
start_grafana()
wait_for_grafana_start()
add_datasources(api_key)
import_apps(api_key)
import_dashboards(api_key)
# modify database when Grafana is stopped to avoid a data race
stop_grafana()
try:
delete_api_key(db_key, upgrade)
finally:
start_grafana()
shutil.copyfile(NEW_VERSION_FILE, OLD_VERSION_FILE)
if __name__ == '__main__':
main()