-
Notifications
You must be signed in to change notification settings - Fork 44
/
get_data_advanced.py
executable file
·76 lines (66 loc) · 2.06 KB
/
get_data_advanced.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
#!/usr/bin/env python
#
# This script shows an advanced Sysdig Monitor data request that leverages
# filtering and segmentation.
#
# The request returns the last 10 minutes of CPU utilization for the 5
# busiest containers inside the given host, with 1 minute data granularity
#
import json
import sys
from sdcclient import SdcClient
#
# Parse arguments
#
if len(sys.argv) != 3:
print(('usage: %s <sysdig-token> <hostname>' % sys.argv[0]))
print('You can find your token at https://app.sysdigcloud.com/#/settings/user')
sys.exit(1)
sdc_token = sys.argv[1]
hostname = sys.argv[2]
#
# Instantiate the SDC client
#
sdclient = SdcClient(sdc_token)
#
# Prepare the metrics list.
#
metrics = [
# The first metric we request is the container name. This is a segmentation
# metric, and you can tell by the fact that we don't specify any aggregation
# criteria. This entry tells Sysdig Monitor that we want to see the CPU
# utilization for each container separately.
{"id": "container.name"},
# The second metric we request is the CPU. We aggregate it as an average.
{"id": "cpu.used.percent",
"aggregations": {
"time": "avg",
"group": "avg"
}
}
]
#
# Prepare the filter
#
filter = "host.hostName = '%s'" % hostname
#
# Paging (from and to included; by default you get from=0 to=9)
# Here we'll get the top 5.
#
paging = {"from": 0, "to": 4}
#
# Fire the query.
#
ok, res = sdclient.get_data(metrics=metrics, # List of metrics to query
start_ts=-600, # Start of query span is 600 seconds ago
end_ts=0, # End the query span now
sampling_s=60, # 1 data point per minute
filter=filter, # The filter specifying the target host
paging=paging, # Paging to limit to just the 5 most busy
datasource_type='container') # The source for our metrics is the container
#
# Show the result!
#
print((json.dumps(res, sort_keys=True, indent=4)))
if not ok:
sys.exit(1)