From e217e20dc69485bd193413ff484614ac4acb20e3 Mon Sep 17 00:00:00 2001 From: cmk-bonobo Date: Fri, 2 May 2025 16:52:07 +0200 Subject: [PATCH] parameter handling added --- rulesets/rs_traefik_params.py | 144 +++++++++++++++++++++++++++++ server_side_calls/agent_traefik.py | 59 ++++++++++++ 2 files changed, 203 insertions(+) create mode 100644 rulesets/rs_traefik_params.py create mode 100644 server_side_calls/agent_traefik.py diff --git a/rulesets/rs_traefik_params.py b/rulesets/rs_traefik_params.py new file mode 100644 index 0000000..703525b --- /dev/null +++ b/rulesets/rs_traefik_params.py @@ -0,0 +1,144 @@ +#!/usr/bin/env python3 +# pylint: disable=line-too-long +"""defines the form for typing in all needed Traefik parameters""" + +from cmk.rulesets.v1 import Help, Title, Label +from cmk.rulesets.v1.form_specs import ( + DictElement, + Dictionary, + String, + validators, + BooleanChoice, + Integer, + Password, + migrate_to_password, + InputHint, + DefaultValue, + SingleChoice, + SingleChoiceElement, +) +from cmk.rulesets.v1.rule_specs import SpecialAgent, Topic + + +def _form_spec_special_agent_traefik() -> Dictionary: + return Dictionary( + title=Title("Traefik Server Information"), + help_text=Help("Checking Traefik systems via API"), + elements={ + "hostname": DictElement( + required=True, + parameter_form=String( + title=Title("Hostname"), + help_text=Help( + "Hostname of Traefik server (bare FQDN or IP), mandatory, eg. traefik.yourdomain.tld" + ), + custom_validate=(validators.LengthInRange(min_value=1),), + prefill=InputHint("traefik.yourdomain.tld"), + ), + ), + "username": DictElement( + required=True, + parameter_form=String( + title=Title("Username"), + help_text=Help("Username for authentification, mandatory"), + custom_validate=(validators.LengthInRange(min_value=1),), + prefill=InputHint("traefikadmin"), + ), + ), + "password": DictElement( + required=True, + parameter_form=Password( + title=Title("Password"), + help_text=Help("Specify password, mandatory"), + custom_validate=(validators.LengthInRange(min_value=1),), + migrate=migrate_to_password, + ), + ), + "auth_type": DictElement( + required=True, + parameter_form=SingleChoice( + title=Title("Authentification type"), + help_text=Help("Type of authentification to use"), + elements=[ + SingleChoiceElement( + name="basic", + title=Title("Basic"), + ), + SingleChoiceElement( + name="digest", + title=Title("Digest"), + ), + ], + ), + ), + "no_http_check": DictElement( + required=False, + parameter_form=BooleanChoice( + title=Title("Disable HTTP components"), + help_text=Help( + "Activate to disable check of HTTP components, optional" + ), + label=Label("Disable HTTP components"), + ), + ), + "no_tcp_check": DictElement( + required=False, + parameter_form=BooleanChoice( + title=Title("Disable TCP components"), + help_text=Help( + "Activate to disable check of TCP components, optional" + ), + label=Label("Disable TCP components"), + ), + ), + "no_udp_check": DictElement( + required=False, + parameter_form=BooleanChoice( + title=Title("Disable UDP components"), + help_text=Help( + "Activate to disable check of UDP components, optional" + ), + label=Label("Disable UDP components"), + ), + ), + "no_https": DictElement( + required=False, + parameter_form=BooleanChoice( + title=Title("Disable HTTPS"), + help_text=Help( + "Activate to disable encryption (not recommended), optional" + ), + label=Label("Disable HTTPS"), + ), + ), + "no_cert_check": DictElement( + required=False, + parameter_form=BooleanChoice( + title=Title("Disable certificate validation"), + help_text=Help( + "Activate to disable certificate validation (not recommended), optional" + ), + label=Label("Disable certificate validation"), + ), + ), + "port": DictElement( + required=False, + parameter_form=Integer( + title=Title("Port"), + help_text=Help( + "Specify port the Traefik system ist listening on (if not 80/443), optional" + ), + prefill=DefaultValue(443), + custom_validate=(validators.NetworkPort(),), + ), + ), + }, + ) + + +rule_spec_traefik = SpecialAgent( + name="traefik", + title=Title("Traefik connection parameters"), + topic=Topic.APPLICATIONS, + parameter_form=_form_spec_special_agent_traefik, +) diff --git a/server_side_calls/agent_traefik.py b/server_side_calls/agent_traefik.py new file mode 100644 index 0000000..53ab535 --- /dev/null +++ b/server_side_calls/agent_traefik.py @@ -0,0 +1,59 @@ +"""Traefik parameter handling for the special agent""" + +from collections.abc import Iterable + +from pydantic import BaseModel + +from cmk.server_side_calls.v1 import ( + HostConfig, + Secret, + SpecialAgentCommand, + SpecialAgentConfig, +) + + +class TraefikArgs(BaseModel): + """defines all needed parameters for the special agent""" + + hostname: str + username: str + password: Secret + auth_type: str + no_http_check: bool = False + no_tcp_check: bool = False + no_udp_check: bool = False + no_cert_check: bool = False + no_https: bool = False + port: int | None = None + + +def agent_traefik_arguments( + params: TraefikArgs, _host_config: HostConfig +) -> Iterable[SpecialAgentCommand]: + """replaces the argument_thingy from the old API""" + command_arguments: list[str | Secret] = [] + command_arguments += ["--hostname", params.hostname] + command_arguments += ["--username", params.username] + command_arguments += ["--password", params.password.unsafe()] + command_arguments += ["--auth-typ", params.auth_type] + if params.no_http_check: + command_arguments.append("--no-http-check") + if params.no_tcp_check: + command_arguments.append("--no-tcp-check") + if params.no_udp_check: + command_arguments.append("--no-udp-check") + if params.no_https: + command_arguments.append("--no-https") + if params.no_cert_check: + command_arguments.append("--no-cert-check") + if params.port is not None: + command_arguments += ["--port", str(params.port)] + yield SpecialAgentCommand(command_arguments=command_arguments) + + +special_agent_traefik = SpecialAgentConfig( + # name must be the filename of the executable for the special agent (without prefix) + name="traefik", + parameter_parser=TraefikArgs.model_validate, + commands_function=agent_traefik_arguments, +)