|
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 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: |
6 | 7 | # |
7 | 8 | # host-1 12 |
8 | 9 | # host-2 4 |
9 | 10 | # |
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. |
14 | 13 | # |
15 | 14 | import getopt |
16 | 15 | import json |
17 | 16 | import os |
18 | 17 | import sys |
19 | 18 | sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..')) |
20 | 19 | from sdcclient import SdcClient |
| 20 | + |
21 | 21 | # |
22 | 22 | # Parse arguments |
23 | 23 | # |
24 | 24 | 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)' |
27 | 28 | print '-j|--json: Print output as json' |
28 | 29 | print 'You can find your token at https://app.sysdigcloud.com/#/settings/user' |
29 | 30 | sys.exit(1) |
30 | 31 | 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="]) |
32 | 33 | except getopt.GetoptError: |
33 | 34 | usage() |
| 35 | + |
34 | 36 | duration = 3600 |
| 37 | +count = 100 |
35 | 38 | print_json = False |
36 | 39 | for opt, arg in opts: |
| 40 | + print opt |
| 41 | + print arg |
37 | 42 | if opt in ("-d", "--duration"): |
38 | 43 | duration = int(arg) |
| 44 | + elif opt in ("-c", "--count"): |
| 45 | + count = int(arg) |
39 | 46 | elif opt in ("-j", "--json"): |
40 | 47 | print_json = True |
41 | 48 | sdc_token = args[0] |
42 | | -# |
| 49 | + |
| 50 | +# Instantiate the SDC client |
| 51 | +sdclient = SdcClient(sdc_token) |
| 52 | + |
43 | 53 | # |
44 | 54 | # 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 |
48 | 58 | # |
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 | + |
52 | 64 | 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 = { |
54 | 71 | "from": 0, |
55 | | - "to": 999 |
| 72 | + "to": count - 1 |
56 | 73 | }) |
| 74 | + |
57 | 75 | 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) |
67 | 90 | else: |
| 91 | + # data fetch failed |
68 | 92 | print res[1] |
69 | 93 | sys.exit(1) |
0 commit comments