diff --git a/dockstat.py b/dockstat.py index b2ae33e..940cc10 100644 --- a/dockstat.py +++ b/dockstat.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 """ Module to act as a Prometheus Exporter for Docker containers with a - healthcheck configured +healthcheck configured """ import argparse @@ -9,33 +9,30 @@ import os import os.path import sys from http.server import HTTPServer +from typing import Any -import docker # type: ignore +import docker # type: ignore[import] import numpy -from prometheus_client import ( # type: ignore - CollectorRegistry, - Gauge, - MetricsHandler, - generate_latest, -) +from prometheus_client import CollectorRegistry # type: ignore[import] +from prometheus_client import Gauge, MetricsHandler, generate_latest LISTEN_PORT = int(os.environ.get('DOCKSTAT_LISTEN_PORT', 8080)) HEALTHY_STR = 'healthy' -class HTTPHandler(MetricsHandler): +class HTTPHandler(MetricsHandler): # type: ignore[misc] """ Class to encompass the requirements of a Prometheus Exporter for Docker containers with a healthcheck configured """ - def __init__(self, *args, **kwargs): + def __init__(self, *args: Any, **kwargs: Any): self.docker_api: docker.APIClient = docker.APIClient() self.docker_client = docker.from_env() super().__init__(*args, **kwargs) # Override built-in method - def do_GET(self): + def do_GET(self) -> None: """ Handle GET requests """ @@ -49,7 +46,7 @@ class HTTPHandler(MetricsHandler): self._respond(200, 'OK') - def _respond(self, status: int, message: str): + def _respond(self, status: int, message: str) -> None: """ Output a simple HTTP status and string to the client @@ -63,7 +60,7 @@ class HTTPHandler(MetricsHandler): self.end_headers() self.wfile.write(bytes(str(message).encode())) - def _metrics(self): + def _metrics(self) -> None: """ Handle the request for metrics """ @@ -108,13 +105,13 @@ class HTTPHandler(MetricsHandler): for container in self.docker_client.containers.list(all=True): data = self.docker_api.inspect_container(container.id) - running: str = bool(data['State']['Running']) - started_at: int = data['State']['StartedAt'] - exit_code: int = int(data['State']['ExitCode']) + running = bool(data['State']['Running']) + started_at = data['State']['StartedAt'] + exit_code = int(data['State']['ExitCode']) alert_threshold = int( data['Config']['Labels'].get('io.prometheus.alert.downtime', 3600) ) - starttime = numpy.datetime64(started_at, 's').astype('long') + starttime: numpy.longlong = numpy.datetime64(started_at, 's').astype('long') status_gauge.labels( container.id, @@ -134,7 +131,7 @@ class HTTPHandler(MetricsHandler): ).set(alert_threshold) try: - health_str: str = data['State']['Health']['Status'] + health_str = data['State']['Health']['Status'] health_gauge.labels( container.id, container.name, diff --git a/mypy.ini b/mypy.ini new file mode 100644 index 0000000..46d21cc --- /dev/null +++ b/mypy.ini @@ -0,0 +1,3 @@ +[mypy] +plugins = numpy.typing.mypy_plugin +exclude = ['\.pyenv', 'venv']