From 29de6639d89058e5ce5396d4eaa3ec5dccdbc3fc Mon Sep 17 00:00:00 2001 From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch> Date: Tue, 23 Jan 2024 16:49:20 +0100 Subject: [PATCH] Show a list of docker containers; add path --- check_docker_info | 79 ++++++++++++++++++++++++----- docs/20_Checks/check_docker_info.md | 29 +++++++++-- 2 files changed, 90 insertions(+), 18 deletions(-) diff --git a/check_docker_info b/check_docker_info index b84e04b..e558395 100755 --- a/check_docker_info +++ b/check_docker_info @@ -13,13 +13,14 @@ # ---------------------------------------------------------------------- # 2024-01-18 v1.0 <axel.hahn@unibe.ch> init # 2024-01-22 v1.1 <axel.hahn@unibe.ch> detect DOCKER; use sudo; add debug +# 2024-01-23 v1.2 <axel.hahn@unibe.ch> Show a list of docker containers; add path # ====================================================================== . $(dirname $0)/inc_pluginfunctions . $(dirname $0)/inc_dockerfunctions.sh -self_APPVERSION=1.1 +self_APPVERSION=1.2 # ---------------------------------------------------------------------- # FUNCTIONS @@ -27,7 +28,7 @@ self_APPVERSION=1.1 # show help -function _usage(){ +function _showHelp(){ local _self=$( basename $0 ) cat <<EOH $( ph.showImlHelpHeader ) @@ -36,7 +37,7 @@ Show docker version and count of containers total and by its status. It returns CRITICAL if a created container is not running. - OK if no container was created yet or all conmtainers are running + OK if no container was created yet or all conmtainers are running. UNKNOWM if - docker or jq were not found - docker data were not fetched @@ -48,15 +49,30 @@ USAGE: $_self [OPTIONS] OPTIONS: - -h this help - -d Debug; Flag: show all docker system infos + -h, --help this help + -c, --containers Flag: Show containers (slow) + -e, --errors Flag: Show containers on error only (slow) + -d, --debug Debug; Flag: show all docker system infos + -p, --path Custom directory for docker binary EXAMPLES: $_self Show Status of all containers + $_self -c + Show Status of all containers and a list of container names with its + status. Warning: this feature uses docker ps --all and can be slow. + + $_self -e + Show Status of all containers. If not all containers are running you + get a list of container names with its status. + + $_self -p /usr/bin + Show Status of all containers. The docker binary will be searched in + given path first - then in all other dirs of \$PATH + $_self -d - Show Status of all containers and json with all docker system infos + Show Status of all containers and json with all docker system infos. EOH } @@ -65,13 +81,22 @@ EOH # MAIN # ---------------------------------------------------------------------- -ph.hasParamoption "h" "$@"; bOptHelp=$? -ph.hasParamoption "d" "$@"; bOptDebug=$? +bOptContainers=0 +bOptContOnErrors=0 +bOptDebug=0 + +while [[ "$#" -gt 0 ]]; do case $1 in + -h|--help) _showHelp; exit 0;; + -c|--containers) bOptContainers=1; shift;; + -d|--debug) bOptDebug=1; shift;; + -e|--errors) bOptContOnErrors=1; shift;; + -p|--path) if ! grep ":{$2}:" <<< ":{$PATH}:" >/dev/null; then + PATH="$2:$PATH"; + fi + shift; shift;; + *) echo "ERROR: Unknown parameter: $1"; showHelp; exit 1; +esac; done -if [ $bOptHelp -eq 0 ]; then - _usage - exit 0 -fi ph.require "docker" ph.require "jq" @@ -95,15 +120,21 @@ sLicense=$( _getString "$data" ".ProductLicense" ) # --- generate result if [ "$iCTotal" -eq "0" ]; then - out+="No container was created yet." + out+="Welcome to Docker! No container was created yet." else if [ "$iCRunning" -eq "0" ]; then ph.setStatus critical out+="No container is running" + + # if -e was given: enable to show containers + test $bOptContOnErrors -eq 1 && bOptContainers=1 else if [ "$iCRunning" -ne "$iCTotal" ]; then ph.setStatus critical out+="Not all containers are running" + + # if -e was given: enable to show containers + test $bOptContOnErrors -eq 1 && bOptContainers=1 else out+="All containers are running" fi @@ -120,8 +151,28 @@ ph.perfadd "images" "$iImages" ph.status "Docker $sVersion ($sLicense) .. containers: $iCTotal running: $iCRunning paused: $iCPaused stopped: $iCStopped .. images: $iImages" echo "$out" +if [ $bOptContainers -eq 1 ]; then + echo + echo "Containers:" + containers=$( sudo -n --preserve-env docker ps --all --format "{{ json . }}" --all 2>/dev/null ) + echo "$containers" | while read -r line; do + sName=$( _getString "$line" ".Names" ) + sState=$( _getString "$line" ".State" ) + sStatus=$( _getString "$line" ".Status" ) + + sIco="🔶" + grep "exited" <<< "${sState}" >/dev/null && sIco="❌" + grep "running" <<< "${sState}" >/dev/null && sIco="✅" + + printf " %-14s %-40s %-20s\n" "$sIco $sState" "$sName" "$sStatus" + # echo "$line" | jq + done + echo + +fi + # if -d was given then show debug infos too -test $bOptDebug -eq 0 && ( echo; echo "DEBUG: full docker system infos as json"; echo "$data" | jq ) +test $bOptDebug -eq 1 && ( echo; echo "DEBUG: full docker system infos as json"; echo "$data" | jq ) ph.exit diff --git a/docs/20_Checks/check_docker_info.md b/docs/20_Checks/check_docker_info.md index e10d9b9..b2f331b 100644 --- a/docs/20_Checks/check_docker_info.md +++ b/docs/20_Checks/check_docker_info.md @@ -21,7 +21,7 @@ icingaclient ALL=(ALL) NOPASSWD:SETENV: /usr/bin/docker ______________________________________________________________________ CHECK_DOCKER_INFO -v1.1 +v1.2 (c) Institute for Medical Education - University of Bern Licence: GNU GPL 3 @@ -45,15 +45,30 @@ USAGE: check_docker_info [OPTIONS] OPTIONS: - -h this help - -d Debug; Flag: show all docker system infos + -h, --help this help + -c, --containers Flag: Show containers (slow) + -e, --errors Flag: Show containers on error only (slow) + -d, --debug Debug; Flag: show all docker system infos + -p, --path Custom directory for docker binary EXAMPLES: check_docker_info Show Status of all containers + check_docker_info -c + Show Status of all containers and a list of container names with its + status. Warning: this feature uses docker ps --all and can be slow. + + check_docker_info -e + Show Status of all containers. If not all containers are running you + get a list of container names with its status. + + check_docker_info -p /usr/bin + Show Status of all containers. The docker binary will be searched in + given path first - then in all other dirs of $PATH + check_docker_info -d - Show Status of all containers and json with all docker system infos + Show Status of all containers and json with all docker system infos. ``` @@ -84,3 +99,9 @@ Solution: you did not set SETENV in the sudoers config file icingaclient ALL=(ALL) NOPASSWD:**SETENV:** /usr/bin/docker +### Docker not found + +If you get an error message that docker was not found then it is not reachable in ofe of the directories of $PATH. +This can happen especialy in rootless docker instances. + +Solution: Use the parameter `-p /usr/bin` to define a target. Copy the docker binary there. -- GitLab