|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# This script demonstrates how to use the `get_label_values` function to retrieve values |
| 4 | +# for a specific label and render the output as a table. |
| 5 | +# |
| 6 | + |
| 7 | +import sys |
| 8 | +from sdcclient import SdcClient |
| 9 | + |
| 10 | +def render_label_values_as_table(label_values): |
| 11 | + if not label_values: |
| 12 | + print("No values found for the specified label.") |
| 13 | + return |
| 14 | + |
| 15 | + # Calculate the maximum width for the value column |
| 16 | + max_width = max(len(value) for value in label_values) |
| 17 | + |
| 18 | + # Create a horizontal separator |
| 19 | + separator = "+" + "-" * (max_width + 2) + "+" |
| 20 | + |
| 21 | + # Create the header row |
| 22 | + header = f"| {'Value'.ljust(max_width)} |" |
| 23 | + |
| 24 | + # Create the rows for each label value |
| 25 | + rows = [f"| {value.ljust(max_width)} |" for value in label_values] |
| 26 | + |
| 27 | + # Combine everything into a table |
| 28 | + print(f"{separator}\n{header}\n{separator}\n" + "\n".join(rows) + f"\n{separator}") |
| 29 | + |
| 30 | + |
| 31 | +# |
| 32 | +# Parse arguments |
| 33 | +# |
| 34 | +if len(sys.argv) != 3: |
| 35 | + print(('usage: %s <sysdig-token> <label-name>' % sys.argv[0])) |
| 36 | + print('You can find your token at https://app.sysdigcloud.com/#/settings/user') |
| 37 | + sys.exit(1) |
| 38 | + |
| 39 | +sdc_token = sys.argv[1] |
| 40 | + |
| 41 | +sdclient = SdcClient(sdc_token) |
| 42 | + |
| 43 | +# |
| 44 | +# The label name to fetch values for |
| 45 | +# |
| 46 | +label_name = "job" |
| 47 | + |
| 48 | +# |
| 49 | +# Optional matchers to filter the label values |
| 50 | +# |
| 51 | +match = None # Replace with a list of matchers if needed |
| 52 | + |
| 53 | +# |
| 54 | +# Optional limit |
| 55 | +# |
| 56 | +limit = 10 # Set to None to disable the limit |
| 57 | + |
| 58 | +# |
| 59 | +# Fetch label values |
| 60 | +# |
| 61 | +ok, response_json = sdclient.get_label_values(label_name, match=match, limit=limit) |
| 62 | + |
| 63 | +# |
| 64 | +# Show the result |
| 65 | +# |
| 66 | +if ok: |
| 67 | + # |
| 68 | + # Read the response. The JSON looks like this: |
| 69 | + # |
| 70 | + # { |
| 71 | + # "data": [ |
| 72 | + # "fluentd-default", |
| 73 | + # "harbor-registry-default", |
| 74 | + # "k8s-cadvisor-default", |
| 75 | + # "k8s-kubelet-default", |
| 76 | + # "k8s-pods", |
| 77 | + # "k8s-pvc-default", |
| 78 | + # ], |
| 79 | + # "status": "success" |
| 80 | + # } |
| 81 | + # |
| 82 | + label_values = response_json.get("data", []) |
| 83 | + render_label_values_as_table(label_values) |
| 84 | +else: |
| 85 | + print("Error retrieving label values:", response_json) |
0 commit comments