-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgrafana-dash.py
executable file
·173 lines (140 loc) · 5.48 KB
/
grafana-dash.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
#!/usr/bin/env python3
import json
import sys
import os
import argparse
import time
import csv
import difflib
import re
from urllib.parse import urlparse
# https://pypi.org/project/grafana-api/
from grafana_api import GrafanaFace
from grafana_api.grafana_api import GrafanaClientError
from datetime import datetime
SUCCESS=0
ERROR=1
ALL_ORGS = None
CLI = None
def printlist(l):
for e in l:
print(e)
def printerror(*args):
print(*args, file=sys.stderr)
def read_dash_json(fname):
with open(fname, 'r') as f:
dash_json = json.loads(f.read())
return dash_json
def dashboard_command(args):
if args.list:
# get source org dashboards
dashboards = CLI.search.search_dashboards()
writer = csv.writer(sys.stdout, dialect=csv.excel_tab)
keys = dashboards[0].keys()
writer.writerow(keys)
for d in dashboards:
writer.writerow([d[k] for k in keys])
elif args.dump is None:
# download all dashboards for the org
dashboards = CLI.search.search_dashboards()
for dash in dashboards:
if dash['type'] != 'dash-db':
continue
dump_dashboard(dash['title'])
elif args.dump != '__all__':
# download a dashboard
dump_dashboard(args.dump)
elif args.diff:
# show diff of the locally saved dashboard json, and the one in the Grafana
new_dash = read_dash_json(args.diff)
uid = new_dash['dashboard']['uid']
old_dash = CLI.dashboard.get_dashboard(uid)
new_dash_json=json.dumps(new_dash, sort_keys=True, indent=2)
old_dash_json=json.dumps(old_dash, sort_keys=True, indent=2)
printlist(difflib.unified_diff(old_dash_json.split("\n"), new_dash_json.split("\n")))
elif args.upload:
# upload the local dashboard json. Overwrite if the same uid exists
new_dash = read_dash_json(args.upload)
payload = {
"dashboard": new_dash['dashboard'],
"folderId": new_dash['meta']['folderId'],
"overwrite": True
}
upload_dashboard(payload)
return SUCCESS
def dump_dashboard(dash_title):
# prepare source
dashes = CLI.search.search_dashboards(dash_title)
if not dashes:
raise ValueError("{} not found".format(dash_title))
dash = None
for d in dashes:
if d['title'] == dash_title:
dash = d
break
else:
raise ValueError("Ambiguous dashboard title {}. Found multiple {}".format(dash_title, dash))
# ignore folders and other non-dashboard data
if dash['type'] != 'dash-db':
raise ValueError("{} is not a dashboard type: The type is {}".format(dash_title, dash['type']))
# Getting the dashboard from the source org
dash_detail = CLI.dashboard.get_dashboard(dash['uid'])
name = "{}_{}".format(dash['id'], dash['title']).lower() + ".json"
name = re.sub(r'[\\/:*?"<>| ]+','_',name)
printerror("Saving {}".format(dash['title']))
with open(name, 'w') as fh:
fh.write(json.dumps(dash_detail))
def upload_dashboard(dash_json):
CLI.dashboard.update_dashboard(dash_json)
def find_org_by_name(org_name):
"""
find the target org id by name
"""
global ALL_ORGS
if not ALL_ORGS:
ALL_ORGS = CLI.organizations.list_organization()
org = None
for o in ALL_ORGS:
if o["name"] == org_name:
org = o
return org
return None
def main():
global CLI
parser = argparse.ArgumentParser(description="Grafana admin")
parser.add_argument("--url", help="Grafana URL")
parser.add_argument("--admin", help="admin username")
parser.add_argument("--password", help="admin password")
parser.add_argument("--org", dest="org_name", help="org name")
# dashbord command
parser.add_argument("--list", default=False, action='store_true', help="list dashboards")
parser.add_argument("--dump", nargs='?', help="dump a dashboard", default='__all__')
parser.add_argument("--diff", help="take a diff of locally saved dashboard json and the one in the Grafana")
parser.add_argument("--upload", help="upload a dashboard. Overwrite existing dashboard if the same uid exists")
parser.set_defaults(func=dashboard_command)
args = parser.parse_args()
# try to find configs from env var if not given in the command line
args.url = args.url or os.environ.get("GRAFANA_URL")
args.admin = args.admin or os.environ.get("GRAFANA_ADMIN_USER")
args.password = args.password or os.environ.get("GRAFANA_ADMIN_PASS")
args.org_name = args.org_name or os.environ.get("GRAFANA_ORG")
# check mandatory configs
if not (args.admin and args.password and args.org_name):
print("Please set admin user and password. Alternatively the admin user and the password can be set via GRAFANA_ADMIN_USER and GRAFANA_ADMIN_PASS env variables.")
sys.exit(ERROR)
# create a grafana client.
u = urlparse(args.url)
CLI = GrafanaFace((args.admin, args.password), host= u.netloc, protocol=u.scheme)
# make sure the org exists
org = find_org_by_name(args.org_name)
if not org:
printerror("org \"{}\" not found".format(args.org_name))
printerror("Valid orgs:")
for o in ALL_ORGS:
printerror(o['name'])
return ERROR
# Switch the cli to the target org. Please note the cli is stateful in terms of org.
CLI.organizations.switch_organization(org["id"])
return args.func(args)
if __name__ == "__main__":
sys.exit(main())