More Typing

This commit is contained in:
benoit 2021-08-12 11:38:55 +02:00
parent 1e6adc6a1a
commit db3f008b81
6 changed files with 22 additions and 27 deletions

View file

@ -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),

View file

@ -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}")

View file

@ -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}")

View file

@ -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

View file

@ -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

View file

@ -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