-
Notifications
You must be signed in to change notification settings - Fork 44
/
dashboard.py
executable file
·155 lines (131 loc) · 3.38 KB
/
dashboard.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
#!/usr/bin/env python
#
# This example shows various functions to create a new dashboard or find an existing on,
# edit the content, and then delete it.
#
import getopt
import sys
from sdcclient import SdMonitorClient
#
# Parse arguments
#
def usage():
print(('usage: %s [-d|--dashboard <name>] <sysdig-token>' % sys.argv[0]))
print('-d|--dashboard: Set name of dashboard to create')
print('You can find your token at https://app.sysdigcloud.com/#/settings/user')
sys.exit(1)
try:
opts, args = getopt.getopt(sys.argv[1:], "d:", ["dashboard="])
except getopt.GetoptError:
usage()
dashboard_name = "My Dashboard"
for opt, arg in opts:
if opt in ("-d", "--dashboard"):
dashboard_name = arg
if len(args) != 1:
usage()
sdc_token = args[0]
#
# Instantiate the SDC client
#
sdclient = SdMonitorClient(sdc_token)
#
# Create an empty dashboard
#
dashboard_configuration = None
ok, res = sdclient.create_dashboard(dashboard_name)
# Check the result
if ok:
print(('Dashboard %d created successfully' % res['dashboard']['id']))
dashboard_configuration = res['dashboard']
else:
print(res)
sys.exit(1)
#
# Find a dashboard by name
#
ok, res = sdclient.find_dashboard_by(dashboard_name)
# Check the result
if ok and len(res) > 0:
print('Dashboard found')
dashboard_configuration = res[0]['dashboard']
else:
print(res)
sys.exit(1)
#
# Add a time series
#
panel_name = 'CPU Over Time'
panel_type = 'timeSeries'
metrics = [
{'id': 'proc.name'},
{'id': 'cpu.used.percent', 'aggregations': {'time': 'avg', 'group': 'avg'}}
]
scope = 'proc.name = "cassandra"'
ok, res = sdclient.add_dashboard_panel(dashboard_configuration, panel_name, panel_type, metrics, scope=scope)
# Check the result
if ok:
print('Panel added successfully')
dashboard_configuration = res['dashboard']
else:
print(res)
sys.exit(1)
#
# Add a top bar chart
#
panel_name = 'CPU by host'
panel_type = 'top'
metrics = [
{'id': 'host.hostName'},
{'id': 'cpu.used.percent', 'aggregations': {'time': 'avg', 'group': 'avg'}}
]
sort_direction = 'desc'
limit = 10
layout = {'col': 1, 'row': 7, 'size_x': 12, 'size_y': 6}
ok, res = sdclient.add_dashboard_panel(dashboard_configuration, panel_name, panel_type, metrics,
sort_direction=sort_direction, limit=limit, layout=layout)
# Check the result
if ok:
print('Panel added successfully')
dashboard_configuration = res['dashboard']
else:
print(res)
sys.exit(1)
#
# Add a number panel
#
panel_name = 'CPU'
panel_type = 'number'
metrics = [
{'id': 'cpu.used.percent', 'aggregations': {'time': 'avg', 'group': 'avg'}}
]
layout = {'col': 1, 'row': 13, 'size_x': 12, 'size_y': 6}
ok, res = sdclient.add_dashboard_panel(dashboard_configuration, panel_name, panel_type, metrics, layout=layout)
# Check the result
if ok:
print('Panel added successfully')
dashboard_configuration = res['dashboard']
else:
print(res)
sys.exit(1)
#
# Remove a panel
#
ok, res = sdclient.remove_dashboard_panel(dashboard_configuration, 'CPU Over Time')
# 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)