More Typing
This commit is contained in:
parent
1e6adc6a1a
commit
db3f008b81
|
@ -133,8 +133,7 @@ def main(
|
||||||
"""Nagios plugin for patroni."""
|
"""Nagios plugin for patroni."""
|
||||||
ctx.obj = ConnectionInfo(endpoints, cert_file, key_file, ca_file)
|
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
|
# FIXME Not all "is/has" services have the same return code for ok. Check if it's ok
|
||||||
# Typing
|
|
||||||
|
|
||||||
|
|
||||||
@main.command(name="cluster_node_count") # required otherwise _ are converted to -
|
@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
|
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 = nagiosplugin.Check()
|
||||||
check.add(
|
check.add(
|
||||||
ClusterHasLeader(ctx.obj),
|
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.
|
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 = nagiosplugin.Check()
|
||||||
check.add(
|
check.add(
|
||||||
NodeIsReplica(ctx.obj, lag),
|
NodeIsReplica(ctx.obj, lag),
|
||||||
|
|
|
@ -3,6 +3,7 @@ import hashlib
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import nagiosplugin
|
import nagiosplugin
|
||||||
|
from typing import Iterable
|
||||||
|
|
||||||
from .types import PatroniResource, ConnectionInfo, handle_unknown
|
from .types import PatroniResource, ConnectionInfo, handle_unknown
|
||||||
|
|
||||||
|
@ -14,9 +15,8 @@ def replace_chars(text: str) -> str:
|
||||||
|
|
||||||
|
|
||||||
class ClusterNodeCount(PatroniResource):
|
class ClusterNodeCount(PatroniResource):
|
||||||
def probe(self: "ClusterNodeCount") -> nagiosplugin.Metric:
|
def probe(self: "ClusterNodeCount") -> Iterable[nagiosplugin.Metric]:
|
||||||
r = self.rest_api("cluster")
|
r = self.rest_api("cluster")
|
||||||
# FIXME RC <> 200 ?
|
|
||||||
_log.debug(f"api call status: {r.status}")
|
_log.debug(f"api call status: {r.status}")
|
||||||
_log.debug(f"api call data: {r.data}")
|
_log.debug(f"api call data: {r.data}")
|
||||||
|
|
||||||
|
@ -51,9 +51,8 @@ class ClusterNodeCount(PatroniResource):
|
||||||
|
|
||||||
|
|
||||||
class ClusterHasLeader(PatroniResource):
|
class ClusterHasLeader(PatroniResource):
|
||||||
def probe(self: "ClusterHasLeader") -> nagiosplugin.Metric:
|
def probe(self: "ClusterHasLeader") -> Iterable[nagiosplugin.Metric]:
|
||||||
r = self.rest_api("cluster")
|
r = self.rest_api("cluster")
|
||||||
# FIXME RC <> 200 ?
|
|
||||||
_log.debug(f"api call status: {r.status}")
|
_log.debug(f"api call status: {r.status}")
|
||||||
_log.debug(f"api call data: {r.data}")
|
_log.debug(f"api call data: {r.data}")
|
||||||
|
|
||||||
|
@ -82,9 +81,8 @@ class ClusterHasLeaderSummary(nagiosplugin.Summary):
|
||||||
|
|
||||||
|
|
||||||
class ClusterHasReplica(PatroniResource):
|
class ClusterHasReplica(PatroniResource):
|
||||||
def probe(self: "ClusterHasReplica") -> nagiosplugin.Metric:
|
def probe(self: "ClusterHasReplica") -> Iterable[nagiosplugin.Metric]:
|
||||||
r = self.rest_api("cluster")
|
r = self.rest_api("cluster")
|
||||||
# FIXME RC <> 200 ?
|
|
||||||
_log.debug(f"api call status: {r.status}")
|
_log.debug(f"api call status: {r.status}")
|
||||||
_log.debug(f"api call data: {r.data}")
|
_log.debug(f"api call data: {r.data}")
|
||||||
|
|
||||||
|
@ -124,9 +122,8 @@ class ClusterConfigHasChanged(PatroniResource):
|
||||||
self.state_file = state_file
|
self.state_file = state_file
|
||||||
self.config_hash = config_hash
|
self.config_hash = config_hash
|
||||||
|
|
||||||
def probe(self: "ClusterConfigHasChanged") -> nagiosplugin.Metric:
|
def probe(self: "ClusterConfigHasChanged") -> Iterable[nagiosplugin.Metric]:
|
||||||
r = self.rest_api("config")
|
r = self.rest_api("config")
|
||||||
# FIXME RC <> 200 ?
|
|
||||||
_log.debug(f"api call status: {r.status}")
|
_log.debug(f"api call status: {r.status}")
|
||||||
_log.debug(f"api call data: {r.data}")
|
_log.debug(f"api call data: {r.data}")
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,16 @@
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import nagiosplugin
|
import nagiosplugin
|
||||||
|
from typing import Iterable
|
||||||
|
|
||||||
from .types import ConnectionInfo, handle_unknown, PatroniResource
|
from .types import ConnectionInfo, handle_unknown, PatroniResource
|
||||||
|
|
||||||
|
|
||||||
_log = logging.getLogger("nagiosplugin")
|
_log = logging.getLogger("nagiosplugin")
|
||||||
|
|
||||||
|
|
||||||
class NodeIsPrimary(PatroniResource):
|
class NodeIsPrimary(PatroniResource):
|
||||||
def probe(self: "NodeIsPrimary") -> nagiosplugin.Metric:
|
def probe(self: "NodeIsPrimary") -> Iterable[nagiosplugin.Metric]:
|
||||||
r = self.rest_api("primary")
|
r = self.rest_api("primary")
|
||||||
_log.debug(f"api call status: {r.status}")
|
_log.debug(f"api call status: {r.status}")
|
||||||
_log.debug(f"api call data: {r.data}")
|
_log.debug(f"api call data: {r.data}")
|
||||||
|
@ -32,7 +34,7 @@ class NodeIsReplica(PatroniResource):
|
||||||
super().__init__(connection_info)
|
super().__init__(connection_info)
|
||||||
self.lag = lag
|
self.lag = lag
|
||||||
|
|
||||||
def probe(self: "NodeIsReplica") -> nagiosplugin.Metric:
|
def probe(self: "NodeIsReplica") -> Iterable[nagiosplugin.Metric]:
|
||||||
if self.lag is None:
|
if self.lag is None:
|
||||||
r = self.rest_api("replica")
|
r = self.rest_api("replica")
|
||||||
else:
|
else:
|
||||||
|
@ -60,9 +62,8 @@ class NodeIsReplicaSummary(nagiosplugin.Summary):
|
||||||
|
|
||||||
|
|
||||||
class NodeIsPendingRestart(PatroniResource):
|
class NodeIsPendingRestart(PatroniResource):
|
||||||
def probe(self: "NodeIsPendingRestart") -> nagiosplugin.Metric:
|
def probe(self: "NodeIsPendingRestart") -> Iterable[nagiosplugin.Metric]:
|
||||||
r = self.rest_api("patroni")
|
r = self.rest_api("patroni")
|
||||||
# FIXME RC <> 200 ?
|
|
||||||
_log.debug(f"api call status: {r.status}")
|
_log.debug(f"api call status: {r.status}")
|
||||||
_log.debug(f"api call data: {r.data}")
|
_log.debug(f"api call data: {r.data}")
|
||||||
|
|
||||||
|
@ -98,9 +99,8 @@ class NodeTLHasChanged(PatroniResource):
|
||||||
self.state_file = state_file
|
self.state_file = state_file
|
||||||
self.timeline = timeline
|
self.timeline = timeline
|
||||||
|
|
||||||
def probe(self: "NodeTLHasChanged") -> nagiosplugin.Metric:
|
def probe(self: "NodeTLHasChanged") -> Iterable[nagiosplugin.Metric]:
|
||||||
r = self.rest_api("patroni")
|
r = self.rest_api("patroni")
|
||||||
# FIXME RC <> 200 ?
|
|
||||||
_log.debug(f"api call status: {r.status}")
|
_log.debug(f"api call status: {r.status}")
|
||||||
_log.debug(f"api call data: {r.data}")
|
_log.debug(f"api call data: {r.data}")
|
||||||
|
|
||||||
|
@ -151,9 +151,8 @@ class NodePatroniVersion(PatroniResource):
|
||||||
super().__init__(connection_info)
|
super().__init__(connection_info)
|
||||||
self.patroni_version = patroni_version
|
self.patroni_version = patroni_version
|
||||||
|
|
||||||
def probe(self: "NodePatroniVersion") -> nagiosplugin.Metric:
|
def probe(self: "NodePatroniVersion") -> Iterable[nagiosplugin.Metric]:
|
||||||
r = self.rest_api("patroni")
|
r = self.rest_api("patroni")
|
||||||
# FIXME RC <> 200 ?
|
|
||||||
|
|
||||||
_log.debug(f"api call status: {r.status}")
|
_log.debug(f"api call status: {r.status}")
|
||||||
_log.debug(f"api call data: {r.data}")
|
_log.debug(f"api call data: {r.data}")
|
||||||
|
@ -188,7 +187,7 @@ class NodePatroniVersionSummary(nagiosplugin.Summary):
|
||||||
|
|
||||||
|
|
||||||
class NodeIsAlive(PatroniResource):
|
class NodeIsAlive(PatroniResource):
|
||||||
def probe(self: "NodeIsAlive") -> nagiosplugin.Metric:
|
def probe(self: "NodeIsAlive") -> Iterable[nagiosplugin.Metric]:
|
||||||
r = self.rest_api("liveness")
|
r = self.rest_api("liveness")
|
||||||
_log.debug(f"api call status: {r.status}")
|
_log.debug(f"api call status: {r.status}")
|
||||||
_log.debug(f"api call data: {r.data}")
|
_log.debug(f"api call data: {r.data}")
|
||||||
|
|
|
@ -48,16 +48,16 @@ class PatroniResource(nagiosplugin.Resource):
|
||||||
raise nagiosplugin.CheckError("Connection failed for all provided endpoints")
|
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"""
|
"""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:
|
if results.most_significant[0].state.code == 3:
|
||||||
"""get the appropriate message for all unknown error"""
|
"""get the appropriate message for all unknown error"""
|
||||||
return results.most_significant[0].hint
|
return results.most_significant[0].hint
|
||||||
return action(summary, results)
|
return func(summary, results)
|
||||||
|
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
2
mypy.ini
2
mypy.ini
|
@ -1,5 +1,5 @@
|
||||||
[mypy]
|
[mypy]
|
||||||
# nagiosplugin => Skipping analyzing "nagiosplugin": found module but no type hints or library stubs [import]
|
# 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
|
show_error_codes = true
|
||||||
strict = true
|
strict = true
|
||||||
|
|
|
@ -14,7 +14,7 @@ def test_cluster_has_leader_ok(mocker: MockerFixture) -> None:
|
||||||
main, ["-e", "https://10.20.199.3:8008", "cluster_has_leader"]
|
main, ["-e", "https://10.20.199.3:8008", "cluster_has_leader"]
|
||||||
)
|
)
|
||||||
assert result.exit_code == 0
|
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
|
# assert "CLUSTERHASLEADER OK - has_leader is 1 | has_leader=1;;@0" in result.output
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue