|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# This example uses IBM Cloud IAM authentication and makes a few calls to the |
| 4 | +# Dashboard API as validation. Creates, edits and then deletes a dashboard. |
| 5 | + |
| 6 | +import os |
| 7 | +import sys |
| 8 | +sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..')) |
| 9 | +from sdcclient import IbmAuthHelper, SdMonitorClient |
| 10 | + |
| 11 | +# Parse arguments. |
| 12 | +def usage(): |
| 13 | + print('usage: %s <endpoint-url> <apikey> <instance-guid>' % sys.argv[0]) |
| 14 | + print('endpoint-url: The endpoint URL that should point to IBM Cloud') |
| 15 | + print('apikey: IBM Cloud IAM apikey that will be used to retrieve an access token') |
| 16 | + print('instance-guid: GUID of an IBM Cloud Monitoring with Sysdig instance') |
| 17 | + sys.exit(1) |
| 18 | + |
| 19 | +if len(sys.argv) != 4: |
| 20 | + usage() |
| 21 | + |
| 22 | +URL = sys.argv[1] |
| 23 | +APIKEY = sys.argv[2] |
| 24 | +GUID = sys.argv[3] |
| 25 | +DASHBOARD_NAME = 'IBM Cloud IAM with Python Client Example' |
| 26 | +PANEL_NAME = 'CPU Over Time' |
| 27 | + |
| 28 | +# Instantiate the client with an IBM Cloud auth object |
| 29 | +ibm_headers = IbmAuthHelper.get_headers(URL, APIKEY, GUID) |
| 30 | +sdclient = SdMonitorClient(sdc_url=URL, custom_headers=ibm_headers) |
| 31 | + |
| 32 | +# Create an empty dashboard |
| 33 | +ok, res = sdclient.create_dashboard(DASHBOARD_NAME) |
| 34 | + |
| 35 | +# Check the result |
| 36 | +dashboard_configuration = None |
| 37 | +if ok: |
| 38 | + print('Dashboard %d created successfully' % res['dashboard']['id']) |
| 39 | + dashboard_configuration = res['dashboard'] |
| 40 | +else: |
| 41 | + print(res) |
| 42 | + sys.exit(1) |
| 43 | + |
| 44 | +# Add a time series panel |
| 45 | +panel_type = 'timeSeries' |
| 46 | +metrics = [ |
| 47 | + {'id': 'proc.name'}, |
| 48 | + {'id': 'cpu.used.percent', 'aggregations': {'time': 'avg', 'group': 'avg'}} |
| 49 | +] |
| 50 | +ok, res = sdclient.add_dashboard_panel( |
| 51 | + dashboard_configuration, PANEL_NAME, panel_type, metrics) |
| 52 | + |
| 53 | +# Check the result |
| 54 | +if ok: |
| 55 | + print('Panel added successfully') |
| 56 | + dashboard_configuration = res['dashboard'] |
| 57 | +else: |
| 58 | + print(res) |
| 59 | + sys.exit(1) |
| 60 | + |
| 61 | +# Remove the time series panel |
| 62 | +ok, res = sdclient.remove_dashboard_panel(dashboard_configuration, PANEL_NAME) |
| 63 | + |
| 64 | +# Check the result |
| 65 | +if ok: |
| 66 | + print('Panel removed successfully') |
| 67 | + dashboard_configuration = res['dashboard'] |
| 68 | +else: |
| 69 | + print(res) |
| 70 | + sys.exit(1) |
| 71 | + |
| 72 | +# Delete the dashboard |
| 73 | +ok, res = sdclient.delete_dashboard(dashboard_configuration) |
| 74 | + |
| 75 | +# Check the result |
| 76 | +if ok: |
| 77 | + print('Dashboard deleted successfully') |
| 78 | +else: |
| 79 | + print(res) |
| 80 | + sys.exit(1) |
| 81 | + |
| 82 | +print('IBM Cloud IAM auth worked successfully!') |
0 commit comments