#!/usr/bin/env python3 from json import loads from subprocess import check_output from sys import argv try: container_name = argv[1] docker_ps = check_output([ 'docker', 'container', 'ls', '--all', '--format', 'json', '--filter', f'name={container_name}' ]) containers = loads(f"[{','.join([l for l in docker_ps.decode().splitlines() if l])}]") if not containers: print(f'CRITICAL: container {container_name} not found!') exit(2) if len(containers) > 1: print(f'Found more than one container matching {container_name}!') print(docker_ps) exit(3) if containers[0]['State'] != 'running': print(f'WARNING: container {container_name} not "running"') exit(2) print(f"OK: {containers[0]['Status']}") except Exception as e: print(repr(e)) exit(2)