|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Search a dashboard by name and saving it in a zip file |
| 4 | +# |
| 5 | + |
| 6 | + |
| 7 | +import sys |
| 8 | +import os |
| 9 | +import zipfile |
| 10 | + |
| 11 | +from sdcclient import SdMonitorClient |
| 12 | + |
| 13 | + |
| 14 | +def zipdir(path, ziph): |
| 15 | + # ziph is zipfile handle |
| 16 | + for root, dirs, files in os.walk(path): |
| 17 | + for file in files: |
| 18 | + ziph.write(os.path.join(root, file)) |
| 19 | + |
| 20 | + |
| 21 | +def cleanup_dir(path): |
| 22 | + if not os.path.exists(path): |
| 23 | + return |
| 24 | + if not os.path.isdir(path): |
| 25 | + print('Provided path is not a directory') |
| 26 | + sys.exit(-1) |
| 27 | + |
| 28 | + for file in os.listdir(path): |
| 29 | + file_path = os.path.join(path, file) |
| 30 | + try: |
| 31 | + if os.path.isfile(file_path): |
| 32 | + os.unlink(file_path) |
| 33 | + else: |
| 34 | + print(('Cannot clean the provided directory due to delete failure on %s' % file_path)) |
| 35 | + except Exception as e: |
| 36 | + print(e) |
| 37 | + os.rmdir(path) |
| 38 | + |
| 39 | + |
| 40 | +# |
| 41 | +# Parse arguments |
| 42 | +# |
| 43 | +if len(sys.argv) != 4: |
| 44 | + print(('usage: %s <sysdig-token> "<dashboard-name>" <output-file-name>' % sys.argv[0])) |
| 45 | + print('You can find your token at https://app.sysdigcloud.com/#/settings/user') |
| 46 | + sys.exit(1) |
| 47 | + |
| 48 | + |
| 49 | +# Define args |
| 50 | +sdc_token = sys.argv[1] |
| 51 | +dashboard_name = sys.argv[2] |
| 52 | +dashboard_state_file = sys.argv[3] |
| 53 | +sysdig_dashboard_dir = 'sysdig-dashboard-dir' |
| 54 | + |
| 55 | +# |
| 56 | +# Instantiate the SDC client |
| 57 | +# |
| 58 | +sdclient = SdMonitorClient(sdc_token) |
| 59 | + |
| 60 | +# |
| 61 | +# Find a dashboard by name |
| 62 | +# |
| 63 | +ok, res = sdclient.find_dashboard_by(dashboard_name) |
| 64 | + |
| 65 | + |
| 66 | +# Check the result |
| 67 | +if ok and len(res) > 0: |
| 68 | + print('Dashboard found, ID: ', res[0]['dashboard']['id']) |
| 69 | + dashboard_id = res[0]['dashboard']['id'] |
| 70 | + dashboard_configuration = res[0]['dashboard'] |
| 71 | +else: |
| 72 | + print(res) |
| 73 | + sys.exit(1) |
| 74 | + |
| 75 | + |
| 76 | +# |
| 77 | +# Get Dashboard by id |
| 78 | +# |
| 79 | +ok, res = sdclient.get_dashboard(dashboard_id) |
| 80 | + |
| 81 | +# Clean up any state in the tmp directory |
| 82 | +cleanup_dir(sysdig_dashboard_dir) |
| 83 | + |
| 84 | + |
| 85 | +# Creating sysdig dashboard directory to store dashboards |
| 86 | +if not os.path.exists(sysdig_dashboard_dir): |
| 87 | + os.makedirs(sysdig_dashboard_dir) |
| 88 | + |
| 89 | + |
| 90 | +# |
| 91 | +# Check for successful retrieval and save it |
| 92 | +# |
| 93 | +if len(res['dashboard']) > 0: |
| 94 | + print('Downloading Dashboard ID: ', dashboard_id) |
| 95 | + sdclient.save_dashboard_to_file(res['dashboard'], os.path.join(sysdig_dashboard_dir, str(res['dashboard']['id']))) |
| 96 | + print('Dashboard Name: "%s"' % (res['dashboard']['name']), 'ID:', dashboard_id, 'downloaded') |
| 97 | + |
| 98 | + |
| 99 | +zipf = zipfile.ZipFile(dashboard_state_file, 'w', zipfile.ZIP_DEFLATED) |
| 100 | +zipdir(sysdig_dashboard_dir, zipf) |
| 101 | +zipf.close() |
| 102 | + |
| 103 | +# Clean up any state in the directory |
| 104 | +cleanup_dir(sysdig_dashboard_dir) |
0 commit comments