|
1 | 1 | #!/usr/bin/env python |
2 | 2 | # |
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. |
5 | 13 | # |
6 | | - |
7 | 14 | import getopt |
8 | 15 | import json |
9 | 16 | import os |
|
15 | 22 | # Parse arguments |
16 | 23 | # |
17 | 24 | 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)' |
20 | 28 | print '-j|--json: Print output as json' |
21 | 29 | print 'You can find your token at https://app.sysdigcloud.com/#/settings/user' |
22 | 30 | sys.exit(1) |
23 | | - |
24 | 31 | 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="]) |
26 | 33 | except getopt.GetoptError: |
27 | 34 | usage() |
28 | 35 |
|
29 | | -duration = 600 |
| 36 | +duration = 3600 |
| 37 | +count = 100 |
30 | 38 | print_json = False |
31 | 39 | for opt, arg in opts: |
32 | 40 | if opt in ("-d", "--duration"): |
33 | 41 | duration = int(arg) |
| 42 | + elif opt in ("-c", "--count"): |
| 43 | + count = int(arg) |
34 | 44 | elif opt in ("-j", "--json"): |
35 | 45 | print_json = True |
36 | | - |
37 | | -if len(args) != 1: |
38 | | - usage() |
39 | | - |
40 | 46 | sdc_token = args[0] |
41 | 47 |
|
42 | | -# |
43 | 48 | # Instantiate the SDC client |
44 | | -# |
45 | 49 | sdclient = SdcClient(sdc_token) |
46 | 50 |
|
47 | 51 | # |
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 |
51 | 56 | # |
52 | | -metrics = [{"id": "host.hostName"}] |
| 57 | +metrics = [ |
| 58 | + { "id": "host.hostName" }, |
| 59 | + { "id": "container.count", "aggregations": { "time": "avg", "group": "avg" } } |
| 60 | +] |
53 | 61 |
|
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 | + }) |
63 | 72 |
|
64 | | -# |
65 | | -# Show the results! |
66 | | -# |
67 | 73 | 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) |
69 | 88 | else: |
| 89 | + # data fetch failed |
70 | 90 | print res[1] |
71 | 91 | sys.exit(1) |
72 | | - |
73 | | -if print_json: |
74 | | - print json.dumps(data) |
75 | | -else: |
76 | | - print data |
|
0 commit comments