-
Notifications
You must be signed in to change notification settings - Fork 33
/
setup_grafana.py
67 lines (53 loc) · 1.93 KB
/
setup_grafana.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
#!/usr/bin/python3
import requests
import json
import os
import sys
from influxdb import InfluxDBClient
HOST = "localhost"
PORT = 9001
USER = "pi"
PASSWORD = "pi"
DB_NAME = "sleep_monitor"
def get(req):
return requests.get('http://localhost:3000%s' % req, auth=('admin', 'admin'))
def post(req, **kwargs):
return requests.post('http://localhost:3000%s' % req, auth=('admin', 'admin'), **kwargs)
def printResponse(prefix, r):
if r.status_code == 200:
print('%s: success!' % prefix)
else:
print('%s: failed! (%d)' % (prefix, r.status_code))
json.dump(r.json(), sys.stdout, indent=2)
def setupGrafana():
if not get('/api/datasources/name/sleep_monitor'):
print('Creating sleep_monitor datasource for grafana')
s = open('grafana_datasource.json').read()
s = s % {'hostname': os.uname().nodename}
js = json.loads(s)
r = post('/api/datasources', json=js)
printResponse('Datasource creation', r)
else:
print('Data source is already set up.')
print('Creating sleep_monitor dashboard for grafana')
js = json.load(open('grafana_dashboard.json'))
js['inputs'] = []
js['overwrite'] = True
if 'meta' in js:
del js['meta']
js['dashboard']['id'] = None
r = post('/api/dashboards/import', json=js)
printResponse('Dashboard creation', r)
def setupInfluxDb():
client = InfluxDBClient(HOST, PORT, USER, PASSWORD, DB_NAME)
client.create_database('sleep_monitor')
client.create_retention_policy('four_weeks', '4w', '1', default=True)
client.create_database('_internal')
retention_policies = client.get_list_retention_policies('_internal')
for rp in retention_policies:
if rp['name'] == 'monitor':
client.drop_retention_policy('monitor', database='_internal')
break
client.create_retention_policy('monitor', '2h', '1', database='_internal', default=True)
setupGrafana()
setupInfluxDb()