directory structure re-arranged

This commit is contained in:
2026-03-06 12:34:05 +00:00
parent 51c5c3447d
commit 5be0232a23
21 changed files with 1577 additions and 3 deletions

View File

@@ -0,0 +1,158 @@
#!/usr/bin/env python3
# pylint: disable=missing-module-docstring, unused-argument, consider-using-f-string
# pylint: disable=missing-function-docstring, line-too-long
# import necessary elements from API version 2
from cmk.agent_based.v2 import (
AgentSection,
CheckPlugin,
Service,
State,
Metric,
Result,
DiscoveryResult,
CheckResult,
check_levels,
)
def parse_traefik_http_components(string_table):
"""the parse function"""
parsed_data = {}
for line in string_table:
if line[0] == "routers":
parsed_data["num_routers"] = int(line[1])
parsed_data["num_routers_warn"] = int(line[2])
parsed_data["num_routers_crit"] = int(line[3])
elif line[0] == "services":
parsed_data["num_services"] = int(line[1])
parsed_data["num_services_warn"] = int(line[2])
parsed_data["num_services_crit"] = int(line[3])
elif line[0] == "middlewares":
parsed_data["num_middlewares"] = int(line[1])
parsed_data["num_middlewares_warn"] = int(line[2])
parsed_data["num_middlewares_crit"] = int(line[3])
return parsed_data
def discover_traefik_http_components(section) -> DiscoveryResult:
"""the discover function"""
yield Service()
def check_traefik_http_components(params, section) -> CheckResult:
"""the check function"""
_level_type, levels_percent_not_ok = params["levels_traefik_http_components_not_ok"]
levels_min_routers = params["levels_traefik_min_http_routers"]
levels_max_routers = params["levels_traefik_max_http_routers"]
levels_min_services = params["levels_traefik_min_http_services"]
levels_max_services = params["levels_traefik_max_http_services"]
levels_min_middlewares = params["levels_traefik_min_http_middlewares"]
levels_max_middlewares = params["levels_traefik_max_http_middlewares"]
num_routers: int = section["num_routers"]
num_routers_warn: int = section["num_routers_warn"]
num_routers_crit: int = section["num_routers_crit"]
num_services: int = section["num_services"]
num_services_warn: int = section["num_services_warn"]
num_services_crit: int = section["num_services_crit"]
num_middlewares: int = section["num_middlewares"]
num_middlewares_warn: int = section["num_middlewares_warn"]
num_middlewares_crit: int = section["num_middlewares_crit"]
num_components: int = num_routers + num_services + num_middlewares
components_warn: int = num_routers_warn + num_services_warn + num_middlewares_warn
components_crit: int = num_routers_crit + num_services_crit + num_middlewares_crit
components_percent_not_ok: float = 0.0
if num_components > 0:
components_percent_not_ok = (
(components_warn + components_crit) * 100 / num_components
)
yield Metric(
name="traefik_percent_http_components_not_ok",
value=components_percent_not_ok,
levels=levels_percent_not_ok,
)
summary: str = f"Number of HTTP routers/services/middlewares: {num_routers}/{num_services}/{num_middlewares}"
details_routers: str = (
f"Routers WARN: {num_routers_warn}\nRouters CRIT: {num_routers_crit}"
)
details_services: str = (
f"Services WARN: {num_services_warn}\nServices CRIT: {num_services_crit}"
)
details_middlewares: str = f"Middlewares WARN: {num_middlewares_warn}\nMiddlewares CRIT: {num_middlewares_crit}\n\n"
details = f"{details_routers}\n\n{details_services}\n\n{details_middlewares}"
state: State = State.OK
if components_warn > 0:
state = State.WARN
if components_crit > 0:
state = State.CRIT
yield Result(
state=state,
summary=summary,
details=details,
)
yield from check_levels(
metric_name="traefik_num_http_routers",
value=num_routers,
levels_lower=levels_min_routers,
levels_upper=levels_max_routers,
label="Number of HTTP routers",
notice_only=True,
render_func=lambda v: "%.0f" % v,
)
yield from check_levels(
metric_name="traefik_num_http_services",
value=num_services,
levels_lower=levels_min_services,
levels_upper=levels_max_services,
label="Number of HTTP services",
notice_only=True,
render_func=lambda v: "%.0f" % v,
)
yield from check_levels(
metric_name="traefik_num_http_middlewares",
value=num_middlewares,
levels_lower=levels_min_middlewares,
levels_upper=levels_max_middlewares,
label="Number of HTTP middlewares",
notice_only=True,
render_func=lambda v: "%.0f" % v,
)
# create the new agent section, must begin with "agent_section_"
# and must be an instance of "AgentSection"
agent_section_traefik_http_components = AgentSection(
# "name" must exactly match the section name within the agent output
name="traefik_http_components",
# define the parse function, name is arbitrary, a good choice is to choose
# "parse_" as prefix and append the section name
parse_function=parse_traefik_http_components,
)
# create the new check plugin, must begin with "check_plugin_"
# and must be an instance of "CheckPlugin"
check_plugin_traefik_http_components = CheckPlugin(
# "name" should be the same as the corresponding section within the agent output
name="traefik_http_components",
service_name="Traefik HTTP components",
# define the discovery function, name is arbitrary, a good choice is to choose
# "discover_" as prefix and append the section name
discovery_function=discover_traefik_http_components,
# define the check function, name is arbitrary, a good choice is to choose
# "check_" as prefix and append the section name
check_function=check_traefik_http_components,
# define the default parameters
check_default_parameters={
"levels_traefik_min_http_routers": ("fixed", (10, 5)),
"levels_traefik_max_http_routers": ("fixed", (75, 100)),
"levels_traefik_min_http_services": ("fixed", (10, 5)),
"levels_traefik_max_http_services": ("fixed", (75, 100)),
"levels_traefik_min_http_middlewares": ("fixed", (5, 2)),
"levels_traefik_max_http_middlewares": ("fixed", (20, 50)),
"levels_traefik_http_components_not_ok": ("fixed", (0.5, 1.0)),
},
# connect to the ruleset where parameters can be defined
# must match the name of the ruleset exactly
check_ruleset_name="traefik_http_components",
)

View File

@@ -0,0 +1,114 @@
#!/usr/bin/env python3
# pylint: disable=missing-module-docstring, unused-argument,
# pylint: disable=missing-function-docstring, line-too-long
import datetime
import time
# import necessary elements from API version 2
from cmk.agent_based.v2 import (
AgentSection,
CheckPlugin,
Service,
State,
Metric,
Result,
DiscoveryResult,
CheckResult,
render,
)
def get_state_upper(
levels: tuple[int | float, int | float], value: int | float
) -> State:
"""returns OK/WARN/CRIT depending on the given parameters"""
warn, crit = levels
if value >= crit:
return State.CRIT
if value >= warn:
return State.WARN
return State.OK
def parse_traefik_info(string_table):
"""the parse function"""
parsed_data = {}
for line in string_table:
if line[0] == "Traefik_Version":
parsed_data["version"] = line[1]
elif line[0] == "Traefik_CodeName":
parsed_data["codename"] = line[1]
elif line[0] == "Traefik_StartDate":
parsed_data["startdate"] = line[1]
# convert timestamp into seconds since epoch
dt = datetime.datetime.fromisoformat(line[1])
parsed_data["startdate_seconds_since_epoch"] = int(dt.timestamp())
elif line[0] == "Agent_Runtime":
# convert seconds into ms
parsed_data["agent_runtime"] = float(line[1]) * 1_000
elif line[0] == "Agent_Version":
parsed_data["agent_version"] = line[1]
return parsed_data
def discover_traefik_info(section) -> DiscoveryResult:
"""the discover function"""
yield Service()
def check_traefik_info(params, section) -> CheckResult:
"""the check function"""
_level_type, levels = params["levels_traefik_agent_execution_time"]
codename: str = section["codename"]
version: str = section["version"]
startdate: str = section["startdate"]
startdate_seconds_since_epoch: int = section["startdate_seconds_since_epoch"]
# calculate runtime of Traefik in seconds
current_epoch_time: int = int(time.time())
runtime: int = current_epoch_time - startdate_seconds_since_epoch
agent_version: str = section["agent_version"]
agent_runtime: float = round(section["agent_runtime"], 1)
state: State = get_state_upper(levels=levels, value=agent_runtime)
yield Metric(
name="traefik_agent_execution_time",
value=agent_runtime,
levels=levels,
)
summary: str = f"Traefik version: {version}, code name: {codename}"
details: str = f"Traefik start date: {startdate}\nRunning since: {render.timespan(runtime)}\n\nAgent version: {agent_version}\nAgent execution time: {agent_runtime}ms"
yield Result(state=state, summary=summary, details=details)
# create the new agent section, must begin with "agent_section_"
# and must be an instance of "AgentSection"
agent_section_traefik_info = AgentSection(
# "name" must exactly match the section name within the agent output
name="traefik_info",
# define the parse function, name is arbitrary, a good choice is to choose
# "parse_" as prefix and append the section name
parse_function=parse_traefik_info,
)
# create the new check plugin, must begin with "check_plugin_"
# and must be an instance of "CheckPlugin"
check_plugin_traefik_info = CheckPlugin(
# "name" should be the same as the corresponding section within the agent output
name="traefik_info",
service_name="Traefik Info",
# define the discovery function, name is arbitrary, a good choice is to choose
# "discover_" as prefix and append the section name
discovery_function=discover_traefik_info,
# define the check function, name is arbitrary, a good choice is to choose
# "check_" as prefix and append the section name
check_function=check_traefik_info,
# define the default parameters
check_default_parameters={
"levels_traefik_agent_execution_time": ("fixed", (500.0, 750.0))
},
# connect to the ruleset where parameters can be defined
# must match the name of the ruleset exactly
check_ruleset_name="traefik_info",
)

View File

@@ -0,0 +1,158 @@
#!/usr/bin/env python3
# pylint: disable=missing-module-docstring, unused-argument, consider-using-f-string
# pylint: disable=missing-function-docstring, line-too-long
# import necessary elements from API version 2
from cmk.agent_based.v2 import (
AgentSection,
CheckPlugin,
Service,
State,
Metric,
Result,
DiscoveryResult,
CheckResult,
check_levels,
)
def parse_traefik_tcp_components(string_table):
"""the parse function"""
parsed_data = {}
for line in string_table:
if line[0] == "routers":
parsed_data["num_routers"] = int(line[1])
parsed_data["num_routers_warn"] = int(line[2])
parsed_data["num_routers_crit"] = int(line[3])
elif line[0] == "services":
parsed_data["num_services"] = int(line[1])
parsed_data["num_services_warn"] = int(line[2])
parsed_data["num_services_crit"] = int(line[3])
elif line[0] == "middlewares":
parsed_data["num_middlewares"] = int(line[1])
parsed_data["num_middlewares_warn"] = int(line[2])
parsed_data["num_middlewares_crit"] = int(line[3])
return parsed_data
def discover_traefik_tcp_components(section) -> DiscoveryResult:
"""the discover function"""
yield Service()
def check_traefik_tcp_components(params, section) -> CheckResult:
"""the check function"""
_level_type, levels_percent_not_ok = params["levels_traefik_tcp_components_not_ok"]
levels_min_routers = params["levels_traefik_min_tcp_routers"]
levels_max_routers = params["levels_traefik_max_tcp_routers"]
levels_min_services = params["levels_traefik_min_tcp_services"]
levels_max_services = params["levels_traefik_max_tcp_services"]
levels_min_middlewares = params["levels_traefik_min_tcp_middlewares"]
levels_max_middlewares = params["levels_traefik_max_tcp_middlewares"]
num_routers: int = section["num_routers"]
num_routers_warn: int = section["num_routers_warn"]
num_routers_crit: int = section["num_routers_crit"]
num_services: int = section["num_services"]
num_services_warn: int = section["num_services_warn"]
num_services_crit: int = section["num_services_crit"]
num_middlewares: int = section["num_middlewares"]
num_middlewares_warn: int = section["num_middlewares_warn"]
num_middlewares_crit: int = section["num_middlewares_crit"]
num_components: int = num_routers + num_services + num_middlewares
components_warn: int = num_routers_warn + num_services_warn + num_middlewares_warn
components_crit: int = num_routers_crit + num_services_crit + num_middlewares_crit
components_percent_not_ok: float = 0.0
if num_components > 0:
components_percent_not_ok = (
(components_warn + components_crit) * 100 / num_components
)
yield Metric(
name="traefik_percent_tcp_components_not_ok",
value=components_percent_not_ok,
levels=levels_percent_not_ok,
)
summary: str = f"Number of TCP routers/services/middlewares: {num_routers}/{num_services}/{num_middlewares}"
details_routers: str = (
f"Routers WARN: {num_routers_warn}\nRouters CRIT: {num_routers_crit}"
)
details_services: str = (
f"Services WARN: {num_services_warn}\nServices CRIT: {num_services_crit}"
)
details_middlewares: str = f"Middlewares WARN: {num_middlewares_warn}\nMiddlewares CRIT: {num_middlewares_crit}\n\n"
details = f"{details_routers}\n\n{details_services}\n\n{details_middlewares}"
state: State = State.OK
if components_warn > 0:
state = State.WARN
if components_crit > 0:
state = State.CRIT
yield Result(
state=state,
summary=summary,
details=details,
)
yield from check_levels(
metric_name="traefik_num_tcp_routers",
value=num_routers,
levels_lower=levels_min_routers,
levels_upper=levels_max_routers,
label="Number of TCP routers",
notice_only=True,
render_func=lambda v: "%.0f" % v,
)
yield from check_levels(
metric_name="traefik_num_tcp_services",
value=num_services,
levels_lower=levels_min_services,
levels_upper=levels_max_services,
label="Number of TCP services",
notice_only=True,
render_func=lambda v: "%.0f" % v,
)
yield from check_levels(
metric_name="traefik_num_tcp_middlewares",
value=num_middlewares,
levels_lower=levels_min_middlewares,
levels_upper=levels_max_middlewares,
label="Number of TCP middlewares",
notice_only=True,
render_func=lambda v: "%.0f" % v,
)
# create the new agent section, must begin with "agent_section_"
# and must be an instance of "AgentSection"
agent_section_traefik_tcp_components = AgentSection(
# "name" must exactly match the section name within the agent output
name="traefik_tcp_components",
# define the parse function, name is arbitrary, a good choice is to choose
# "parse_" as prefix and append the section name
parse_function=parse_traefik_tcp_components,
)
# create the new check plugin, must begin with "check_plugin_"
# and must be an instance of "CheckPlugin"
check_plugin_traefik_tcp_components = CheckPlugin(
# "name" should be the same as the corresponding section within the agent output
name="traefik_tcp_components",
service_name="Traefik TCP components",
# define the discovery function, name is arbitrary, a good choice is to choose
# "discover_" as prefix and append the section name
discovery_function=discover_traefik_tcp_components,
# define the check function, name is arbitrary, a good choice is to choose
# "check_" as prefix and append the section name
check_function=check_traefik_tcp_components,
# define the default parameters
check_default_parameters={
"levels_traefik_min_tcp_routers": ("fixed", (0, 0)),
"levels_traefik_max_tcp_routers": ("fixed", (25, 50)),
"levels_traefik_min_tcp_services": ("fixed", (0, 0)),
"levels_traefik_max_tcp_services": ("fixed", (25, 50)),
"levels_traefik_min_tcp_middlewares": ("fixed", (0, 0)),
"levels_traefik_max_tcp_middlewares": ("fixed", (25, 50)),
"levels_traefik_tcp_components_not_ok": ("fixed", (0.5, 1.0)),
},
# connect to the ruleset where parameters can be defined
# must match the name of the ruleset exactly
check_ruleset_name="traefik_tcp_components",
)

View File

@@ -0,0 +1,137 @@
#!/usr/bin/env python3
# pylint: disable=missing-module-docstring, unused-argument, consider-using-f-string
# pylint: disable=missing-function-docstring, line-too-long
# import necessary elements from API version 2
from cmk.agent_based.v2 import (
AgentSection,
CheckPlugin,
Service,
State,
Metric,
Result,
DiscoveryResult,
CheckResult,
check_levels,
)
def parse_traefik_udp_components(string_table):
"""the parse function"""
parsed_data = {}
for line in string_table:
if line[0] == "routers":
parsed_data["num_routers"] = int(line[1])
parsed_data["num_routers_warn"] = int(line[2])
parsed_data["num_routers_crit"] = int(line[3])
elif line[0] == "services":
parsed_data["num_services"] = int(line[1])
parsed_data["num_services_warn"] = int(line[2])
parsed_data["num_services_crit"] = int(line[3])
return parsed_data
def discover_traefik_udp_components(section) -> DiscoveryResult:
"""the discover function"""
yield Service()
def check_traefik_udp_components(params, section) -> CheckResult:
"""the check function"""
_level_type, levels_percent_not_ok = params["levels_traefik_udp_components_not_ok"]
levels_min_routers = params["levels_traefik_min_udp_routers"]
levels_max_routers = params["levels_traefik_max_udp_routers"]
levels_min_services = params["levels_traefik_min_udp_services"]
levels_max_services = params["levels_traefik_max_udp_services"]
num_routers: int = section["num_routers"]
num_routers_warn: int = section["num_routers_warn"]
num_routers_crit: int = section["num_routers_crit"]
num_services: int = section["num_services"]
num_services_warn: int = section["num_services_warn"]
num_services_crit: int = section["num_services_crit"]
num_components: int = num_routers + num_services
components_warn: int = num_routers_warn + num_services_warn
components_crit: int = num_routers_crit + num_services_crit
components_percent_not_ok: float = 0.0
if num_components > 0:
components_percent_not_ok = (
(components_warn + components_crit) * 100 / num_components
)
yield Metric(
name="traefik_percent_udp_components_not_ok",
value=components_percent_not_ok,
levels=levels_percent_not_ok,
)
summary: str = f"Number of UDP routers/services: {num_routers}/{num_services}"
details_routers: str = (
f"Routers WARN: {num_routers_warn}\nRouters CRIT: {num_routers_crit}"
)
details_services: str = (
f"Services WARN: {num_services_warn}\nServices CRIT: {num_services_crit}"
)
details = f"{details_routers}\n\n{details_services}"
state: State = State.OK
if components_warn > 0:
state = State.WARN
if components_crit > 0:
state = State.CRIT
yield Result(
state=state,
summary=summary,
details=details,
)
yield from check_levels(
metric_name="traefik_num_udp_routers",
value=num_routers,
levels_lower=levels_min_routers,
levels_upper=levels_max_routers,
label="Number of UDP routers",
notice_only=True,
render_func=lambda v: "%.0f" % v,
)
yield from check_levels(
metric_name="traefik_num_udp_services",
value=num_services,
levels_lower=levels_min_services,
levels_upper=levels_max_services,
label="Number of UDP services",
notice_only=True,
render_func=lambda v: "%.0f" % v,
)
# create the new agent section, must begin with "agent_section_"
# and must be an instance of "AgentSection"
agent_section_traefik_udp_components = AgentSection(
# "name" must exactly match the section name within the agent output
name="traefik_udp_components",
# define the parse function, name is arbitrary, a good choice is to choose
# "parse_" as prefix and append the section name
parse_function=parse_traefik_udp_components,
)
# create the new check plugin, must begin with "check_plugin_"
# and must be an instance of "CheckPlugin"
check_plugin_traefik_udp_components = CheckPlugin(
# "name" should be the same as the corresponding section within the agent output
name="traefik_udp_components",
service_name="Traefik UDP components",
# define the discovery function, name is arbitrary, a good choice is to choose
# "discover_" as prefix and append the section name
discovery_function=discover_traefik_udp_components,
# define the check function, name is arbitrary, a good choice is to choose
# "check_" as prefix and append the section name
check_function=check_traefik_udp_components,
# define the default parameters
check_default_parameters={
"levels_traefik_min_udp_routers": ("fixed", (0, 0)),
"levels_traefik_max_udp_routers": ("fixed", (25, 50)),
"levels_traefik_min_udp_services": ("fixed", (0, 0)),
"levels_traefik_max_udp_services": ("fixed", (25, 50)),
"levels_traefik_udp_components_not_ok": ("fixed", (0.5, 1.0)),
},
# connect to the ruleset where parameters can be defined
# must match the name of the ruleset exactly
check_ruleset_name="traefik_udp_components",
)