Allow to run check on a specified Docker compose project

This commit is contained in:
Benjamin Renard 2024-03-11 19:15:06 +01:00
parent c7283ea44e
commit 1cde0c3b86
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC
2 changed files with 39 additions and 9 deletions

View file

@ -25,12 +25,13 @@ service nagios-nrpe-server reload
```
Usage : check_container_upgrade [-d] [-E /path/to/engine] [container1,...]
-E [path] Force a specific engine (possible values: auto docker podman, default: auto)
-x [container] Exclude specified container (could be repeat)
-M [integer] Max number of container checks to run in parallel (default: 4, 0=no limit)
-d Debug mode
-X Enable bash tracing (=set -x)
-h Show this message
-E [path] Force a specific engine (possible values: auto docker podman, default: auto)
-x [container] Exclude specified container (could be repeat)
-M [integer] Max number of container checks to run in parallel (default: 4, 0=no limit)
-f [docker-compose.yml] To check upgrade on docker compose project, specified the path of the docker-compose.yml file
-d Debug mode
-X Enable bash tracing (=set -x)
-h Show this message
```
## Copyright

View file

@ -8,6 +8,7 @@
ENGINE="auto"
POSSIBLE_ENGINES=( "auto" "docker" "podman" )
DOCKERCOMPOSE_FILE=""
DEBUG=0
MAX_PARALLEL_CHECKS=4
ONLY_CONTAINERS=()
@ -47,6 +48,7 @@ Usage : $(basename $0) [-d] [-E /path/to/engine] [container1,...]
-E [path] Force a specific engine (possible values: ${POSSIBLE_ENGINES[@]}, default: $ENGINE)
-x [container] Exclude specified container (could be repeat)
-M [integer] Max number of container checks to run in parallel (default: $MAX_PARALLEL_CHECKS, 0=no limit)
-f [docker-compose.yml] To check upgrade on docker compose project, specified the path of the docker-compose.yml file
-d Debug mode
-X Enable bash tracing (=set -x)
-h Show this message
@ -74,6 +76,10 @@ do
in_array $ENGINE ${POSSIBLE_ENGINES[@]} || usage "Invalid engine $ENGINE"
fi
;;
-f)
((idx++))
DOCKERCOMPOSE_FILE=${!idx}
;;
-x)
((idx++))
EXCLUDED_CONTAINERS+=( ${!idx} )
@ -117,6 +123,12 @@ then
debug "Auto-detected engine: $ENGINE"
fi
if [ -n "$DOCKERCOMPOSE_FILE" -a ! -e "$DOCKERCOMPOSE_FILE" ]
then
echo "UNKNOWN - Docker compose file not found ($DOCKERCOMPOSE_FILE)"
exit 3
fi
EXIT_CODE=0
declare -A CONTAINER_STATUS_FILE
declare -A CONTAINER_PID
@ -125,9 +137,26 @@ declare -A ERRORS
CHECKED_CONTAINERS=( )
debug "List running containers..."
RUNNING_CONTAINERS=$($ENGINE ps --format '{{.Names}}' | tr '\n' ' ')
if [ -n "$DOCKERCOMPOSE_FILE" ]
then
RUNNING_CONTAINERS=$($ENGINE compose -f $DOCKERCOMPOSE_FILE ps --format '{{.Service}}' | tr '\n' ' ')
else
RUNNING_CONTAINERS=$($ENGINE ps --format '{{.Names}}' | tr '\n' ' ')
fi
debug "Running containers: $RUNNING_CONTAINERS"
function exec_in_container() {
container=$1
shift;
if [ -n "$DOCKERCOMPOSE_FILE" ]
then
$ENGINE compose -f $DOCKERCOMPOSE_FILE exec $container $@
return $?
fi
$ENGINE exec $container $@
return $?
}
# Implement check inside a function to allow running it in parallel
# Parameters : [container] [output file]
function check_container() {
@ -136,14 +165,14 @@ function check_container() {
STATUS=""
for check_plugin in ${CHECK_PLUGINS[@]}
do
$ENGINE exec $container test -e $check_plugin > /dev/null 2>&1
exec_in_container $container test -e $check_plugin > /dev/null 2>&1
if [ $? -ne 0 ]
then
debug "$container - Plugin $check_plugin not found"
continue
fi
debug "$container - Plugin $check_plugin found, use it"
STATUS="$($ENGINE exec $container ${CHECK_PLUGINS[${check_plugin}]} 2>&1)"
STATUS="$(exec_in_container $container ${CHECK_PLUGINS[${check_plugin}]} 2>&1)"
ex=$?
debug "$container - Plugin output: $STATUS"
debug "$container - Plugin exit code: $ex"