|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# This script shows the basics of getting data out of Sysdig Cloud by crating a |
| 4 | +# very simple request that has no filter an no segmentation. |
| 5 | +# |
| 6 | +# The request queries for the average CPU across all of the instrumented hosts for |
| 7 | +# the last 10 minutes, with 1 minute data granularity |
| 8 | +# |
| 9 | + |
| 10 | +import sys |
| 11 | +sys.path.insert(0, '../') |
| 12 | +from sdcclient import SdcClient |
| 13 | + |
| 14 | +# |
| 15 | +# Parse arguments |
| 16 | +# |
| 17 | +if len(sys.argv) != 2: |
| 18 | + print 'usage: %s <sysdig-token>' % sys.argv[0] |
| 19 | + print 'You can find your token at https://app.sysdigcloud.com/#/settings/user' |
| 20 | + sys.exit(0) |
| 21 | + |
| 22 | +sdc_token = sys.argv[1] |
| 23 | + |
| 24 | +# |
| 25 | +# Instantiate the SDC client |
| 26 | +# |
| 27 | +sdclient = SdcClient(sdc_token) |
| 28 | + |
| 29 | +cpu_metric = [ |
| 30 | + {"id": "cpu.used.percent", |
| 31 | + "aggregations": { |
| 32 | + "time": "avg", |
| 33 | + "group": "avg" |
| 34 | + } |
| 35 | + }] |
| 36 | + |
| 37 | +# |
| 38 | +# First example: CPU by host name |
| 39 | +# datasource_type is not necessary since it's infered from the grouping key host.hostName |
| 40 | +# |
| 41 | +req = [{"id": "host.hostName"}] |
| 42 | +req.extend(cpu_metric) |
| 43 | +res = sdclient.get_data(req, # metrics list |
| 44 | + -600, # start_ts = 600 seconds ago |
| 45 | + 0) # end_ts = now |
| 46 | + |
| 47 | +if res[0]: |
| 48 | + data = res[1] |
| 49 | +else: |
| 50 | + print res[1] |
| 51 | + sys.exit(0) |
| 52 | + |
| 53 | +print "\n\nCPU by host:" |
| 54 | +print data |
| 55 | + |
| 56 | +# |
| 57 | +# Second example: CPU by container name |
| 58 | +# datasource_type is not necessary since it's infered from the grouping key container.name |
| 59 | +# |
| 60 | +req = [{"id": "container.name"}] |
| 61 | +req.extend(cpu_metric) |
| 62 | +res = sdclient.get_data(req, # metrics list |
| 63 | + -600, # start_ts = 600 seconds ago |
| 64 | + 0) # end_ts = now |
| 65 | + |
| 66 | +if res[0]: |
| 67 | + data = res[1] |
| 68 | +else: |
| 69 | + print res[1] |
| 70 | + sys.exit(0) |
| 71 | + |
| 72 | +print "\n\nCPU by container:" |
| 73 | +print data |
| 74 | + |
| 75 | +# |
| 76 | +# Third example: CPU average across all hosts |
| 77 | +# datasource_type is set to host since no grouping keys or filters are specified (default would be host anyway) |
| 78 | +# |
| 79 | +res = sdclient.get_data(cpu_metric, # metrics list |
| 80 | + -600, # start_ts = 600 seconds ago |
| 81 | + 0, # end_ts = now |
| 82 | + datasource_type='host') # ask data from hosts |
| 83 | + |
| 84 | +if res[0]: |
| 85 | + data = res[1] |
| 86 | +else: |
| 87 | + print res[1] |
| 88 | + sys.exit(0) |
| 89 | + |
| 90 | +print "\n\nAverage CPU across all the hosts in the infrastructure:" |
| 91 | +print data |
| 92 | + |
| 93 | +# |
| 94 | +# Third example: CPU average across all containers |
| 95 | +# datasource_type is set to container since no grouping keys or filters are specified (ovverrides the host default) |
| 96 | +# |
| 97 | +res = sdclient.get_data(cpu_metric, # metrics list |
| 98 | + -600, # start_ts = 600 seconds ago |
| 99 | + 0, # end_ts = now |
| 100 | + datasource_type='container') # ask data from containers |
| 101 | + |
| 102 | +if res[0]: |
| 103 | + data = res[1] |
| 104 | +else: |
| 105 | + print res[1] |
| 106 | + sys.exit(0) |
| 107 | + |
| 108 | +print "\n\nAverage CPU across all the containers in the infrastructure:" |
| 109 | +print data |
0 commit comments