Skip to content

Commit d775904

Browse files
Merge pull request #71 from draios/dalejrodriguez-patch-1
Update list_hosts.py
2 parents d9ec67e + 59e611d commit d775904

File tree

1 file changed

+51
-36
lines changed

1 file changed

+51
-36
lines changed

examples/list_hosts.py

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
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 5 minutes.
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:
7+
#
8+
# host-1 12
9+
# host-2 4
10+
#
11+
# where the first column is the hostname and the second column is the number of containers running
12+
# in each host.
513
#
6-
714
import getopt
815
import json
916
import os
@@ -15,62 +22,70 @@
1522
# Parse arguments
1623
#
1724
def usage():
18-
print 'usage: %s [-j|--json] [-d|--duration <secs>] <sysdig-token>' % sys.argv[0]
19-
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)'
2028
print '-j|--json: Print output as json'
2129
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
2230
sys.exit(1)
23-
2431
try:
25-
opts, args = getopt.getopt(sys.argv[1:],"jd:",["json", "duration="])
32+
opts, args = getopt.getopt(sys.argv[1:],"jd:c:",["json", "duration=", "count="])
2633
except getopt.GetoptError:
2734
usage()
2835

29-
duration = 600
36+
duration = 3600
37+
count = 100
3038
print_json = False
3139
for opt, arg in opts:
3240
if opt in ("-d", "--duration"):
3341
duration = int(arg)
42+
elif opt in ("-c", "--count"):
43+
count = int(arg)
3444
elif opt in ("-j", "--json"):
3545
print_json = True
36-
37-
if len(args) != 1:
38-
usage()
39-
4046
sdc_token = args[0]
4147

42-
#
4348
# Instantiate the SDC client
44-
#
4549
sdclient = SdcClient(sdc_token)
4650

4751
#
48-
# Prepare the query's metrics list. In this case, we have just one metric:
49-
# host.hostName. This is a 'key' metric, and we don't include any number metric.
50-
# Essentially, we create an 'enumeration' of hostnames.
52+
# Prepare the query's metrics list.
53+
# In this case, we have one tag (used for segmentation) and one metric:
54+
# - host.hostName. This is a tag, to identify each item of the output
55+
# - container.count: This is the metric
5156
#
52-
metrics = [{"id": "host.hostName"}]
57+
metrics = [
58+
{ "id": "host.hostName" },
59+
{ "id": "container.count", "aggregations": { "time": "avg", "group": "avg" } }
60+
]
5361

54-
#
55-
# Fire the query.
56-
# Note: there's no sampling time. This means that we're requesting the result to
57-
# come as a single sample.
58-
#
59-
res = sdclient.get_data(metrics, # metrics list
60-
-duration, # cover the last duration seconds...
61-
0, # ... ending now...
62-
duration) # ... with just one durations sample
62+
res = sdclient.get_data(
63+
metrics, # list of metrics
64+
-duration, # start time: either a unix timestamp, or a difference from "now"
65+
0, # end time: either a unix timestamp, or a difference from "now" (0 means you need "last X seconds")
66+
duration, # sampling time, ie. data granularity;
67+
# if equal to the time window span then the result will contain a single sample
68+
paging = {
69+
"from": 0,
70+
"to": count - 1
71+
})
6372

64-
#
65-
# Show the results!
66-
#
6773
if res[0]:
68-
data = res[1]
74+
# data fetched successfully
75+
if print_json:
76+
print json.dumps(res[1])
77+
else:
78+
data = res[1]['data']
79+
output = []
80+
for i in range(0, len(data)):
81+
sample = data[i]
82+
metrics = sample['d']
83+
hostName = metrics[0]
84+
count = metrics[1]
85+
output.append('%s\t%d' % (hostName, count))
86+
87+
print '\n'.join(output)
6988
else:
89+
# data fetch failed
7090
print res[1]
7191
sys.exit(1)
72-
73-
if print_json:
74-
print json.dumps(data)
75-
else:
76-
print data

0 commit comments

Comments
 (0)