Skip to content
Snippets Groups Projects

add check_dockercontainer_top

Merged Hahn Axel (hahn) requested to merge docker-checks into master
1 file
+ 138
0
Compare changes
  • Side-by-side
  • Inline
+ 138
0
 
#!/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
 
# ======================================================================
 
 
 
. $(dirname $0)/inc_pluginfunctions
 
. $(dirname $0)/inc_dockerfunctions.sh
 
 
self_APPVERSION=1.0
 
 
# ----------------------------------------------------------------------
 
# 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:
 
-h, --help this help
 
-d, --debug Debug; Flag: show all docker system infos
 
-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 -c
 
Show processes 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 processes of all containers. If not all containers are running you
 
get a list of container names with its status.
 
 
$_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 all docker system 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
 
 
# ----------------------------------------------------------------------
Loading