dockstat/dockstat.py

135 lines
3.2 KiB
Python
Raw Normal View History

2020-10-10 14:11:43 +01:00
#!/usr/bin/env python3
"""
Module to act as a Prometheus Exporter for Docker containers with a
healthcheck configured
"""
import argparse
import os.path
import sys
2020-10-10 16:24:44 +01:00
from http.server import HTTPServer
2020-10-10 14:11:43 +01:00
import docker
2020-10-10 16:24:44 +01:00
from prometheus_client import (
CollectorRegistry,
Gauge,
generate_latest,
MetricsHandler,
)
2020-10-10 14:11:43 +01:00
LISTEN_PORT = 8080
HEALTHY_STR = 'healthy'
2020-10-10 16:24:44 +01:00
class HTTPHandler(MetricsHandler):
2020-10-10 14:11:43 +01:00
"""
Class to encompass the requirements of a Prometheus Exporter
for Docker containers with a healthcheck configured
"""
2020-10-10 16:24:44 +01:00
def __init__(self, *args, **kwargs):
self.docker_api = docker.APIClient()
self.docker_client = docker.from_env()
super().__init__(*args, **kwargs)
# Override built-in method
2020-10-10 14:11:43 +01:00
# pylint: disable=invalid-name
def do_GET(self):
"""
Method to handle GET requests
"""
if self.path == '/metrics':
self._metrics()
if self.path == '/healthcheck':
self._healthcheck()
def _healthcheck(self, message=True):
"""
Method to return 200 or 500 response and an optional message
"""
if not healthy():
self.send_response(500)
self.end_headers()
if message:
self.wfile.write(b'ERR')
return
self.send_response(200)
self.end_headers()
if message:
self.wfile.write(b'OK')
def _metrics(self):
"""
Method to handle the request for metrics
"""
self._healthcheck(message=False)
2020-10-10 16:24:44 +01:00
registry = CollectorRegistry()
2020-10-10 14:11:43 +01:00
2020-10-10 16:24:44 +01:00
gauge = Gauge(
'container_inspect_state_health_status',
"Container's healthcheck value (binary)",
labelnames=['id', 'name', 'value'],
registry=registry
)
for container in self.docker_client.containers.list():
data = self.docker_api.inspect_container(container.id)
2020-10-10 14:11:43 +01:00
try:
health_str = data["State"]["Health"]["Status"]
2020-10-10 16:24:44 +01:00
label_values = [
container.id,
container.name,
health_str,
]
2020-10-10 14:11:43 +01:00
except KeyError:
pass
else:
2020-10-10 16:24:44 +01:00
gauge.labels(*label_values).set(int(health_str == HEALTHY_STR))
self.wfile.write(generate_latest(registry))
2020-10-10 14:11:43 +01:00
def healthy():
"""
Simple funtion to return if all the requirements are met
"""
return all([
os.path.exists('/var/run/docker.sock'),
])
if __name__ == '__main__':
def cli_parse():
"""
Function to parse the CLI
"""
parser = argparse.ArgumentParser()
parser.add_argument(
'-H', '--healthcheck',
action='store_true',
help='Simply exit with 0 for healthy or 1 when unhealthy',
)
return parser.parse_args()
def main():
"""
main()
"""
args = cli_parse()
if args.healthcheck:
# Invert the sense of 'healthy' for Unix CLI usage
return not healthy()
2020-10-10 16:24:44 +01:00
HTTPServer(('', LISTEN_PORT), HTTPHandler).serve_forever()
2020-10-10 14:11:43 +01:00
2020-10-10 16:24:44 +01:00
return 0
2020-10-10 14:11:43 +01:00
sys.exit(main())