Skip to content

Commit 8983ddb

Browse files
Adityashankar KiniAdityashankar Kini
authored andcommitted
Download/restore of dashboards after some basic testing.
1 parent 74ff0c0 commit 8983ddb

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

examples/download_dashboards.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env python
2+
#
3+
# Save/restore dashboards
4+
#
5+
6+
import os
7+
import sys
8+
import zipfile
9+
import json
10+
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
11+
from sdcclient import SdcClient
12+
13+
def zipdir(path, ziph):
14+
# ziph is zipfile handle
15+
for root, dirs, files in os.walk(path):
16+
for file in files:
17+
ziph.write(os.path.join(root, file))
18+
19+
def cleanup_dir(path):
20+
if os.path.exists(path) == False:
21+
return
22+
if os.path.isdir(path) == False:
23+
print 'Provided path is not a directory'
24+
sys.exit(-1)
25+
26+
for file in os.listdir(path):
27+
file_path = os.path.join(path, file)
28+
try:
29+
if os.path.isfile(file_path):
30+
os.unlink(file_path)
31+
else:
32+
print 'Cannot clean the provided directory due to delete failure on %s' % file_path
33+
except Exception as e:
34+
print(e)
35+
os.rmdir(path)
36+
37+
#
38+
# Parse arguments
39+
#
40+
if len(sys.argv) != 3:
41+
print 'usage: %s <sysdig-token> <file-name>' % sys.argv[0]
42+
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
43+
sys.exit(1)
44+
45+
sdc_token = sys.argv[1]
46+
dashboard_state_file = sys.argv[2]
47+
sysdig_dashboard_dir = 'sysdig-dashboard-dir'
48+
49+
#
50+
# Instantiate the SDC client
51+
#
52+
sdclient = SdcClient(sdc_token, sdc_url='https://app-staging.sysdigcloud.com')
53+
54+
#
55+
# Fire the request.
56+
#
57+
res = sdclient.get_dashboards()
58+
59+
#
60+
# Show the list of dashboards
61+
#
62+
if res[0]:
63+
data = res[1]
64+
else:
65+
print res[1]
66+
sys.exit(1)
67+
68+
69+
# Clean up any state in the tmp directory
70+
cleanup_dir(sysdig_dashboard_dir)
71+
72+
# Creating sysdig dashboard directory to store dashboards
73+
if not os.path.exists(sysdig_dashboard_dir):
74+
os.makedirs(sysdig_dashboard_dir)
75+
76+
77+
for db in data['dashboards']:
78+
file_path = os.path.join(sysdig_dashboard_dir, str(db['id']))
79+
f = open(file_path, 'w')
80+
f.write(json.dumps(db))
81+
print "Name: %s, # Charts: %d" % (db['name'], len(db['items']))
82+
83+
84+
zipf = zipfile.ZipFile(dashboard_state_file, 'w', zipfile.ZIP_DEFLATED)
85+
zipdir(sysdig_dashboard_dir, zipf)
86+
zipf.close()
87+
88+
# Clean up any state in the directory
89+
cleanup_dir(sysdig_dashboard_dir)

examples/restore_dashboards.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env python
2+
#
3+
# Save/restore dashboards
4+
#
5+
6+
import os
7+
import sys
8+
import zipfile
9+
import json
10+
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
11+
from sdcclient import SdcClient
12+
13+
#
14+
# Parse arguments
15+
#
16+
if len(sys.argv) != 3:
17+
print 'usage: %s <sysdig-token> <file-name>' % sys.argv[0]
18+
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
19+
sys.exit(1)
20+
21+
sdc_token = sys.argv[1]
22+
dashboard_state_file = sys.argv[2]
23+
24+
#
25+
# Instantiate the SDC client
26+
#
27+
sdclient = SdcClient(sdc_token)
28+
29+
zipf = zipfile.ZipFile(dashboard_state_file, 'r')
30+
31+
32+
dashboard_conf_items = ['showAsType', 'filterRoot', 'linkMetrics',
33+
'singleTimeNavigation', 'gridConfiguration', 'responsive',
34+
'nodesNoiseFilter', 'compareWith', 'format', 'linksNoiseFilter',
35+
'filterProcesses', 'isLegendExpanded', 'inhertitTimeNavigation',
36+
'schema', 'sortAscending', 'mapDataLimit', 'metrics', 'filterExtNodes',
37+
'sorting', 'name', 'sourceExploreView', 'items', 'showAs', 'eventsFilter',
38+
'timeMode', 'isShared', 'sourceDrilldownView']
39+
40+
for info in zipf.infolist():
41+
data = zipf.read(info.filename)
42+
j = json.loads(data)
43+
k = {}
44+
for item in j.keys():
45+
if item in dashboard_conf_items:
46+
k[item] = j[item]
47+
48+
res = sdclient.create_dashboard_with_configuration(k)
49+
if res[0] == False:
50+
print "Dashboard creation failed for dashboard name %s with error %s" % (j['name'], res[1])

sdcclient/_client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,13 @@ def create_item(configuration):
406406
dashboards = map(create_item, filter(filter_fn, res[1]['dashboards']))
407407
return [True, dashboards]
408408

409+
def create_dashboard_with_configuration(self, configuration):
410+
res = requests.post(self.url + '/ui/dashboards', headers=self.hdrs, data=json.dumps({'dashboard': configuration}))
411+
if not self.__checkResponse(res):
412+
return [False, self.lasterr]
413+
else:
414+
return [True, res.json()]
415+
409416
def create_dashboard(self, name):
410417
dashboard_configuration = {
411418
'name': name,

0 commit comments

Comments
 (0)