Skip to content

Commit 6c88014

Browse files
Update list_hosts.py
Worked with engineering to add support for adding the container count with the host names and also for showing a larger number of hosts. The original script only showed about 10 hosts per environment.
1 parent d9ec67e commit 6c88014

File tree

1 file changed

+19
-37
lines changed

1 file changed

+19
-37
lines changed

examples/list_hosts.py

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
#
33
# This script shows how to leverage sysdig's data query API to obtain the list
44
# of the instrumented hosts that have been seen in the last 5 minutes.
5+
# Updated 1/24/2019 - Dale J. Rodriguez
6+
# This script will also show the container count in addition to the hostnames in a from like this: host.name 12
7+
# Where 12 is the container count and host.name is the host
58
#
6-
79
import getopt
810
import json
911
import os
1012
import sys
1113
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
1214
from sdcclient import SdcClient
13-
1415
#
1516
# Parse arguments
1617
#
@@ -20,57 +21,38 @@ def usage():
2021
print '-j|--json: Print output as json'
2122
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
2223
sys.exit(1)
23-
2424
try:
2525
opts, args = getopt.getopt(sys.argv[1:],"jd:",["json", "duration="])
2626
except getopt.GetoptError:
2727
usage()
28-
29-
duration = 600
28+
duration = 3600
3029
print_json = False
3130
for opt, arg in opts:
3231
if opt in ("-d", "--duration"):
3332
duration = int(arg)
3433
elif opt in ("-j", "--json"):
3534
print_json = True
36-
37-
if len(args) != 1:
38-
usage()
39-
4035
sdc_token = args[0]
41-
4236
#
4337
# Instantiate the SDC client
4438
#
4539
sdclient = SdcClient(sdc_token)
46-
47-
#
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.
51-
#
52-
metrics = [{"id": "host.hostName"}]
53-
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
63-
64-
#
65-
# Show the results!
66-
#
40+
metrics = [{"id": "host.hostName"}, {"id": "container.count","aggregations":{"time":"avg","group":"avg"}}]
41+
res = sdclient.get_data(
42+
metrics, -duration, 0, duration, paging={
43+
"from": 0,
44+
"to": 999
45+
})
6746
if res[0]:
68-
data = res[1]
47+
output = []
48+
data = res[1]['data']
49+
for i in range(0, len(data)):
50+
sample = data[i]
51+
metrics = sample['d']
52+
hostName = metrics[0]
53+
count = metrics[1]
54+
output.append('%s\t%d' % (hostName, count))
55+
print '\n'.join(output)
6956
else:
7057
print res[1]
7158
sys.exit(1)
72-
73-
if print_json:
74-
print json.dumps(data)
75-
else:
76-
print data

0 commit comments

Comments
 (0)