forked from sysdiglabs/sysdig-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdashboard.py
executable file
·159 lines (131 loc) · 3.47 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
156
157
158
159
#!/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 os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
from sdcclient import SdcClient
#
# 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 = SdcClient(sdc_token)
#
# Create an empty dashboard
#
dashboard_configuration = None
res = sdclient.create_dashboard(dashboard_name)
# Check the result
if res[0]:
print 'Dashboard %d created successfully' % res[1]['dashboard']['id']
dashboard_configuration = res[1]['dashboard']
else:
print res[1]
sys.exit(1)
#
# Find a dashboard by name
#
res = sdclient.find_dashboard_by(dashboard_name)
# Check the result
if res[0] and len(res[1]) > 0:
print 'Dashboard found'
dashboard_configuration = res[1][0]['dashboard']
else:
print res[1]
sys.exit(1)
#
# Add a time series
#
panel_name = 'CPU Over Time'
panel_type = 'timeSeries'
metrics = [
{ 'id': 'kubernetes.pod.name' },
{ 'id': 'cpu.used.percent', 'aggregations': { 'time': 'avg', 'group': 'avg' } }
]
scope = 'kubernetes.namespace.name = "dev" and kubernetes.replicationController.name = "cassandra"'
res = sdclient.add_dashboard_panel(dashboard_configuration, panel_name, panel_type, metrics, scope=scope)
# Check the result
if res[0]:
print 'Panel added successfully'
dashboard_configuration = res[1]['dashboard']
else:
print res[1]
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_by = { 'metric': 'cpu.used.percent', 'mode': 'desc' }
limit = 10
res = sdclient.add_dashboard_panel(dashboard_configuration, panel_name, panel_type, metrics, sort_by=sort_by, limit=limit)
# Check the result
if res[0]:
print 'Panel added successfully'
dashboard_configuration = res[1]['dashboard']
else:
print res[1]
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': 6, 'row': 1, 'size_x': 2, 'size_y': 3 }
res = sdclient.add_dashboard_panel(dashboard_configuration, panel_name, panel_type, metrics, layout=layout)
# Check the result
if res[0]:
print 'Panel added successfully'
dashboard_configuration = res[1]['dashboard']
else:
print res[1]
sys.exit(1)
#
# Remove a panel
#
res = sdclient.remove_dashboard_panel(dashboard_configuration, 'CPU')
# Check the result
if res[0]:
print 'Panel removed successfully'
dashboard_configuration = res[1]['dashboard']
else:
print res[1]
sys.exit(1)
#
# Delete the dashboard
#
res = sdclient.delete_dashboard(dashboard_configuration)
# Check the result
if res[0]:
print 'Dashboard deleted successfully'
else:
print res[1]
sys.exit(1)