|
1 | 1 | #!/usr/bin/env python |
2 | 2 | # |
3 | | -# This script shows an advanced Sysdig Cloud data request that leverages |
| 3 | +# This script shows an advanced Sysdig Monitor data request that leverages |
4 | 4 | # filtering and segmentation. |
5 | 5 | # |
6 | | -# The request returns the last 10 minutes of CPU utilization for all of the |
7 | | -# containers inside the given host, with 1 minute data granularity |
| 6 | +# The request returns the last 10 minutes of CPU utilization for the 5 |
| 7 | +# busiest containers inside the given host, with 1 minute data granularity |
8 | 8 | # |
9 | 9 |
|
10 | 10 | import os |
11 | 11 | import sys |
| 12 | +import json |
12 | 13 | sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..')) |
13 | 14 | from sdcclient import SdcClient |
14 | 15 |
|
|
34 | 35 | metrics = [ |
35 | 36 | # The first metric we request is the container name. This is a segmentation |
36 | 37 | # metric, and you can tell by the fact that we don't specify any aggregation |
37 | | - # criteria. This entry tells Sysdig Cloud that we want to see the CPU |
| 38 | + # criteria. This entry tells Sysdig Monitor that we want to see the CPU |
38 | 39 | # utilization for each container separately. |
39 | 40 | {"id": "container.name"}, |
40 | | - # The second metric we reuest is the CPU. We aggregate it as an average. |
| 41 | + # The second metric we request is the CPU. We aggregate it as an average. |
41 | 42 | {"id": "cpu.used.percent", |
42 | 43 | "aggregations": { |
43 | 44 | "time": "avg", |
|
51 | 52 | # |
52 | 53 | filter = "host.hostName = '%s'" % hostname |
53 | 54 |
|
| 55 | +# |
| 56 | +# Paging (from and to included; by default you get from=0 to=9) |
| 57 | +# Here we'll get the top 5. |
| 58 | +# |
| 59 | +paging = { "from": 0, "to": 4 } |
| 60 | + |
54 | 61 | # |
55 | 62 | # Fire the query. |
56 | 63 | # |
57 | | -res = sdclient.get_data(metrics, # metrics list |
58 | | - -600, # start_ts = 600 seconds ago |
59 | | - 0, # end_ts = now |
60 | | - 60, # 1 data point per minute |
61 | | - filter, # The filter |
62 | | - 'container') # The source for our metrics is the container |
| 64 | +res = sdclient.get_data(metrics=metrics, # List of metrics to query |
| 65 | + start_ts=-600, # Start of query span is 600 seconds ago |
| 66 | + end_ts=0, # End the query span now |
| 67 | + sampling_s=60, # 1 data point per minute |
| 68 | + filter=filter, # The filter specifying the target host |
| 69 | + paging=paging, # Paging to limit to just the 5 most busy |
| 70 | + datasource_type='container') # The source for our metrics is the container |
63 | 71 |
|
64 | 72 | # |
65 | 73 | # Show the result! |
66 | 74 | # |
67 | | -print res[1] |
| 75 | +print json.dumps(res[1], sort_keys=True, indent=4) |
68 | 76 | if not res[0]: |
69 | 77 | sys.exit(1) |
0 commit comments