diff --git a/check_docker_info b/check_docker_info index 06947defe4a3da083e6d84a63877d9b681626c22..a7c55506c7cec68ee8e46600295000b400812346 100755 --- a/check_docker_info +++ b/check_docker_info @@ -14,13 +14,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 +# 2024-01-24 v1.3 <axel.hahn@unibe.ch> remove emoji icons; handle "null" in license info; show rootless or not # ====================================================================== . $(dirname $0)/inc_pluginfunctions . $(dirname $0)/inc_dockerfunctions.sh -self_APPVERSION=1.2 +self_APPVERSION=1.3 # ---------------------------------------------------------------------- # FUNCTIONS @@ -117,6 +118,7 @@ iImages=$( _getString "$data" ".Images" ) sVersion=$( _getString "$data" ".ServerVersion") sLicense=$( _getString "$data" ".ProductLicense" ) + # --- generate result if [ "$iCTotal" -eq "0" ]; then @@ -148,7 +150,15 @@ ph.perfadd "images" "$iImages" # --- output -ph.status "Docker $sVersion ($sLicense) .. containers: $iCTotal running: $iCRunning paused: $iCPaused stopped: $iCStopped .. images: $iImages" +sStatus="Docker $sVersion (" +test -n "$sLicense" && sStatus+="$sLicense, " +test "$_is_docker_rootless" -eq "0" && sStatus+="as root" +test "$_is_docker_rootless" -eq "1" && sStatus+="rootless" +sStatus+=") .. " +sStatus+="containers: $iCTotal running: $iCRunning paused: $iCPaused stopped: $iCStopped .. " +sStatus+="images: $iImages" + +ph.status "$sStatus" echo "$out" if [ $bOptContainers -eq 1 ] && [ "$iCTotal" -gt "0" ]; then @@ -159,20 +169,21 @@ if [ $bOptContainers -eq 1 ] && [ "$iCTotal" -gt "0" ]; then 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="✅" + sIco= + # Icinga ui does not show these characters + # 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 1 && ( 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 ; _debugInfos) ph.exit diff --git a/docs/20_Checks/check_docker_info.md b/docs/20_Checks/check_docker_info.md index b2f331b17af18d272a30383d3e13551d6fcbe97a..9a9748a1cf93969aa844c83581d67d5c2c08d7af 100644 --- a/docs/20_Checks/check_docker_info.md +++ b/docs/20_Checks/check_docker_info.md @@ -15,13 +15,28 @@ This check sends performance data. icingaclient ALL=(ALL) NOPASSWD:SETENV: /usr/bin/docker ``` +## Includes + +Additional needed files in the current folder that this check can run: + +* inc_dockerfunctions.sh +* inc_pluginfunctions + +## Rootless docker + +It works with docker setups as root and can handle rootless docker instances. + +To bring it up and running with an unpriviledged icinga user it must be able to access docker. Copy the docker binary eg. from /home/dockeruser/bin/docker to /usr/bin/. Then use the parameter `-p /usr/bin`. + +If DOCKER_HOST is not set then the docker socket is detected from a running docker instance and will be fetched from the process list. + ## Syntax ```txt ______________________________________________________________________ CHECK_DOCKER_INFO -v1.2 +v1.3 (c) Institute for Medical Education - University of Bern Licence: GNU GPL 3 @@ -81,7 +96,7 @@ None. `$ ./check_docker_info` returns ```txt -OK: Docker 20.10.14 (Community Engine) .. containers: 2 running: 2 paused: 0 stopped: 0 .. images: 5 +OK: Docker 20.10.14 (Community Engine, rootless) .. containers: 2 running: 2 paused: 0 stopped: 0 .. images: 5 All containers are running |containers-running=2;;;0;2 containers-paused=0;;;0;2 containers-stopped=0;;;0;2 images=33;; ``` diff --git a/inc_dockerfunctions.sh b/inc_dockerfunctions.sh index 22da4ddad3902cdb9beecc9e31c871476c359e68..86383e80cbb080b3bf44d45320a0b93d9c9f8fb9 100644 --- a/inc_dockerfunctions.sh +++ b/inc_dockerfunctions.sh @@ -17,6 +17,9 @@ . $(dirname $0)/inc_pluginfunctions +_is_docker_rootless=0 +_is_docker_detected=0 + # ---------------------------------------------------------------------- # FUNCTIONS # ---------------------------------------------------------------------- @@ -32,19 +35,33 @@ function _filterJson(){ # param string json data # param string jq filter function _getString(){ - _filterJson "$1" "$2" | tr -d '"' + _filterJson "$1" "$2" | tr -d '"' | sed "s#^null\$##" } # if DOCKER_HOST is not set we try to detect the user running "containerd" # create an env var DOCKER_HOST="unix:///run/user/<ID>/docker.sock" function _detectDockerenv(){ + local dockeruid= if [ -z "$DOCKER_HOST" ]; then + _is_docker_detected=1 dockeruid=$( ps -ef | grep containerd | grep -Eo "/run/user/([0-9]*)/" | head -1 | cut -f 4 -d '/' ) test -n "$dockeruid" && export DOCKER_HOST="unix:///run/user/$dockeruid/docker.sock" # Don't abort - it is allowed that the variable DOCKER_HOST is missing fi + if grep "/run/user/[0-9]*" <<< "$DOCKER_HOST" >/dev/null; then + _is_docker_rootless=1 + fi } +# show some debug infos to be shown if needed. +function _debugInfos(){ + echo + echo "DEBUG INFOS:" + echo "DOCKER_HOST = $DOCKER_HOST" + test "$_is_docker_detected" -eq 0 && echo "It was set in the environment." + test "$_is_docker_detected" -eq 1 && echo "It was detected from process list." + echo +} # detect error after dicker command. It stops if # - no content was fetched # - content contains key "ServerErrors" @@ -57,6 +74,7 @@ function _detectDockererror(){ ph.exit fi + local sSrvErrror; sSrvErrror=$( _filterJson "$data" ".ServerErrors" ) if [ "$sSrvErrror" != "null" ] ; then ph.setStatus unknown