Skip to content
Snippets Groups Projects
Commit 29de6639 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

Show a list of docker containers; add path

parent ea97b5fd
No related branches found
No related tags found
1 merge request!231docker infos: Show a list of docker containers; add path
...@@ -13,13 +13,14 @@ ...@@ -13,13 +13,14 @@
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# 2024-01-18 v1.0 <axel.hahn@unibe.ch> init # 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-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_pluginfunctions
. $(dirname $0)/inc_dockerfunctions.sh . $(dirname $0)/inc_dockerfunctions.sh
self_APPVERSION=1.1 self_APPVERSION=1.2
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# FUNCTIONS # FUNCTIONS
...@@ -27,7 +28,7 @@ self_APPVERSION=1.1 ...@@ -27,7 +28,7 @@ self_APPVERSION=1.1
# show help # show help
function _usage(){ function _showHelp(){
local _self=$( basename $0 ) local _self=$( basename $0 )
cat <<EOH cat <<EOH
$( ph.showImlHelpHeader ) $( ph.showImlHelpHeader )
...@@ -36,7 +37,7 @@ Show docker version and count of containers total and by its status. ...@@ -36,7 +37,7 @@ Show docker version and count of containers total and by its status.
It returns It returns
CRITICAL if a created container is not running. 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 UNKNOWM if
- docker or jq were not found - docker or jq were not found
- docker data were not fetched - docker data were not fetched
...@@ -48,15 +49,30 @@ USAGE: ...@@ -48,15 +49,30 @@ USAGE:
$_self [OPTIONS] $_self [OPTIONS]
OPTIONS: OPTIONS:
-h this help -h, --help this help
-d Debug; Flag: show all docker system infos -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: EXAMPLES:
$_self $_self
Show Status of all containers 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 $_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 EOH
} }
...@@ -65,13 +81,22 @@ EOH ...@@ -65,13 +81,22 @@ EOH
# MAIN # MAIN
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
ph.hasParamoption "h" "$@"; bOptHelp=$? bOptContainers=0
ph.hasParamoption "d" "$@"; bOptDebug=$? 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 "docker"
ph.require "jq" ph.require "jq"
...@@ -95,15 +120,21 @@ sLicense=$( _getString "$data" ".ProductLicense" ) ...@@ -95,15 +120,21 @@ sLicense=$( _getString "$data" ".ProductLicense" )
# --- generate result # --- generate result
if [ "$iCTotal" -eq "0" ]; then if [ "$iCTotal" -eq "0" ]; then
out+="No container was created yet." out+="Welcome to Docker! No container was created yet."
else else
if [ "$iCRunning" -eq "0" ]; then if [ "$iCRunning" -eq "0" ]; then
ph.setStatus critical ph.setStatus critical
out+="No container is running" out+="No container is running"
# if -e was given: enable to show containers
test $bOptContOnErrors -eq 1 && bOptContainers=1
else else
if [ "$iCRunning" -ne "$iCTotal" ]; then if [ "$iCRunning" -ne "$iCTotal" ]; then
ph.setStatus critical ph.setStatus critical
out+="Not all containers are running" out+="Not all containers are running"
# if -e was given: enable to show containers
test $bOptContOnErrors -eq 1 && bOptContainers=1
else else
out+="All containers are running" out+="All containers are running"
fi fi
...@@ -120,8 +151,28 @@ ph.perfadd "images" "$iImages" ...@@ -120,8 +151,28 @@ ph.perfadd "images" "$iImages"
ph.status "Docker $sVersion ($sLicense) .. containers: $iCTotal running: $iCRunning paused: $iCPaused stopped: $iCStopped .. images: $iImages" ph.status "Docker $sVersion ($sLicense) .. containers: $iCTotal running: $iCRunning paused: $iCPaused stopped: $iCStopped .. images: $iImages"
echo "$out" 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 # 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 ph.exit
......
...@@ -21,7 +21,7 @@ icingaclient ALL=(ALL) NOPASSWD:SETENV: /usr/bin/docker ...@@ -21,7 +21,7 @@ icingaclient ALL=(ALL) NOPASSWD:SETENV: /usr/bin/docker
______________________________________________________________________ ______________________________________________________________________
CHECK_DOCKER_INFO CHECK_DOCKER_INFO
v1.1 v1.2
(c) Institute for Medical Education - University of Bern (c) Institute for Medical Education - University of Bern
Licence: GNU GPL 3 Licence: GNU GPL 3
...@@ -45,15 +45,30 @@ USAGE: ...@@ -45,15 +45,30 @@ USAGE:
check_docker_info [OPTIONS] check_docker_info [OPTIONS]
OPTIONS: OPTIONS:
-h this help -h, --help this help
-d Debug; Flag: show all docker system infos -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: EXAMPLES:
check_docker_info check_docker_info
Show Status of all containers 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 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 ...@@ -84,3 +99,9 @@ Solution: you did not set SETENV in the sudoers config file
icingaclient ALL=(ALL) NOPASSWD:**SETENV:** /usr/bin/docker 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.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment