forked from sysdiglabs/sysdig-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_data_advanced.py
More file actions
executable file
·69 lines (61 loc) · 1.75 KB
/
get_data_advanced.py
File metadata and controls
executable file
·69 lines (61 loc) · 1.75 KB
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
#!/usr/bin/env python
#
# This script shows an advanced Sysdig Cloud data request that leverages
# filtering and segmentation.
#
# The request returns the last 10 minutes of CPU utilization for all of the
# containers inside the given host, with 1 minute data granularity
#
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
#
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 Cloud that we want to see the CPU
# utilization for each container separately.
{"id": "container.name"},
# The second metric we reuest 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
#
# Fire the query.
#
res = sdclient.get_data(metrics, # metrics list
-600, # start_ts = 600 seconds ago
0, # end_ts = now
60, # 1 data point per minute
filter, # The filter
'container') # The source for our metrics is the container
#
# Show the result!
#
print res[1]
if not res[0]:
sys.exit(1)