Add new scripts for checking status of Docker services

These are bundled within the Docker image to make it possible to alert
on status of other Docker containers.

Will potentially include other sets of "standard" scripts for
convenience sake.
This commit is contained in:
IamTheFij 2018-10-29 12:28:17 -07:00
parent 7ea24b8a89
commit 5783190f58
6 changed files with 101 additions and 1 deletions

View File

@ -4,3 +4,4 @@
*.py[cod]
*.so
**/__pycache__/
scripts/README.md

View File

@ -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" ]

5
scripts/README.md Normal file
View File

@ -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`.

38
scripts/docker_check.sh Executable file
View File

@ -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

47
scripts/docker_healthcheck.sh Executable file
View File

@ -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

View File

@ -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',