Skip to content

Commit 33c7a16

Browse files
author
Davide Schiera
authored
Dashboards API v2 (#85)
1 parent 3dc1a8e commit 33c7a16

12 files changed

+813
-145
lines changed

examples/create_dashboard.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import os
1111
import sys
1212
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
13-
from sdcclient import SdcClient
13+
from sdcclient import SdMonitorClient
1414

1515

1616
#
@@ -42,7 +42,7 @@ def usage():
4242
#
4343
# Instantiate the SDC client
4444
#
45-
sdclient = SdcClient(sdc_token)
45+
sdclient = SdMonitorClient(sdc_token)
4646

4747
#
4848
# Create the new dashboard, applying to cassandra in production
@@ -55,7 +55,7 @@ def usage():
5555
# in Sysdig Cloud Explore page.
5656
# You can also refer to AWS tags by using "cloudProvider.tag.*" metadata or
5757
# agent tags by using "agent.tag.*" metadata
58-
dashboardFilter = "kubernetes.namespace.name = prod"
58+
dashboardFilter = 'proc.name = "cassandra"'
5959
print('Creating dashboard from view')
6060
ok, res = sdclient.create_dashboard_from_view(dashboardName, viewName, dashboardFilter)
6161
#
@@ -75,7 +75,7 @@ def usage():
7575
# Name of the dashboard to copy
7676
dashboardCopy = "Copy of {}".format(dashboardName)
7777
# Filter to apply to the new dashboard. Same as above.
78-
dashboardFilter = "kubernetes.namespace.name != prod"
78+
dashboardFilter = 'proc.name != "cassandra"'
7979

8080
print('Creating dashboard from dashboard')
8181
ok, res = sdclient.create_dashboard_from_dashboard(dashboardCopy, dashboardName, dashboardFilter)

examples/dashboard.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import os
99
import sys
1010
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
11-
from sdcclient import SdcClient
11+
from sdcclient import SdMonitorClient
1212

1313

1414
#
@@ -39,7 +39,7 @@ def usage():
3939
#
4040
# Instantiate the SDC client
4141
#
42-
sdclient = SdcClient(sdc_token)
42+
sdclient = SdMonitorClient(sdc_token)
4343

4444

4545
#
@@ -77,10 +77,10 @@ def usage():
7777
panel_name = 'CPU Over Time'
7878
panel_type = 'timeSeries'
7979
metrics = [
80-
{'id': 'kubernetes.pod.name'},
80+
{'id': 'proc.name'},
8181
{'id': 'cpu.used.percent', 'aggregations': {'time': 'avg', 'group': 'avg'}}
8282
]
83-
scope = 'kubernetes.namespace.name = "dev" and kubernetes.replicationController.name = "cassandra"'
83+
scope = 'proc.name = "cassandra"'
8484
ok, res = sdclient.add_dashboard_panel(dashboard_configuration, panel_name, panel_type, metrics, scope=scope)
8585

8686
# Check the result
@@ -101,9 +101,9 @@ def usage():
101101
{'id': 'host.hostName'},
102102
{'id': 'cpu.used.percent', 'aggregations': {'time': 'avg', 'group': 'avg'}}
103103
]
104-
sort_by = {'metric': 'cpu.used.percent', 'mode': 'desc'}
104+
sort_direction = 'desc'
105105
limit = 10
106-
ok, res = sdclient.add_dashboard_panel(dashboard_configuration, panel_name, panel_type, metrics, sort_by=sort_by, limit=limit)
106+
ok, res = sdclient.add_dashboard_panel(dashboard_configuration, panel_name, panel_type, metrics, sort_direction=sort_direction, limit=limit)
107107

108108
# Check the result
109109
if ok:
@@ -137,7 +137,7 @@ def usage():
137137
#
138138
# Remove a panel
139139
#
140-
ok, res = sdclient.remove_dashboard_panel(dashboard_configuration, 'CPU')
140+
ok, res = sdclient.remove_dashboard_panel(dashboard_configuration, 'CPU Over Time')
141141

142142
# Check the result
143143
if ok:
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env python
2+
#
3+
# Save the first user dashboard to file and then use create_dashboard_from_file()
4+
# to apply the stored dasboard again with a different filter.
5+
#
6+
import os
7+
import sys
8+
import json
9+
sys.path.insert(
10+
0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
11+
from sdcclient import SdMonitorClient
12+
from sdcclient import SdMonitorClientV1
13+
14+
#
15+
# Parse arguments
16+
#
17+
if len(sys.argv) != 5:
18+
print(
19+
'usage: %s <sysdig-v1-url> <sysdig-v1-token> <sysdig-v2-url> <sysdig-v2-token>'
20+
% sys.argv[0])
21+
print(
22+
'You can find your token at https://app.sysdigcloud.com/#/settings/user'
23+
)
24+
sys.exit(1)
25+
26+
sdc_v1_url = sys.argv[1]
27+
sdc_v1_token = sys.argv[2]
28+
sdc_v2_url = sys.argv[3]
29+
sdc_v2_token = sys.argv[4]
30+
31+
#
32+
# Instantiate the SDC client
33+
#
34+
sdclient_v2 = SdMonitorClient(sdc_v2_token, sdc_url=sdc_v2_url)
35+
sdclient_v1 = SdMonitorClientV1(sdc_v1_token, sdc_url=sdc_v1_url)
36+
37+
#
38+
# Serialize the first user dashboard to disk
39+
#
40+
ok, res = sdclient_v1.get_dashboards()
41+
42+
if not ok:
43+
print(res)
44+
sys.exit(1)
45+
46+
for dashboard in res['dashboards']:
47+
file_name = '{}.json'.format(dashboard['id'])
48+
print('Saving v1 dashboard {} to file {}...'.format(
49+
dashboard['name'], file_name))
50+
sdclient_v1.save_dashboard_to_file(dashboard, file_name)
51+
52+
print('Importing dashboard to v2...')
53+
ok, res = sdclient_v2.create_dashboard_from_file(
54+
u'import of {}'.format(dashboard['name']),
55+
file_name,
56+
None,
57+
shared=dashboard['isShared'],
58+
public=dashboard['isPublic'])
59+
60+
if ok:
61+
print('Dashboard {} imported!'.format(dashboard['name']))
62+
sdclient_v2.delete_dashboard(res['dashboard'])
63+
else:
64+
print('Dashboard {} import failed:'.format(dashboard['name']))
65+
print(res)
66+
67+
print('\n')

examples/dashboard_save_load.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import sys
88
import json
99
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
10-
from sdcclient import SdcClient
10+
from sdcclient import SdMonitorClient
1111

1212
#
1313
# Parse arguments
@@ -22,7 +22,7 @@
2222
#
2323
# Instantiate the SDC client
2424
#
25-
sdclient = SdcClient(sdc_token)
25+
sdclient = SdMonitorClient(sdc_token)
2626

2727
#
2828
# Serialize the first user dashboard to disk
@@ -34,8 +34,7 @@
3434
sys.exit(1)
3535

3636
if len(res[u'dashboards']) > 0:
37-
with open('dashboard.json', 'w') as outf:
38-
json.dump(res[u'dashboards'][0], outf)
37+
sdclient.save_dashboard_to_file(res[u'dashboards'][0], 'dashboard.json')
3938
else:
4039
print('the user has no dashboards. Exiting.')
4140
sys.exit(0)
@@ -44,7 +43,7 @@
4443
# Now create the dashboard from the file. We use a filter for the Cassandra process
4544
# as an example.
4645
#
47-
dashboardFilter = "proc.name = cassandra"
46+
dashboardFilter = 'proc.name = "cassandra"'
4847

4948
ok, res = sdclient.create_dashboard_from_file('test dasboard from file', 'dashboard.json', dashboardFilter)
5049

examples/delete_dashboard.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import os
88
import sys
99
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
10-
from sdcclient import SdcClient
10+
from sdcclient import SdMonitorClient
1111

1212

1313
#
@@ -38,7 +38,7 @@ def usage():
3838
#
3939
# Instantiate the SDC client
4040
#
41-
sdclient = SdcClient(sdc_token)
41+
sdclient = SdMonitorClient(sdc_token)
4242

4343
#
4444
# List the dashboards

examples/download_dashboards.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import zipfile
99
import json
1010
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
11-
from sdcclient import SdcClient
11+
from sdcclient import SdMonitorClient
1212

1313

1414
def zipdir(path, ziph):
@@ -52,7 +52,7 @@ def cleanup_dir(path):
5252
#
5353
# Instantiate the SDC client
5454
#
55-
sdclient = SdcClient(sdc_token, sdc_url='https://app.sysdigcloud.com')
55+
sdclient = SdMonitorClient(sdc_token)
5656

5757
#
5858
# Fire the request.
@@ -76,11 +76,9 @@ def cleanup_dir(path):
7676

7777

7878
for db in res['dashboards']:
79-
file_path = os.path.join(sysdig_dashboard_dir, str(db['id']))
80-
f = open(file_path, 'w')
81-
f.write(json.dumps(db))
82-
print("Name: %s, # Charts: %d" % (db['name'], len(db['items'])))
83-
f.close()
79+
sdclient.save_dashboard_to_file(db, os.path.join(sysdig_dashboard_dir, str(db['id'])))
80+
81+
print("Name: %s, # Charts: %d" % (db['name'], len(db['widgets'])))
8482

8583
zipf = zipfile.ZipFile(dashboard_state_file, 'w', zipfile.ZIP_DEFLATED)
8684
zipdir(sysdig_dashboard_dir, zipf)

examples/list_dashboards.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import os
77
import sys
88
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
9-
from sdcclient import SdcClient
9+
from sdcclient import SdMonitorClient
1010

1111
#
1212
# Parse arguments
@@ -21,7 +21,7 @@
2121
#
2222
# Instantiate the SDC client
2323
#
24-
sdclient = SdcClient(sdc_token)
24+
sdclient = SdMonitorClient(sdc_token)
2525

2626
#
2727
# Fire the request.
@@ -36,4 +36,4 @@
3636
sys.exit(1)
3737

3838
for db in res['dashboards']:
39-
print("Name: %s, # Charts: %d" % (db['name'], len(db['items'] if 'items' in db else [])))
39+
print("Name: %s, # Charts: %d" % (db['name'], len(db['widgets'] if 'widgets' in db else [])))

examples/restore_dashboards.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import zipfile
99
import json
1010
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
11-
from sdcclient import SdcClient
11+
from sdcclient import SdMonitorClient
1212

1313
#
1414
# Parse arguments
@@ -24,32 +24,25 @@
2424
#
2525
# Instantiate the SDC client
2626
#
27-
sdclient = SdcClient(sdc_token)
27+
sdclient = SdMonitorClient(sdc_token)
2828

2929
zipf = zipfile.ZipFile(dashboard_state_file, 'r')
3030

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', 'filterExpression']
39-
4031
for info in zipf.infolist():
4132
data = zipf.read(info.filename)
4233
try:
4334
j = json.loads(data)
4435
except ValueError:
45-
print('Non-JSON item found in ZIP: ' + info.filename + ' (skipping)')
36+
print('Invalid JSON file found in ZIP file ' + info.filename + ': skipping')
4637
continue
47-
k = {}
48-
for item in j.keys():
49-
if item in dashboard_conf_items:
50-
k[item] = j[item]
5138

52-
ok, res = sdclient.create_dashboard_with_configuration(k)
39+
#
40+
# Handle old files
41+
#
42+
if 'dashboard' in j:
43+
j = j['dashboard']
44+
45+
ok, res = sdclient.create_dashboard_with_configuration(j)
5346
if ok:
5447
print('Restored Dashboard named: ' + j['name'])
5548
else:

sdcclient/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from sdcclient._monitor import SdcClient
22
from sdcclient._monitor import SdMonitorClient
3+
from sdcclient._monitor_v1 import SdMonitorClientV1
34
from sdcclient._secure import SdSecureClient
45
from sdcclient._scanning import SdScanningClient

sdcclient/_common.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,18 @@ def _checkResponse(self, res):
3838
return False
3939

4040
if 'errors' in j:
41-
if 'message' in j['errors'][0]:
42-
self.lasterr = j['errors'][0]['message']
41+
error_msgs = []
42+
for error in j['errors']:
43+
error_msg = []
44+
if 'message' in error:
45+
error_msg.append(error['message'])
4346

44-
if 'reason' in j['errors'][0]:
45-
if self.lasterr is not None:
46-
self.lasterr += ' '
47-
else:
48-
self.lasrerr = ''
47+
if 'reason' in error:
48+
error_msg.append(error['reason'])
4949

50-
self.lasterr += j['errors'][0]['reason']
50+
error_msgs.append(': '.join(error_msg))
51+
52+
self.lasterr = '\n'.join(error_msgs)
5153
elif 'message' in j:
5254
self.lasterr = j['message']
5355
else:

0 commit comments

Comments
 (0)