Mypy fix
Stop using ctx.parent.params to get the verbose and timeout parameters parsed in main and use ctx.obj instead. ctx.parent.params is typed as Optional[Context] which forces us to test if it's NULL before using it. It's useless in our case because we know it's not empty and the resulting code is ugly. The mypy ierror. Item "None" of "Optional[Context]" has an attribute "params" [union-attr]
This commit is contained in:
parent
ca95250f2e
commit
6c696a03ee
|
@ -28,7 +28,7 @@ from .node import (
|
||||||
NodeTLHasChanged,
|
NodeTLHasChanged,
|
||||||
NodeTLHasChangedSummary,
|
NodeTLHasChangedSummary,
|
||||||
)
|
)
|
||||||
from .types import ConnectionInfo
|
from .types import ConnectionInfo, Parameters
|
||||||
from .convert import size_to_byte
|
from .convert import size_to_byte
|
||||||
|
|
||||||
|
|
||||||
|
@ -137,9 +137,19 @@ def main(
|
||||||
timeout: int,
|
timeout: int,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Nagios plugin for patroni."""
|
"""Nagios plugin for patroni."""
|
||||||
ctx.obj = ConnectionInfo(endpoints, cert_file, key_file, ca_file)
|
|
||||||
# FIXME Not all "is/has" services have the same return code for ok. Check if it's ok
|
# FIXME Not all "is/has" services have the same return code for ok. Check if it's ok
|
||||||
|
|
||||||
|
# We use this to pass parameters instead of ctx.parent.params because the
|
||||||
|
# latter is typed as Optional[Context] and mypy complains with the following
|
||||||
|
# error unless we test if ctx.parent is none which looked ugly.
|
||||||
|
#
|
||||||
|
# error: Item "None" of "Optional[Context]" has an attribute "params" [union-attr]
|
||||||
|
ctx.obj = Parameters(
|
||||||
|
ConnectionInfo(endpoints, cert_file, key_file, ca_file),
|
||||||
|
timeout,
|
||||||
|
verbose,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@main.command(name="cluster_node_count") # required otherwise _ are converted to -
|
@main.command(name="cluster_node_count") # required otherwise _ are converted to -
|
||||||
@click.option(
|
@click.option(
|
||||||
|
@ -192,7 +202,7 @@ def cluster_node_count(
|
||||||
"""
|
"""
|
||||||
check = nagiosplugin.Check()
|
check = nagiosplugin.Check()
|
||||||
check.add(
|
check.add(
|
||||||
ClusterNodeCount(ctx.obj),
|
ClusterNodeCount(ctx.obj.connection_info),
|
||||||
nagiosplugin.ScalarContext(
|
nagiosplugin.ScalarContext(
|
||||||
"members",
|
"members",
|
||||||
warning,
|
warning,
|
||||||
|
@ -206,9 +216,7 @@ def cluster_node_count(
|
||||||
nagiosplugin.ScalarContext("members_roles"),
|
nagiosplugin.ScalarContext("members_roles"),
|
||||||
nagiosplugin.ScalarContext("members_statuses"),
|
nagiosplugin.ScalarContext("members_statuses"),
|
||||||
)
|
)
|
||||||
check.main(
|
check.main(verbose=ctx.obj.verbose, timeout=ctx.obj.timeout)
|
||||||
verbose=ctx.parent.params["verbose"], timeout=ctx.parent.params["timeout"]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@main.command(name="cluster_has_leader")
|
@main.command(name="cluster_has_leader")
|
||||||
|
@ -227,13 +235,11 @@ def cluster_has_leader(ctx: click.Context) -> None:
|
||||||
# FIXME: Manage primary or standby leader in the same place ?
|
# FIXME: Manage primary or standby leader in the same place ?
|
||||||
check = nagiosplugin.Check()
|
check = nagiosplugin.Check()
|
||||||
check.add(
|
check.add(
|
||||||
ClusterHasLeader(ctx.obj),
|
ClusterHasLeader(ctx.obj.connection_info),
|
||||||
nagiosplugin.ScalarContext("has_leader", None, "@0:0"),
|
nagiosplugin.ScalarContext("has_leader", None, "@0:0"),
|
||||||
ClusterHasLeaderSummary(),
|
ClusterHasLeaderSummary(),
|
||||||
)
|
)
|
||||||
check.main(
|
check.main(verbose=ctx.obj.verbose, timeout=ctx.obj.timeout)
|
||||||
verbose=ctx.parent.params["verbose"], timeout=ctx.parent.params["timeout"]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@main.command(name="cluster_has_replica")
|
@main.command(name="cluster_has_replica")
|
||||||
|
@ -278,7 +284,7 @@ def cluster_has_replica(
|
||||||
tmax_lag = size_to_byte(max_lag) if max_lag is not None else None
|
tmax_lag = size_to_byte(max_lag) if max_lag is not None else None
|
||||||
check = nagiosplugin.Check()
|
check = nagiosplugin.Check()
|
||||||
check.add(
|
check.add(
|
||||||
ClusterHasReplica(ctx.obj, tmax_lag),
|
ClusterHasReplica(ctx.obj.connection_info, tmax_lag),
|
||||||
nagiosplugin.ScalarContext(
|
nagiosplugin.ScalarContext(
|
||||||
"healthy_replica",
|
"healthy_replica",
|
||||||
warning,
|
warning,
|
||||||
|
@ -287,9 +293,7 @@ def cluster_has_replica(
|
||||||
nagiosplugin.ScalarContext("unhealthy_replica"),
|
nagiosplugin.ScalarContext("unhealthy_replica"),
|
||||||
nagiosplugin.ScalarContext("replica_lag"),
|
nagiosplugin.ScalarContext("replica_lag"),
|
||||||
)
|
)
|
||||||
check.main(
|
check.main(verbose=ctx.obj.verbose, timeout=ctx.obj.timeout)
|
||||||
verbose=ctx.parent.params["verbose"], timeout=ctx.parent.params["timeout"]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@main.command(name="cluster_config_has_changed")
|
@main.command(name="cluster_config_has_changed")
|
||||||
|
@ -329,13 +333,11 @@ def cluster_config_has_changed(
|
||||||
|
|
||||||
check = nagiosplugin.Check()
|
check = nagiosplugin.Check()
|
||||||
check.add(
|
check.add(
|
||||||
ClusterConfigHasChanged(ctx.obj, config_hash, state_file),
|
ClusterConfigHasChanged(ctx.obj.connection_info, config_hash, state_file),
|
||||||
nagiosplugin.ScalarContext("is_configuration_changed", None, "@1:1"),
|
nagiosplugin.ScalarContext("is_configuration_changed", None, "@1:1"),
|
||||||
ClusterConfigHasChangedSummary(),
|
ClusterConfigHasChangedSummary(),
|
||||||
)
|
)
|
||||||
check.main(
|
check.main(verbose=ctx.obj.verbose, timeout=ctx.obj.timeout)
|
||||||
verbose=ctx.parent.params["verbose"], timeout=ctx.parent.params["timeout"]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@main.command(name="cluster_is_in_maintenance")
|
@main.command(name="cluster_is_in_maintenance")
|
||||||
|
@ -355,12 +357,10 @@ def cluster_is_in_maintenance(ctx: click.Context) -> None:
|
||||||
"""
|
"""
|
||||||
check = nagiosplugin.Check()
|
check = nagiosplugin.Check()
|
||||||
check.add(
|
check.add(
|
||||||
ClusterIsInMaintenance(ctx.obj),
|
ClusterIsInMaintenance(ctx.obj.connection_info),
|
||||||
nagiosplugin.ScalarContext("is_in_maintenance", None, "0:0"),
|
nagiosplugin.ScalarContext("is_in_maintenance", None, "0:0"),
|
||||||
)
|
)
|
||||||
check.main(
|
check.main(verbose=ctx.obj.verbose, timeout=ctx.obj.timeout)
|
||||||
verbose=ctx.parent.params["verbose"], timeout=ctx.parent.params["timeout"]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@main.command(name="node_is_primary")
|
@main.command(name="node_is_primary")
|
||||||
|
@ -378,13 +378,11 @@ def node_is_primary(ctx: click.Context) -> None:
|
||||||
"""
|
"""
|
||||||
check = nagiosplugin.Check()
|
check = nagiosplugin.Check()
|
||||||
check.add(
|
check.add(
|
||||||
NodeIsPrimary(ctx.obj),
|
NodeIsPrimary(ctx.obj.connection_info),
|
||||||
nagiosplugin.ScalarContext("is_primary", None, "@0:0"),
|
nagiosplugin.ScalarContext("is_primary", None, "@0:0"),
|
||||||
NodeIsPrimarySummary(),
|
NodeIsPrimarySummary(),
|
||||||
)
|
)
|
||||||
check.main(
|
check.main(verbose=ctx.obj.verbose, timeout=ctx.obj.timeout)
|
||||||
verbose=ctx.parent.params["verbose"], timeout=ctx.parent.params["timeout"]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@main.command(name="node_is_replica")
|
@main.command(name="node_is_replica")
|
||||||
|
@ -404,13 +402,11 @@ def node_is_replica(ctx: click.Context, max_lag: str) -> None:
|
||||||
# FIXME add a lag check ??
|
# FIXME add a lag check ??
|
||||||
check = nagiosplugin.Check()
|
check = nagiosplugin.Check()
|
||||||
check.add(
|
check.add(
|
||||||
NodeIsReplica(ctx.obj, max_lag),
|
NodeIsReplica(ctx.obj.connection_info, max_lag),
|
||||||
nagiosplugin.ScalarContext("is_replica", None, "@0:0"),
|
nagiosplugin.ScalarContext("is_replica", None, "@0:0"),
|
||||||
NodeIsReplicaSummary(max_lag),
|
NodeIsReplicaSummary(max_lag),
|
||||||
)
|
)
|
||||||
check.main(
|
check.main(verbose=ctx.obj.verbose, timeout=ctx.obj.timeout)
|
||||||
verbose=ctx.parent.params["verbose"], timeout=ctx.parent.params["timeout"]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@main.command(name="node_is_pending_restart")
|
@main.command(name="node_is_pending_restart")
|
||||||
|
@ -431,13 +427,11 @@ def node_is_pending_restart(ctx: click.Context) -> None:
|
||||||
"""
|
"""
|
||||||
check = nagiosplugin.Check()
|
check = nagiosplugin.Check()
|
||||||
check.add(
|
check.add(
|
||||||
NodeIsPendingRestart(ctx.obj),
|
NodeIsPendingRestart(ctx.obj.connection_info),
|
||||||
nagiosplugin.ScalarContext("is_pending_restart", None, "@1:1"),
|
nagiosplugin.ScalarContext("is_pending_restart", None, "@1:1"),
|
||||||
NodeIsPendingRestartSummary(),
|
NodeIsPendingRestartSummary(),
|
||||||
)
|
)
|
||||||
check.main(
|
check.main(verbose=ctx.obj.verbose, timeout=ctx.obj.timeout)
|
||||||
verbose=ctx.parent.params["verbose"], timeout=ctx.parent.params["timeout"]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@main.command(name="node_tl_has_changed")
|
@main.command(name="node_tl_has_changed")
|
||||||
|
@ -476,14 +470,12 @@ def node_tl_has_changed(ctx: click.Context, timeline: str, state_file: str) -> N
|
||||||
|
|
||||||
check = nagiosplugin.Check()
|
check = nagiosplugin.Check()
|
||||||
check.add(
|
check.add(
|
||||||
NodeTLHasChanged(ctx.obj, timeline, state_file),
|
NodeTLHasChanged(ctx.obj.connection_info, timeline, state_file),
|
||||||
nagiosplugin.ScalarContext("is_timeline_changed", None, "@1:1"),
|
nagiosplugin.ScalarContext("is_timeline_changed", None, "@1:1"),
|
||||||
nagiosplugin.ScalarContext("timeline"),
|
nagiosplugin.ScalarContext("timeline"),
|
||||||
NodeTLHasChangedSummary(timeline),
|
NodeTLHasChangedSummary(timeline),
|
||||||
)
|
)
|
||||||
check.main(
|
check.main(verbose=ctx.obj.verbose, timeout=ctx.obj.timeout)
|
||||||
verbose=ctx.parent.params["verbose"], timeout=ctx.parent.params["timeout"]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@main.command(name="node_patroni_version")
|
@main.command(name="node_patroni_version")
|
||||||
|
@ -511,14 +503,12 @@ def node_patroni_version(ctx: click.Context, patroni_version: str) -> None:
|
||||||
# TODO the version cannot be written in perfdata find something else ?
|
# TODO the version cannot be written in perfdata find something else ?
|
||||||
check = nagiosplugin.Check()
|
check = nagiosplugin.Check()
|
||||||
check.add(
|
check.add(
|
||||||
NodePatroniVersion(ctx.obj, patroni_version),
|
NodePatroniVersion(ctx.obj.connection_info, patroni_version),
|
||||||
nagiosplugin.ScalarContext("is_version_ok", None, "@0:0"),
|
nagiosplugin.ScalarContext("is_version_ok", None, "@0:0"),
|
||||||
nagiosplugin.ScalarContext("patroni_version"),
|
nagiosplugin.ScalarContext("patroni_version"),
|
||||||
NodePatroniVersionSummary(patroni_version),
|
NodePatroniVersionSummary(patroni_version),
|
||||||
)
|
)
|
||||||
check.main(
|
check.main(verbose=ctx.obj.verbose, timeout=ctx.obj.timeout)
|
||||||
verbose=ctx.parent.params["verbose"], timeout=ctx.parent.params["timeout"]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@main.command(name="node_is_alive")
|
@main.command(name="node_is_alive")
|
||||||
|
@ -538,10 +528,8 @@ def node_is_alive(ctx: click.Context) -> None:
|
||||||
"""
|
"""
|
||||||
check = nagiosplugin.Check()
|
check = nagiosplugin.Check()
|
||||||
check.add(
|
check.add(
|
||||||
NodeIsAlive(ctx.obj),
|
NodeIsAlive(ctx.obj.connection_info),
|
||||||
nagiosplugin.ScalarContext("is_alive", None, "@0:0"),
|
nagiosplugin.ScalarContext("is_alive", None, "@0:0"),
|
||||||
NodeIsAliveSummary(),
|
NodeIsAliveSummary(),
|
||||||
)
|
)
|
||||||
check.main(
|
check.main(verbose=ctx.obj.verbose, timeout=ctx.obj.timeout)
|
||||||
verbose=ctx.parent.params["verbose"], timeout=ctx.parent.params["timeout"]
|
|
||||||
)
|
|
||||||
|
|
|
@ -15,6 +15,13 @@ class ConnectionInfo:
|
||||||
ca_cert: str = "./ssl/CA-cert.pem"
|
ca_cert: str = "./ssl/CA-cert.pem"
|
||||||
|
|
||||||
|
|
||||||
|
@attr.s(auto_attribs=True, frozen=True, slots=True)
|
||||||
|
class Parameters:
|
||||||
|
connection_info: ConnectionInfo
|
||||||
|
timeout: int
|
||||||
|
verbose: int
|
||||||
|
|
||||||
|
|
||||||
@attr.s(auto_attribs=True, slots=True)
|
@attr.s(auto_attribs=True, slots=True)
|
||||||
class PatroniResource(nagiosplugin.Resource):
|
class PatroniResource(nagiosplugin.Resource):
|
||||||
conn_info: ConnectionInfo
|
conn_info: ConnectionInfo
|
||||||
|
|
Loading…
Reference in a new issue