diff --git a/.dockerignore b/.dockerignore index 9e09577..1a276cc 100644 --- a/.dockerignore +++ b/.dockerignore @@ -4,3 +4,4 @@ *.py[cod] *.so **/__pycache__/ +scripts/README.md diff --git a/Dockerfile b/Dockerfile index 0ea4c47..4c94b19 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,9 +8,13 @@ WORKDIR /app # Expose default metrics port EXPOSE 8080 +# Copy Python package to container COPY ./README.md /app/ COPY ./setup.py /app/ COPY ./minitor /app/minitor RUN pip install -e . +# Copy scripts +COPY ./scripts /app/scripts + ENTRYPOINT [ "python3", "-m", "minitor.main" ] diff --git a/scripts/README.md b/scripts/README.md new file mode 100644 index 0000000..328cb77 --- /dev/null +++ b/scripts/README.md @@ -0,0 +1,5 @@ +# Minitor Scripts + +A collection of some handy scripts to use with Minitor + +These are not included with the Python package, but they are included in the Docker image in `/app/scripts`. diff --git a/scripts/docker_check.sh b/scripts/docker_check.sh new file mode 100755 index 0000000..b4c9253 --- /dev/null +++ b/scripts/docker_check.sh @@ -0,0 +1,38 @@ +#! /bin/bash +set -e + +################# +# docker_check.sh +# +# Checks the most recent state exit code of a Docker container +################# + +container_name=$1 + +# Returns caintainer ID for a given container name +function get_container_id { + local container_name=$1 + curl --unix-socket /var/run/docker.sock 'http://localhost/containers/json?all=1' 2>/dev/null \ + | jq -r ".[] | {Id, Name: .Names[]} | select(.Name == \"/${container_name}\") | .Id" +} + +# Returns container JSON +function inspect_container { + local container_id=$1 + curl --unix-socket /var/run/docker.sock http://localhost/containers/$container_id/json 2>/dev/null +} + +if [ -z "$container_name" ]; then + echo "Usage: $0 container_name" + echo "Will exit with the last status code of continer with provided name" + exit 1 +fi + +container_id=$(get_container_id $container_name) +if [ -z "$container_id" ]; then + echo "ERROR: Could not find container with name: $container_name" + exit 1 +fi +exit_code=$(inspect_container $container_id | jq -r .State.ExitCode) + +exit $exit_code diff --git a/scripts/docker_healthcheck.sh b/scripts/docker_healthcheck.sh new file mode 100755 index 0000000..7cbd6fc --- /dev/null +++ b/scripts/docker_healthcheck.sh @@ -0,0 +1,47 @@ +#! /bin/bash + +################# +# docker_healthcheck.sh +# +# Returns the results of a Docker Healthcheck for a container +################# + +container_name=$1 + +# Returns caintainer ID for a given container name +function get_container_id { + local container_name=$1 + curl --unix-socket /var/run/docker.sock 'http://localhost/containers/json?all=1' 2>/dev/null \ + | jq -r ".[] | {Id, Name: .Names[]} | select(.Name == \"/${container_name}\") | .Id" +} + +# Returns container JSON +function inspect_container { + local container_id=$1 + curl --unix-socket /var/run/docker.sock http://localhost/containers/$container_id/json 2>/dev/null +} + +if [ -z "$container_name" ]; then + echo "Usage: $0 container_name" + echo "Will return results of healthcheck for continer with provided name" + exit 1 +fi + +container_id=$(get_container_id $container_name) +if [ -z "$container_id" ]; then + echo "ERROR: Could not find container with name: $container_name" + exit 1 +fi +health=$(inspect_container $container_id | jq -r '.State.Health.Status') + +case $health in + "null") + echo "No healthcheck results" + ;; + "starting|healthy") + echo "Status: '$health'" + ;; + *) + echo "Status: '$health'" + exit 1 +esac diff --git a/setup.py b/setup.py index e4cadca..de61608 100644 --- a/setup.py +++ b/setup.py @@ -36,7 +36,12 @@ setup( 'Programming Language :: Python :: 3.6', ], keywords='minitor monitoring alerting', - packages=find_packages(exclude=['contrib', 'docs', 'tests']), + packages=find_packages(exclude=[ + 'contrib', + 'docs', + 'tests', + 'scripts', + ]), install_requires=[ 'prometheus_client', 'yamlenv',