-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
212 lines (184 loc) · 9.05 KB
/
main.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
import os
import json
import logging
import argparse
import urllib3
import requests
GRAFANA_URL = os.getenv("GRAFANA_URL")
GRAFANA_TOKEN = os.getenv("GRAFANA_TOKEN")
REQUEST_HEADERS = {'Authorization': 'Bearer {}'.format(GRAFANA_TOKEN), 'Content-Type': 'application/json', 'Accept': 'application/json'}
DIR_DATASOURCES = os.getenv("DIR_DATASOURCES", "./datasources")
DIR_FOLDERS = os.getenv("DIR_FOLDERS", "./folders")
DIR_DASHBOARDS = os.getenv("DIR_DASHBOARDS", "./dashboards")
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
logging.basicConfig(
format=u'%(levelname)-8s [%(asctime)s] %(message)s',
level=LOG_LEVEL,
)
def get_datasources():
""" Get Grafana Datasources """
response = requests.get('{}/api/datasources'.format(GRAFANA_URL), headers=REQUEST_HEADERS, verify=False)
if response.status_code != 200:
logging.error('{} {}'.format(response.status_code, response.text))
else:
datasources = json.loads(response.text)
# Create datasources directory
if not os.path.exists(DIR_DATASOURCES):
os.makedirs(DIR_DATASOURCES)
for ds in datasources:
# Remove unneccessary keys from datasource json
ds.pop('id')
ds.pop('typeLogoUrl')
# Write modified datasource json to file
filename = '{}/{}.json'.format(DIR_DATASOURCES, ds['name'])
with open(filename, "w") as file:
file.write(json.dumps(ds, indent=4))
logging.info("Datasource {} downloaded".format(ds['name']))
logging.info("Datasources download done")
def get_folders():
""" Get Grafana Folders """
response = requests.get('{}/api/folders'.format(GRAFANA_URL), headers=REQUEST_HEADERS, verify=False)
if response.status_code != 200:
logging.error('{} {}'.format(response.status_code, response.text))
else:
folders = json.loads(response.text)
# Create folders directory
if not os.path.exists(DIR_FOLDERS):
os.makedirs(DIR_FOLDERS)
for folder in folders:
# Remove all keys except 'uid' and 'title' from folder json
output = {key: folder[key] for key in ['uid', 'title']}
# Replace unwanted symbols with underscore
folder_name = folder['title'].replace(' ', '_').replace(':', '')
# Write modified folder json to file
filename = '{}/{}.json'.format(DIR_FOLDERS, folder_name)
with open(filename, "w") as file:
file.write(json.dumps(output, indent=4))
logging.info("Folder {} downloaded".format(folder_name))
logging.info("Folders download done")
def get_dashboards():
""" Get Grafana Dashboards """
response = requests.get('{}/api/search/?type=dash-db'.format(GRAFANA_URL), headers=REQUEST_HEADERS, verify=False)
if response.status_code != 200:
logging.error('{} {}'.format(response.status_code, response.text))
else:
dashboards = json.loads(response.text)
# Create dashboards directory
if not os.path.exists(DIR_DASHBOARDS):
os.makedirs(DIR_DASHBOARDS)
# Generate dashboard's uid list
dashboards_uid_list = [dash['uid'] for dash in dashboards]
# Get dashboards by uid
for uid in dashboards_uid_list:
response = requests.get('{}/api/dashboards/uid/{}'.format(GRAFANA_URL, uid), headers=REQUEST_HEADERS, verify=False)
dashboard = json.loads(response.text)
# Replace unwanted symbols with underscore
dashboard_name = dashboard['dashboard']['title'].replace(' ', '_').replace(':', '')
# Remove 'id' from 'dashboard' section from dashboard json
dashboard['dashboard'].pop('id')
# Write modified dashboard json to file
filename = '{}/{}.json'.format(DIR_DASHBOARDS, dashboard_name)
with open(filename, "w") as file:
file.write(json.dumps(dashboard, indent=4))
logging.info("Dashboard {} downloaded".format(dashboard_name))
logging.info("Dashboards download done")
def upload_datasources():
""" Upload Grafana Datasources """
# Interactive warning
choice = input('Datasources will be uploaded to {} Continue? [y/n]: '.format(GRAFANA_URL))
if choice != 'y':
exit()
# Get files list if datasources directory
for datasource_file in os.listdir(DIR_DATASOURCES):
filename = '{}/{}'.format(DIR_DATASOURCES, datasource_file)
# Upload datasources json
with open(filename, "r") as file:
datasource_json = json.loads(file.read())
response = requests.post('{}/api/datasources'.format(GRAFANA_URL), json=datasource_json, headers=REQUEST_HEADERS, verify=False)
if response.status_code != 200:
logging.warning('{}: {} {}'.format(datasource_file, response.status_code, response.text))
else:
logging.info("Datasource {} uploaded".format(datasource_file))
def upload_folders():
""" Upload Grafana Folders """
# Interactive warning
choice = input('Folders will be uploaded to {} Continue? [y/n]: '.format(GRAFANA_URL))
if choice != 'y':
exit()
# Get files list from folders directory
for folder_file in os.listdir(DIR_FOLDERS):
filename = '{}/{}'.format(DIR_FOLDERS, folder_file)
# Upload folders json
with open(filename, "r") as file:
folder_json = json.loads(file.read())
response = requests.post('{}/api/folders'.format(GRAFANA_URL), json=folder_json, headers=REQUEST_HEADERS, verify=False)
if response.status_code != 200:
logging.warning('{}: {} {}'.format(folder_file, response.status_code, response.text))
else:
logging.info("Folder {} uploaded".format(folder_file))
def upload_dashboards():
""" Upload Grafana Dashboards """
# Interactive warning
choice = input('Dashboards will be uploaded to {} Continue? [y/n]: '.format(GRAFANA_URL))
if choice != 'y':
exit()
# Get existed folders
response = requests.get('{}/api/folders'.format(GRAFANA_URL), headers=REQUEST_HEADERS, verify=False)
if response.status_code != 200:
logging.error('{} {}'.format(response.status_code, response.text))
else:
folders = json.loads(response.text)
# Get folders id and create dict
folders_ids = {}
for folder in folders:
response = requests.get('{}/api/folders/{}'.format(GRAFANA_URL, folder['uid']), headers=REQUEST_HEADERS, verify=False)
# Get folder id
folder_id = json.loads(response.text)['id']
# Update dict
folders_ids[folder['title']] = folder_id
# Get files list from dashboards directory
for dash_file in os.listdir(DIR_DASHBOARDS):
filename = '{}/{}'.format(DIR_DASHBOARDS, dash_file)
with open(filename, "r") as file:
dash_json = json.loads(file.read())
# Get dashboard's folder name
folder_name = dash_json['meta']['folderTitle']
# Update folderId key to match actual folder id (not needed for root General folder)
if folder_name != 'General':
dash_json['folderId'] = folders_ids[folder_name]
# Upload dashboard json
response = requests.post('{}/api/dashboards/db'.format(GRAFANA_URL), json=dash_json, headers=REQUEST_HEADERS, verify=False)
if response.status_code != 200:
logging.warning('{}: {} {}'.format(dash_file, response.status_code, response.text))
else:
logging.info("Dashboard {} uploaded".format(dash_file))
if __name__ == "__main__":
if not GRAFANA_URL:
logging.error('GRAFANA_URL is not set')
exit()
if not GRAFANA_TOKEN:
logging.error('GRAFANA_TOKEN is not set')
exit()
parser = argparse.ArgumentParser()
parser.add_argument('--get-datasources', help='download datasources as json files', action='store_const', const=True)
parser.add_argument('--get-folders', help='download folders as json files', action='store_const', const=True)
parser.add_argument('--get-dashboards', help='download dashasboards json files', action='store_const', const=True)
parser.add_argument('--upload-datasources', help='upload datasources from json files', action='store_const', const=True)
parser.add_argument('--upload-folders', help='upload folders from json files', action='store_const', const=True)
parser.add_argument('--upload-dashboards', help='upload dashboards from json files', action='store_const', const=True)
args = parser.parse_args()
if args.get_datasources:
get_datasources()
elif args.get_folders:
get_folders()
elif args.get_dashboards:
get_dashboards()
elif args.upload_datasources:
upload_datasources()
elif args.upload_folders:
upload_folders()
elif args.upload_dashboards:
upload_dashboards()
else:
parser.print_help()