-
Notifications
You must be signed in to change notification settings - Fork 44
/
get_data_datasource.py
executable file
·106 lines (88 loc) · 2.58 KB
/
get_data_datasource.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
#!/usr/bin/env python
#
# This script shows the use of the datasource_type argument in the get_data request,
# by providing a few clarifying examples
#
import sys
from sdcclient import SdcClient
#
# Parse arguments
#
if len(sys.argv) != 2:
print(('usage: %s <sysdig-token>' % sys.argv[0]))
print('You can find your token at https://app.sysdigcloud.com/#/settings/user')
sys.exit(1)
sdc_token = sys.argv[1]
#
# Instantiate the SDC client
#
sdclient = SdcClient(sdc_token)
cpu_metric = [
{"id": "cpu.used.percent",
"aggregations": {
"time": "avg",
"group": "avg"
}
}]
#
# First example: CPU by host name
# datasource_type is not necessary since it's infered from the grouping key host.hostName
#
req = [{"id": "host.hostName"}]
req.extend(cpu_metric)
ok, res = sdclient.get_data(req, # metrics list
-600, # start_ts = 600 seconds ago
0) # end_ts = now
if ok:
data = res
else:
print(res)
sys.exit(1)
print("\n\nCPU by host:")
print(data)
#
# Second example: CPU by container name
# datasource_type is not necessary since it's infered from the grouping key container.name
#
req = [{"id": "container.name"}]
req.extend(cpu_metric)
ok, res = sdclient.get_data(req, # metrics list
-600, # start_ts = 600 seconds ago
0) # end_ts = now
if ok:
data = res
else:
print(res)
sys.exit(1)
print("\n\nCPU by container:")
print(data)
#
# Third example: CPU average across all hosts
# datasource_type is set to host since no grouping keys or filters are specified (default would be host anyway)
#
ok, res = sdclient.get_data(cpu_metric, # metrics list
-600, # start_ts = 600 seconds ago
0, # end_ts = now
datasource_type='host') # ask data from hosts
if ok:
data = res
else:
print(res)
sys.exit(1)
print("\n\nAverage CPU across all the hosts in the infrastructure:")
print(data)
#
# Third example: CPU average across all containers
# datasource_type is set to container since no grouping keys or filters are specified (ovverrides the host default)
#
ok, res = sdclient.get_data(cpu_metric, # metrics list
-600, # start_ts = 600 seconds ago
0, # end_ts = now
datasource_type='container') # ask data from containers
if ok:
data = res
else:
print(res)
sys.exit(1)
print("\n\nAverage CPU across all the containers in the infrastructure:")
print(data)