-
Notifications
You must be signed in to change notification settings - Fork 44
/
dashboard_ibm_cloud.py
executable file
·83 lines (65 loc) · 2.16 KB
/
dashboard_ibm_cloud.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
#!/usr/bin/env python
# This example uses IBM Cloud IAM authentication and makes a few calls to the
# Dashboard API as validation. Creates, edits and then deletes a dashboard.
import sys
from sdcclient import IbmAuthHelper, SdMonitorClient
# Parse arguments.
def usage():
print(('usage: %s <endpoint-url> <apikey> <instance-guid>' % sys.argv[0]))
print('endpoint-url: The endpoint URL that should point to IBM Cloud')
print('apikey: IBM Cloud IAM apikey that will be used to retrieve an access token')
print('instance-guid: GUID of an IBM Cloud Monitoring with Sysdig instance')
sys.exit(1)
if len(sys.argv) != 4:
usage()
URL = sys.argv[1]
APIKEY = sys.argv[2]
GUID = sys.argv[3]
DASHBOARD_NAME = 'IBM Cloud IAM with Python Client Example'
PANEL_NAME = 'CPU Over Time'
# Instantiate the client with an IBM Cloud auth object
ibm_headers = IbmAuthHelper.get_headers(URL, APIKEY, GUID)
sdclient = SdMonitorClient(sdc_url=URL, custom_headers=ibm_headers)
# Create an empty dashboard
ok, res = sdclient.create_dashboard(DASHBOARD_NAME)
# Check the result
dashboard_configuration = None
if ok:
print(('Dashboard %d created successfully' % res['dashboard']['id']))
dashboard_configuration = res['dashboard']
else:
print(res)
sys.exit(1)
# Add a time series panel
panel_type = 'timeSeries'
metrics = [
{'id': 'proc.name'},
{'id': 'cpu.used.percent', 'aggregations': {'time': 'avg', 'group': 'avg'}}
]
ok, res = sdclient.add_dashboard_panel(
dashboard_configuration, PANEL_NAME, panel_type, metrics)
# Check the result
if ok:
print('Panel added successfully')
dashboard_configuration = res['dashboard']
else:
print(res)
sys.exit(1)
# Remove the time series panel
ok, res = sdclient.remove_dashboard_panel(dashboard_configuration, PANEL_NAME)
# Check the result
if ok:
print('Panel removed successfully')
dashboard_configuration = res['dashboard']
else:
print(res)
sys.exit(1)
# Delete the dashboard
ok, res = sdclient.delete_dashboard(dashboard_configuration)
# Check the result
if ok:
print('Dashboard deleted successfully')
else:
print(res)
sys.exit(1)
print('IBM Cloud IAM auth worked successfully!')