|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# This script demonstrates how to use the `get_metadata` function to retrieve metadata |
| 4 | +# about metrics and render the output as a table. |
| 5 | +# |
| 6 | + |
| 7 | +import sys |
| 8 | +from sdcclient import SdcClient |
| 9 | + |
| 10 | +def render_metadata_as_table(metadata): |
| 11 | + if not metadata: |
| 12 | + print("No metadata found.") |
| 13 | + return |
| 14 | + |
| 15 | + # Extract all metric names and their metadata |
| 16 | + rows = [] |
| 17 | + for metric, details in metadata.items(): |
| 18 | + for detail in details: |
| 19 | + rows.append({ |
| 20 | + "Metric": metric, |
| 21 | + "Type": detail.get("type", ""), |
| 22 | + "Unit": detail.get("unit", ""), |
| 23 | + "Help": detail.get("help", "") |
| 24 | + }) |
| 25 | + |
| 26 | + # Extract column names |
| 27 | + columns = ["Metric", "Type", "Unit", "Help"] |
| 28 | + |
| 29 | + # Calculate the maximum width for each column |
| 30 | + column_widths = {col: max(len(col), max(len(str(row[col])) for row in rows)) for col in columns} |
| 31 | + |
| 32 | + # Create a horizontal separator |
| 33 | + separator = "+" + "+".join("-" * (column_widths[col] + 2) for col in columns) + "+" |
| 34 | + |
| 35 | + # Create the header row |
| 36 | + header = "|" + "|".join(f" {col.ljust(column_widths[col])} " for col in columns) + "|" |
| 37 | + |
| 38 | + # Create the rows for each metadata entry |
| 39 | + table_rows = [ |
| 40 | + "|" + "|".join(f" {str(row[col]).ljust(column_widths[col])} " for col in columns) + "|" |
| 41 | + for row in rows |
| 42 | + ] |
| 43 | + |
| 44 | + # Combine everything into a table |
| 45 | + print(f"{separator}\n{header}\n{separator}\n" + "\n".join(table_rows) + f"\n{separator}") |
| 46 | + |
| 47 | + |
| 48 | +# |
| 49 | +# Parse arguments |
| 50 | +# |
| 51 | +if len(sys.argv) != 2: |
| 52 | + print(('usage: %s <sysdig-token>' % sys.argv[0])) |
| 53 | + print('You can find your token at https://app.sysdigcloud.com/#/settings/user') |
| 54 | + sys.exit(1) |
| 55 | + |
| 56 | +sdc_token = sys.argv[1] |
| 57 | + |
| 58 | +sdclient = SdcClient(sdc_token) |
| 59 | + |
| 60 | + |
| 61 | +# |
| 62 | +# Optional metric name to filter metadata |
| 63 | +# |
| 64 | +metric_name = "up" # Replace with a specific metric name if needed |
| 65 | + |
| 66 | +# |
| 67 | +# Optional limit |
| 68 | +# |
| 69 | +limit = 10 # Set to None to disable the limit |
| 70 | + |
| 71 | +# |
| 72 | +# Fetch metadata |
| 73 | +# |
| 74 | +ok, response_json = sdclient.get_metadata(metric_name=metric_name, limit=limit) |
| 75 | + |
| 76 | +# |
| 77 | +# Show the result |
| 78 | +# |
| 79 | +if ok: |
| 80 | + # |
| 81 | + # Read the response. The JSON looks like this: |
| 82 | + # |
| 83 | + # { |
| 84 | + # "data": [ |
| 85 | + # "up": [ |
| 86 | + # { |
| 87 | + # "type": "gauge", |
| 88 | + # "unit": "number", |
| 89 | + # "help": "" |
| 90 | + # } |
| 91 | + # ] |
| 92 | + # ... |
| 93 | + # ], |
| 94 | + # "status": "success" |
| 95 | + # } |
| 96 | + # |
| 97 | + metadata = response_json.get("data", []) |
| 98 | + render_metadata_as_table(metadata) |
| 99 | +else: |
| 100 | + print("Error retrieving metadata:", response_json) |
0 commit comments