From 3b3ffaf2d4042070e756073ae50519134ebefd44 Mon Sep 17 00:00:00 2001 From: "Joshua M. Boniface" Date: Fri, 29 Dec 2023 11:06:34 -0500 Subject: [PATCH] Add Prometheus file SD output to connection list Allows an administrator to easily generate a Prometheus file service discovery configuration via the CLI for all clusters they have configured. Assumes that all the various connection details are correct, and due to the limits of the file SD config does not include the scheme or SSL verification options (as these are global in Prometheus). --- client-cli/pvc/cli/cli.py | 2 ++ client-cli/pvc/cli/formatters.py | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/client-cli/pvc/cli/cli.py b/client-cli/pvc/cli/cli.py index f5441b52..d5458e03 100644 --- a/client-cli/pvc/cli/cli.py +++ b/client-cli/pvc/cli/cli.py @@ -5927,6 +5927,7 @@ def cli_connection_remove( "raw": lambda d: "\n".join([c["name"] for c in d]), "json": lambda d: jdumps(d), "json-pretty": lambda d: jdumps(d, indent=2), + "json-prometheus": cli_connection_list_format_prometheus_json, } ) def cli_connection_list( @@ -5942,6 +5943,7 @@ def cli_connection_list( "raw": Output connection names one per line. "json": Output in unformatted JSON. "json-pretty": Output in formatted JSON. + "json-prometheus": Output in Prometheus file service discovery JSON format. """ connections_config = get_store(CLI_CONFIG["store_path"]) diff --git a/client-cli/pvc/cli/formatters.py b/client-cli/pvc/cli/formatters.py index 0db6098a..8537bf44 100644 --- a/client-cli/pvc/cli/formatters.py +++ b/client-cli/pvc/cli/formatters.py @@ -846,6 +846,28 @@ def cli_connection_list_format_pretty(CLI_CONFIG, data): return "\n".join(output) +def cli_connection_list_format_prometheus_json(CLI_CONFIG, data): + """ + Format the output of cli_connection_list as Prometheus file service discovery JSON + """ + + from json import dumps + + output = list() + for connection in data: + output_obj = { + "targets": [f"{connection['address']}:{connection['port']}"], + "labels": { + "job": "pvc", + "pvc_cluster_name": f"{connection['name']}: {connection['description']}", + "pvc_cluster_id": connection["name"], + }, + } + output.append(output_obj) + + return dumps(output, indent=2) + + def cli_connection_detail_format_pretty(CLI_CONFIG, data): """ Pretty format the output of cli_connection_detail