#!/bin/bash # ====================================================================== # # Check DOCKER PROCESSES # # requirements: # - docker # - sudo permissions on docker command # - jq # # ---------------------------------------------------------------------- # Cli docs: # https://docs.docker.com/engine/reference/commandline/docker/ # ---------------------------------------------------------------------- # 2024-01-26 v1.0 <axel.hahn@unibe.ch> init # 2024-01-29 v1.1 <axel.hahn@unibe.ch> fix help; # ====================================================================== . $(dirname $0)/inc_pluginfunctions . $(dirname $0)/inc_dockerfunctions.sh self_APPVERSION=1.1 # ---------------------------------------------------------------------- # FUNCTIONS # ---------------------------------------------------------------------- # show help function _showHelp(){ local _self=$( basename $0 ) cat <<EOH $( ph.showImlHelpHeader ) Show counts and processes of docker containers. 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: General: -h, --help this help -d, --debug Debug; Flag: show docker env values. Connect to docker: -p, --path Custom directory for docker binary -t, --target Custom docker target; value for DOCKER_HOST Needed only if Docker does not run on a unix socket or multiple users run a rootless docker daemon. EXAMPLES: $_self Show processes of all containers $_self -p /usr/bin Show processes of all containers. The docker binary will be searched in given path first - then in all other dirs of \$PATH $_self -d Show processes of all containers and json with docker env infos. EOH } # ---------------------------------------------------------------------- # MAIN # ---------------------------------------------------------------------- bOptDebug=0 while [[ "$#" -gt 0 ]]; do case $1 in -h|--help) _showHelp; exit 0;; -d|--debug) bOptDebug=1; shift;; -p|--path) if ! grep ":{$2}:" <<< ":{$PATH}:" >/dev/null; then PATH="$2:$PATH"; fi shift; shift;; -t|--target) export DOCKER_HOST="$2"; shift; shift;; *) echo "ERROR: Unknown parameter: $1"; _showHelp; exit 1; esac; done ph.require "docker" ph.require "jq" _detectDockerenv # --- fetch data data=$( sudo -n --preserve-env docker ps --format "{{ json . }}" 2>/dev/null ) _detectDockererror "$data" # --- collect data out=$( echo "$data" | while read -r line; do id=$( echo "$line" | jq -r ".ID") name=$( echo "$line" | jq -r ".Names") pslist=$( sudo -n --preserve-env docker top "$id" "-o user,pid,pcpu,pmem,command" 2>/dev/null ) typeset -i ipLines; ipLines=$( echo "$pslist" | wc -l )-1 echo "----------| $ipLines |-- $name (id: $id) ----- " echo "$pslist" done ) typeset -i iContainers; iContainers=$( echo "$data" | wc -l ) typeset -i iLines; iLines=$( echo "$out" | wc -l ) typeset -i iProcesses; iProcesses=$(( iLines-iContainers*2 )) # --- output ph.perfadd "processes" "$iProcesses" "" "" ph.status "Docker processes: $iProcesses .. containers: $iContainers" echo "$out" # 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 ; _debugInfos) ph.exit # ----------------------------------------------------------------------