Skip to content

Commit ca4927c

Browse files
committed
Add count param, minor refactoring and update docs
1 parent 45b32dd commit ca4927c

File tree

1 file changed

+52
-28
lines changed

1 file changed

+52
-28
lines changed

examples/list_hosts.py

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,93 @@
11
#!/usr/bin/env python
22
#
3-
## This script shows how to leverage sysdig's data query API to obtain the list
4-
# of the instrumented hosts that have been seen in the last hour (`duration` variable).
5-
# The output will show the container count (`container.count` metric) in addition to the hostnames (`host.hostName` tag) in a from like this:
3+
# This script shows how to leverage Sysdig data query API to obtain the list of the instrumented
4+
# hosts that have been seen in your infrastructure.
5+
# The output will show the container count (`container.count` metric) in addition to the
6+
# hostnames (`host.hostName` tag) in a format like this:
67
#
78
# host-1 12
89
# host-2 4
910
#
10-
# host-1 12
11-
# host-2 4
12-
#his: host.name 12
13-
# Where 12 is the container count and host.name is the host
11+
# where the first column is the hostname and the second column is the number of containers running
12+
# in each host.
1413
#
1514
import getopt
1615
import json
1716
import os
1817
import sys
1918
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
2019
from sdcclient import SdcClient
20+
2121
#
2222
# Parse arguments
2323
#
2424
def usage():
25-
print 'usage: %s [-j|--json] [-d|--duration <secs>] <sysdig-token>' % sys.argv[0]
26-
print '-d|--duration: List hosts seen in the last <secs> seconds'
25+
print 'usage: %s [-j|--json] [-d|--duration <secs>] [-c|--count <number>] <sysdig-token>' % sys.argv[0]
26+
print '-d|--duration: List hosts seen in the last <secs> seconds (default: 3600, ie. last hour)'
27+
print '-c|--count: Number of hosts to print (default: 100)'
2728
print '-j|--json: Print output as json'
2829
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
2930
sys.exit(1)
3031
try:
31-
opts, args = getopt.getopt(sys.argv[1:],"jd:",["json", "duration="])
32+
opts, args = getopt.getopt(sys.argv[1:],"jd:c:",["json", "duration=", "count="])
3233
except getopt.GetoptError:
3334
usage()
35+
3436
duration = 3600
37+
count = 100
3538
print_json = False
3639
for opt, arg in opts:
40+
print opt
41+
print arg
3742
if opt in ("-d", "--duration"):
3843
duration = int(arg)
44+
elif opt in ("-c", "--count"):
45+
count = int(arg)
3946
elif opt in ("-j", "--json"):
4047
print_json = True
4148
sdc_token = args[0]
42-
#
49+
50+
# Instantiate the SDC client
51+
sdclient = SdcClient(sdc_token)
52+
4353
#
4454
# Prepare the query's metrics list.
45-
# In this case, we have one tag (used for segmentation) and one metric::
46-
# host.hostName. This is a tag, to identify each item of the output.
47-
# container.count: This is the metric
55+
# In this case, we have one tag (used for segmentation) and one metric:
56+
# - host.hostName. This is a tag, to identify each item of the output
57+
# - container.count: This is the metric
4858
#
49-
# Instantiate the SDC client
50-
sdclient = SdcClient(sdc_token)
51-
metrics = [{"id": "host.hostName"}, {"id": "container.count","aggregations":{"time":"avg","group":"avg"}}]
59+
metrics = [
60+
{ "id": "host.hostName" },
61+
{ "id": "container.count", "aggregations": { "time": "avg", "group": "avg" } }
62+
]
63+
5264
res = sdclient.get_data(
53-
metrics, -duration, 0, duration, paging={
65+
metrics, # list of metrics
66+
-duration, # start time: either a unix timestamp, or a difference from "now"
67+
0, # end time: either a unix timestamp, or a difference from "now" (0 means you need "last X seconds")
68+
duration, # sampling time, ie. data granularity;
69+
# if equal to the time window span then the result will contain a single sample
70+
paging = {
5471
"from": 0,
55-
"to": 999
72+
"to": count - 1
5673
})
74+
5775
if res[0]:
58-
output = []
59-
data = res[1]['data']
60-
for i in range(0, len(data)):
61-
sample = data[i]
62-
metrics = sample['d']
63-
hostName = metrics[0]
64-
count = metrics[1]
65-
output.append('%s\t%d' % (hostName, count))
66-
print '\n'.join(output)
76+
# data fetched successfully
77+
if print_json:
78+
print json.dumps(res[1])
79+
else:
80+
data = res[1]['data']
81+
output = []
82+
for i in range(0, len(data)):
83+
sample = data[i]
84+
metrics = sample['d']
85+
hostName = metrics[0]
86+
count = metrics[1]
87+
output.append('%s\t%d' % (hostName, count))
88+
89+
print '\n'.join(output)
6790
else:
91+
# data fetch failed
6892
print res[1]
6993
sys.exit(1)

0 commit comments

Comments
 (0)