Skip to content

Instantly share code, notes, and snippets.

@dusansimic
Created January 6, 2023 12:10
Show Gist options
  • Save dusansimic/f4ed34e485cb4f61dd9807340ba76070 to your computer and use it in GitHub Desktop.
Save dusansimic/f4ed34e485cb4f61dd9807340ba76070 to your computer and use it in GitHub Desktop.
Python script for checking podman container health
#!/usr/bin/python3
import subprocess
import sys
import json
# command for gathering health information
# there are no checks and user is expected to pass valid information
# if container id is incorrect, script will return status code 1
command = ['podman', 'inspect', '--format', '\'{{.State.Health.Status}}\'', sys.argv[1]]
# run the command
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
stdout, stderr = process.communicate()
# parse the output
# result is received as a byte list
# status string is wrapped with single quotes and it ends with a new line
# those characters are removed and string is decoded
status = stdout[1:-2].decode('utf-8')
exit(0 if status == 'healthy' else 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment