From db3f008b81f3d593349b54236d3c178545ad7f14 Mon Sep 17 00:00:00 2001 From: benoit Date: Thu, 12 Aug 2021 11:38:55 +0200 Subject: [PATCH] More Typing --- check_patroni/cli.py | 7 +++---- check_patroni/cluster.py | 13 +++++-------- check_patroni/node.py | 17 ++++++++--------- check_patroni/types.py | 8 ++++---- mypy.ini | 2 +- test/test_cluster_has_leader.py | 2 +- 6 files changed, 22 insertions(+), 27 deletions(-) diff --git a/check_patroni/cli.py b/check_patroni/cli.py index e6f009f..52c2538 100644 --- a/check_patroni/cli.py +++ b/check_patroni/cli.py @@ -133,8 +133,7 @@ def main( """Nagios plugin for patroni.""" ctx.obj = ConnectionInfo(endpoints, cert_file, key_file, ca_file) - # TODO Not all "is/has" services have the same return code for ok. Check if it's ok - # Typing + # FIXME Not all "is/has" services have the same return code for ok. Check if it's ok @main.command(name="cluster_node_count") # required otherwise _ are converted to - @@ -219,7 +218,7 @@ def cluster_has_leader(ctx: click.Context) -> None: Perfdata : `has_leader` is 1 if there is a leader node, 0 otherwise """ - # TODO: Manage primary or standby leader in the same place ? + # FIXME: Manage primary or standby leader in the same place ? check = nagiosplugin.Check() check.add( ClusterHasLeader(ctx.obj), @@ -375,7 +374,7 @@ def node_is_replica(ctx: click.Context, lag: str) -> None: Perfdata : `is_replica` is 1 if the node is a running replica with noloadbalance tag and the lag is under the maximum threshold, 0 otherwise. """ - # add a lag check ?? + # FIXME add a lag check ?? check = nagiosplugin.Check() check.add( NodeIsReplica(ctx.obj, lag), diff --git a/check_patroni/cluster.py b/check_patroni/cluster.py index 356b7ee..a7ec089 100644 --- a/check_patroni/cluster.py +++ b/check_patroni/cluster.py @@ -3,6 +3,7 @@ import hashlib import json import logging import nagiosplugin +from typing import Iterable from .types import PatroniResource, ConnectionInfo, handle_unknown @@ -14,9 +15,8 @@ def replace_chars(text: str) -> str: class ClusterNodeCount(PatroniResource): - def probe(self: "ClusterNodeCount") -> nagiosplugin.Metric: + def probe(self: "ClusterNodeCount") -> Iterable[nagiosplugin.Metric]: r = self.rest_api("cluster") - # FIXME RC <> 200 ? _log.debug(f"api call status: {r.status}") _log.debug(f"api call data: {r.data}") @@ -51,9 +51,8 @@ class ClusterNodeCount(PatroniResource): class ClusterHasLeader(PatroniResource): - def probe(self: "ClusterHasLeader") -> nagiosplugin.Metric: + def probe(self: "ClusterHasLeader") -> Iterable[nagiosplugin.Metric]: r = self.rest_api("cluster") - # FIXME RC <> 200 ? _log.debug(f"api call status: {r.status}") _log.debug(f"api call data: {r.data}") @@ -82,9 +81,8 @@ class ClusterHasLeaderSummary(nagiosplugin.Summary): class ClusterHasReplica(PatroniResource): - def probe(self: "ClusterHasReplica") -> nagiosplugin.Metric: + def probe(self: "ClusterHasReplica") -> Iterable[nagiosplugin.Metric]: r = self.rest_api("cluster") - # FIXME RC <> 200 ? _log.debug(f"api call status: {r.status}") _log.debug(f"api call data: {r.data}") @@ -124,9 +122,8 @@ class ClusterConfigHasChanged(PatroniResource): self.state_file = state_file self.config_hash = config_hash - def probe(self: "ClusterConfigHasChanged") -> nagiosplugin.Metric: + def probe(self: "ClusterConfigHasChanged") -> Iterable[nagiosplugin.Metric]: r = self.rest_api("config") - # FIXME RC <> 200 ? _log.debug(f"api call status: {r.status}") _log.debug(f"api call data: {r.data}") diff --git a/check_patroni/node.py b/check_patroni/node.py index b5f7828..c1d4942 100644 --- a/check_patroni/node.py +++ b/check_patroni/node.py @@ -1,14 +1,16 @@ import json import logging import nagiosplugin +from typing import Iterable from .types import ConnectionInfo, handle_unknown, PatroniResource + _log = logging.getLogger("nagiosplugin") class NodeIsPrimary(PatroniResource): - def probe(self: "NodeIsPrimary") -> nagiosplugin.Metric: + def probe(self: "NodeIsPrimary") -> Iterable[nagiosplugin.Metric]: r = self.rest_api("primary") _log.debug(f"api call status: {r.status}") _log.debug(f"api call data: {r.data}") @@ -32,7 +34,7 @@ class NodeIsReplica(PatroniResource): super().__init__(connection_info) self.lag = lag - def probe(self: "NodeIsReplica") -> nagiosplugin.Metric: + def probe(self: "NodeIsReplica") -> Iterable[nagiosplugin.Metric]: if self.lag is None: r = self.rest_api("replica") else: @@ -60,9 +62,8 @@ class NodeIsReplicaSummary(nagiosplugin.Summary): class NodeIsPendingRestart(PatroniResource): - def probe(self: "NodeIsPendingRestart") -> nagiosplugin.Metric: + def probe(self: "NodeIsPendingRestart") -> Iterable[nagiosplugin.Metric]: r = self.rest_api("patroni") - # FIXME RC <> 200 ? _log.debug(f"api call status: {r.status}") _log.debug(f"api call data: {r.data}") @@ -98,9 +99,8 @@ class NodeTLHasChanged(PatroniResource): self.state_file = state_file self.timeline = timeline - def probe(self: "NodeTLHasChanged") -> nagiosplugin.Metric: + def probe(self: "NodeTLHasChanged") -> Iterable[nagiosplugin.Metric]: r = self.rest_api("patroni") - # FIXME RC <> 200 ? _log.debug(f"api call status: {r.status}") _log.debug(f"api call data: {r.data}") @@ -151,9 +151,8 @@ class NodePatroniVersion(PatroniResource): super().__init__(connection_info) self.patroni_version = patroni_version - def probe(self: "NodePatroniVersion") -> nagiosplugin.Metric: + def probe(self: "NodePatroniVersion") -> Iterable[nagiosplugin.Metric]: r = self.rest_api("patroni") - # FIXME RC <> 200 ? _log.debug(f"api call status: {r.status}") _log.debug(f"api call data: {r.data}") @@ -188,7 +187,7 @@ class NodePatroniVersionSummary(nagiosplugin.Summary): class NodeIsAlive(PatroniResource): - def probe(self: "NodeIsAlive") -> nagiosplugin.Metric: + def probe(self: "NodeIsAlive") -> Iterable[nagiosplugin.Metric]: r = self.rest_api("liveness") _log.debug(f"api call status: {r.status}") _log.debug(f"api call data: {r.data}") diff --git a/check_patroni/types.py b/check_patroni/types.py index 2b4b7e9..f16c3f0 100644 --- a/check_patroni/types.py +++ b/check_patroni/types.py @@ -48,16 +48,16 @@ class PatroniResource(nagiosplugin.Resource): raise nagiosplugin.CheckError("Connection failed for all provided endpoints") -HandleUnknown = Callable[[nagiosplugin.Summary, nagiosplugin.Result], Any] +HandleUnknown = Callable[[nagiosplugin.Summary, nagiosplugin.Results], Any] -def handle_unknown(action: HandleUnknown) -> HandleUnknown: +def handle_unknown(func: HandleUnknown) -> HandleUnknown: """decorator to handle the unknown state in Summary.problem""" - def wrapper(summary: nagiosplugin.Summary, results: nagiosplugin.Result) -> Any: + def wrapper(summary: nagiosplugin.Summary, results: nagiosplugin.Results) -> Any: if results.most_significant[0].state.code == 3: """get the appropriate message for all unknown error""" return results.most_significant[0].hint - return action(summary, results) + return func(summary, results) return wrapper diff --git a/mypy.ini b/mypy.ini index 07c0723..750ec7e 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1,5 +1,5 @@ [mypy] # nagiosplugin => Skipping analyzing "nagiosplugin": found module but no type hints or library stubs [import] -ignore_missing_imports = true +# ignore_missing_imports = true show_error_codes = true strict = true diff --git a/test/test_cluster_has_leader.py b/test/test_cluster_has_leader.py index cf6aa13..e121fa9 100644 --- a/test/test_cluster_has_leader.py +++ b/test/test_cluster_has_leader.py @@ -14,7 +14,7 @@ def test_cluster_has_leader_ok(mocker: MockerFixture) -> None: main, ["-e", "https://10.20.199.3:8008", "cluster_has_leader"] ) assert result.exit_code == 0 - # FIXME Not captured ??? + # FIXME the data seems to not be written to stdout yet ... # assert "CLUSTERHASLEADER OK - has_leader is 1 | has_leader=1;;@0" in result.output