diff --git a/check_docker_info b/check_docker_info index db2fe9e07cc4186a9ce09816b797eac2138990b7..b84e04b6ecdb09cd07c855eabc85c5f3f8088551 100755 --- a/check_docker_info +++ b/check_docker_info @@ -5,17 +5,21 @@ # # requirements: # - docker +# - sudo permissions on docker command # # ---------------------------------------------------------------------- # Cli docs: # https://docs.docker.com/engine/reference/commandline/docker/ # ---------------------------------------------------------------------- # 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 # ====================================================================== . $(dirname $0)/inc_pluginfunctions -self_APPVERSION=1.0 +. $(dirname $0)/inc_dockerfunctions.sh + +self_APPVERSION=1.1 # ---------------------------------------------------------------------- # FUNCTIONS @@ -30,38 +34,39 @@ $( ph.showImlHelpHeader ) 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 + UNKNOWM if + - docker or jq were not found + - docker data were not fetched + - docker cannot be connected + +This check provides performance data. + USAGE: $_self [OPTIONS] OPTIONS: -h this help + -d Debug; Flag: show all docker system infos EXAMPLES: $_self + Show Status of all containers -EOH -} + $_self -d + Show Status of all containers and json with all docker system infos -# filter json data with jq -# param string json data -# param string jq filter -function _filterJson(){ - echo "$1" | jq "$2" -} - -# filter json data with jq (by expecting a single result) and remove quotes -# param string json data -# param string jq filter -function _getString(){ - _filterJson "$1" "$2" | tr -d '"' +EOH } - # ---------------------------------------------------------------------- # MAIN # ---------------------------------------------------------------------- ph.hasParamoption "h" "$@"; bOptHelp=$? +ph.hasParamoption "d" "$@"; bOptDebug=$? if [ $bOptHelp -eq 0 ]; then _usage @@ -71,10 +76,12 @@ fi ph.require "docker" ph.require "jq" +_detectDockerenv # --- get data -data=$( docker system info --format "{{ json . }}" ) +data=$( sudo -n --preserve-env docker system info --format '{{ json . }}' ) +_detectDockererror "$data" typeset -i iCTotal; iCTotal=$( _getString "$data" ".Containers" ) @@ -87,15 +94,19 @@ sLicense=$( _getString "$data" ".ProductLicense" ) # --- generate result -if [ "$iCRunning" -eq "0" ]; then - ph.setStatus critical - out+="No container is running" +if [ "$iCTotal" -eq "0" ]; then + out+="No container was created yet." else - if [ "$iCRunning" -ne "$iCTotal" ]; then - ph.setStatus warning - out+="Not all containers are running" + if [ "$iCRunning" -eq "0" ]; then + ph.setStatus critical + out+="No container is running" else - out+="All containers are running" + if [ "$iCRunning" -ne "$iCTotal" ]; then + ph.setStatus critical + out+="Not all containers are running" + else + out+="All containers are running" + fi fi fi @@ -109,6 +120,8 @@ ph.perfadd "images" "$iImages" ph.status "Docker $sVersion ($sLicense) .. containers: $iCTotal running: $iCRunning paused: $iCPaused stopped: $iCStopped .. images: $iImages" echo "$out" +# 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 ) ph.exit diff --git a/inc_dockerfunctions.sh b/inc_dockerfunctions.sh new file mode 100644 index 0000000000000000000000000000000000000000..22da4ddad3902cdb9beecc9e31c871476c359e68 --- /dev/null +++ b/inc_dockerfunctions.sh @@ -0,0 +1,69 @@ +#!/bin/bash +# ====================================================================== +# +# Shared functions for DOCKER checks +# +# requirements: +# - docker +# - jq +# +# ---------------------------------------------------------------------- +# Cli docs: +# https://docs.docker.com/engine/reference/commandline/docker/ +# ---------------------------------------------------------------------- +# 2024-01-19 v1.0 <axel.hahn@unibe.ch> init +# ====================================================================== + + +. $(dirname $0)/inc_pluginfunctions + +# ---------------------------------------------------------------------- +# FUNCTIONS +# ---------------------------------------------------------------------- + +# filter json data with jq +# param string json data +# param string jq filter +function _filterJson(){ + echo "$1" | jq "$2" +} + +# filter json data with jq (by expecting a single result) and remove quotes +# param string json data +# param string jq filter +function _getString(){ + _filterJson "$1" "$2" | tr -d '"' +} + +# 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(){ + if [ -z "$DOCKER_HOST" ]; then + 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 +} + +# detect error after dicker command. It stops if +# - no content was fetched +# - content contains key "ServerErrors" +function _detectDockererror(){ + + local data="$1" + if [ -z "$data" ] ; then + ph.setStatus unknown + ph.status "No data. Unable to fetch Docker information." + ph.exit + fi + + sSrvErrror=$( _filterJson "$data" ".ServerErrors" ) + if [ "$sSrvErrror" != "null" ] ; then + ph.setStatus unknown + ph.status "Unable to connect to Docker:" + echo "$sSrvErrror" | grep "^ " + ph.exit + fi + +} +# ----------------------------------------------------------------------