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).
This commit is contained in:
Joshua Boniface 2023-12-29 11:06:34 -05:00
parent 2309b9dcf0
commit 3b3ffaf2d4
2 changed files with 24 additions and 0 deletions

View File

@ -5927,6 +5927,7 @@ def cli_connection_remove(
"raw": lambda d: "\n".join([c["name"] for c in d]), "raw": lambda d: "\n".join([c["name"] for c in d]),
"json": lambda d: jdumps(d), "json": lambda d: jdumps(d),
"json-pretty": lambda d: jdumps(d, indent=2), "json-pretty": lambda d: jdumps(d, indent=2),
"json-prometheus": cli_connection_list_format_prometheus_json,
} }
) )
def cli_connection_list( def cli_connection_list(
@ -5942,6 +5943,7 @@ def cli_connection_list(
"raw": Output connection names one per line. "raw": Output connection names one per line.
"json": Output in unformatted JSON. "json": Output in unformatted JSON.
"json-pretty": Output in formatted 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"]) connections_config = get_store(CLI_CONFIG["store_path"])

View File

@ -846,6 +846,28 @@ def cli_connection_list_format_pretty(CLI_CONFIG, data):
return "\n".join(output) 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): def cli_connection_detail_format_pretty(CLI_CONFIG, data):
""" """
Pretty format the output of cli_connection_detail Pretty format the output of cli_connection_detail