Skip to content

Commit c52c600

Browse files
datasource_type documentation
1 parent a30d979 commit c52c600

File tree

3 files changed

+113
-4
lines changed

3 files changed

+113
-4
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,17 @@ A dictionary showing the details of the new dashboard.
122122
**Description**
123123
This is the method you use to export metric data. It's flexible enough to offer both time-series and table-based data export.
124124
**Arguments**
125-
- **metrics**: a list of dictionaries, specifying the metrics and grouping keys that the query will return. A metric is any of the entries that can be found in the _Metrics_ section of the Explore page in sysdig cloud. Metric entries require an _aggregations_ section specifying how to aggregate the metric across time and containers/host. A grouping key is any of the entries that can be found in the _Show_ or _Segment By_ sections of the Explore page in sysdig cloud. These entries are use to apply single or hierarchical segmentation to the returned data and don't require the aggregations section. Refer to the examples section below for ready to use code snippets.
125+
- **metrics**: a list of dictionaries, specifying the metrics and grouping keys that the query will return. A metric is any of the entries that can be found in the _Metrics_ section of the Explore page in sysdig cloud. Metric entries require an _aggregations_ section specifying how to aggregate the metric across time and containers/host. A grouping key is any of the entries that can be found in the _Show_ or _Segment By_ sections of the Explore page in sysdig cloud. These entries are used to apply single or hierarchical segmentation to the returned data and don't require the aggregations section. Refer to the examples section below for ready to use code snippets.
126126
- **start_ts**: the UTC time (in seconds) of the beginning of the data window. A negative value can be optionally used to indicate a relative time in the past from now. For example, -3600 means "one hour ago".
127127
- **end_ts**: the UTC time (in seconds) of the end of the data window, or 0 to indicate "now". A negative value can also be optionally used to indicate a relative time in the past from now. For example, -3600 means "one hour ago".
128128
- **sampling_s**: the duration of the samples that will be returned. 0 Means that the whole data will be returned as a single sample.
129129
- **filter**: a boolean expression combining Sysdig Cloud segmentation criteria defines what the query will be applied to. For example: _kubernetes.namespace.name='production' and container.image='nginx'_.
130-
- **datasource_type**: XXX gianluca edit this with an example or two.
130+
- **datasource_type**: specify the metric source for the request, can be `container` or `host`. Most metrics, for example `cpu.used.percent` or `memory.bytes.used`, are reported by hosts and containers. By default, metrics reported by the host are used, but if the request contains a grouping key in the metric list/filter which is container-specific (e.g. `container.name`), then the container source is used. In cases where grouping keys are missing or grouping keys apply to both hosts and containers (e.g. `tag.Name`), the datasource_type can be explicitly set to avoid any ambiguity and allow the user to select precisely what kind of data should be used for the request. [examples/get_data_datasource.py](examples/get_data_datasource.py) contains a few examples that should clarify the use of this argument.
131131

132132
**Success Return Value**
133133
A dictionary with the requested data. Data is organized in a list of time samples, each of which includes a UTC timestamp and a list of values, whose content and order reflect what was specified in the _metrics_ argument.
134134
**Examples**
135-
[examples/get_data_simple.py](examples/get_data_simple.py), [examples/get_data_advanced.py](examples/get_data_advanced.py), [examples/list_hosts.py](examples/list_hosts.py).
135+
[examples/get_data_simple.py](examples/get_data_simple.py), [examples/get_data_advanced.py](examples/get_data_advanced.py), [examples/list_hosts.py](examples/list_hosts.py), [examples/get_data_datasource.py](examples/get_data_datasource.py).
136136

137137
#### `get_data_retention_info(self)`
138138
**Description**

examples/get_data_datasource.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/usr/bin/env python
2+
#
3+
# This script shows the basics of getting data out of Sysdig Cloud by crating a
4+
# very simple request that has no filter an no segmentation.
5+
#
6+
# The request queries for the average CPU across all of the instrumented hosts for
7+
# the last 10 minutes, with 1 minute data granularity
8+
#
9+
10+
import sys
11+
sys.path.insert(0, '../')
12+
from sdcclient import SdcClient
13+
14+
#
15+
# Parse arguments
16+
#
17+
if len(sys.argv) != 2:
18+
print 'usage: %s <sysdig-token>' % sys.argv[0]
19+
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
20+
sys.exit(0)
21+
22+
sdc_token = sys.argv[1]
23+
24+
#
25+
# Instantiate the SDC client
26+
#
27+
sdclient = SdcClient(sdc_token)
28+
29+
cpu_metric = [
30+
{"id": "cpu.used.percent",
31+
"aggregations": {
32+
"time": "avg",
33+
"group": "avg"
34+
}
35+
}]
36+
37+
#
38+
# First example: CPU by host name
39+
# datasource_type is not necessary since it's infered from the grouping key host.hostName
40+
#
41+
req = [{"id": "host.hostName"}]
42+
req.extend(cpu_metric)
43+
res = sdclient.get_data(req, # metrics list
44+
-600, # start_ts = 600 seconds ago
45+
0) # end_ts = now
46+
47+
if res[0]:
48+
data = res[1]
49+
else:
50+
print res[1]
51+
sys.exit(0)
52+
53+
print "\n\nCPU by host:"
54+
print data
55+
56+
#
57+
# Second example: CPU by container name
58+
# datasource_type is not necessary since it's infered from the grouping key container.name
59+
#
60+
req = [{"id": "container.name"}]
61+
req.extend(cpu_metric)
62+
res = sdclient.get_data(req, # metrics list
63+
-600, # start_ts = 600 seconds ago
64+
0) # end_ts = now
65+
66+
if res[0]:
67+
data = res[1]
68+
else:
69+
print res[1]
70+
sys.exit(0)
71+
72+
print "\n\nCPU by container:"
73+
print data
74+
75+
#
76+
# Third example: CPU average across all hosts
77+
# datasource_type is set to host since no grouping keys or filters are specified (default would be host anyway)
78+
#
79+
res = sdclient.get_data(cpu_metric, # metrics list
80+
-600, # start_ts = 600 seconds ago
81+
0, # end_ts = now
82+
datasource_type='host') # ask data from hosts
83+
84+
if res[0]:
85+
data = res[1]
86+
else:
87+
print res[1]
88+
sys.exit(0)
89+
90+
print "\n\nAverage CPU across all the hosts in the infrastructure:"
91+
print data
92+
93+
#
94+
# Third example: CPU average across all containers
95+
# datasource_type is set to container since no grouping keys or filters are specified (ovverrides the host default)
96+
#
97+
res = sdclient.get_data(cpu_metric, # metrics list
98+
-600, # start_ts = 600 seconds ago
99+
0, # end_ts = now
100+
datasource_type='container') # ask data from containers
101+
102+
if res[0]:
103+
data = res[1]
104+
else:
105+
print res[1]
106+
sys.exit(0)
107+
108+
print "\n\nAverage CPU across all the containers in the infrastructure:"
109+
print data

examples/list_metrics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22
#
3-
# Print the list of dashboards.
3+
# Print the list of metrics.
44
#
55

66
import sys

0 commit comments

Comments
 (0)