#!/bin/bash # ====================================================================== # # Check DOCKERCONTAINER INFOS # # requirements: # - docker # - sudo permissions on docker command # # ---------------------------------------------------------------------- # Cli docs: # https://docs.docker.com/engine/reference/commandline/docker/ # ---------------------------------------------------------------------- # 2024-01-18 v0.1 <axel.hahn@unibe.ch> init # 2024-02-13 v1.0 <axel.hahn@unibe.ch> update docker ps --format # ====================================================================== . $(dirname $0)/inc_pluginfunctions . $(dirname $0)/inc_dockerfunctions.sh self_APPVERSION=1.0 # ---------------------------------------------------------------------- # FUNCTIONS # ---------------------------------------------------------------------- # show help function _usage(){ local _self=$( basename $0 ) cat <<EOH $( ph.showImlHelpHeader ) List existing containers and show container details. To show details you can enter an id or define a regex USAGE: $_self [OPTIONS] OPTIONS: -h Show this help and exit. -s Show containers using 'docker ps --all' and exit. -i ID Show details for container with given id. -f REGEX Show details for first matching container with given regex in output of 'docker ps --all'. If the options -i and -f are used then -i ID has priority. PARAMETERS: ID Hexadcimal value for the container id REGEX String to filter the container list. With it you can set an image name or a container name. EXAMPLES: $_self -s Show all containers $_self -i 0356f42ed27d Show container details of container with id 0356f42ed27d $_self -f myapp Show container details of first container that matches "myapp" in the output of the container list. EOH } # ---------------------------------------------------------------------- # MAIN # ---------------------------------------------------------------------- ph.hasParamoption "h" "$@"; bOptHelp=$? if [ $bOptHelp -eq 0 ]; then _usage exit 0 fi if [ -z "$1" ]; then ph.setStatus critical ph.status "No parameters were given. Use -h to get help." ph.exit fi ph.require "docker" ph.require "jq" if ph.hasParamoption "s" "$@"; then echo "List of all containers (docker ps --all):" echo docker ps --all echo exit 0 fi # --- get data id=$(ph.getValueWithParam '' i "$@") regex=$(ph.getValueWithParam '' f "$@") if [ -z "$id" ]; then if [ -n "$regex" ]; then id=$( docker ps --all | grep -E "$regex" | head -1 | awk '{ print $1 }' ) fi fi if [ -z "$id" ]; then ph.setStatus critical ph.status "No Docker id was given / found. Use -s to show all containers." ph.exit fi _detectDockerenv # data=$( sudo -n --preserve-env docker ps --all --format "{{ json . }}" --filter "id=$id" 2>/dev/null ) data=$( sudo -n --preserve-env docker ps --all --format '{ "Names": "{{.Names}}", "State": "{{.State}}", "Status": "{{.Status}}", "Status": "{{.Status}}", "RunningFor": "{{.RunningFor}}", "Image": "{{.Image}}", "CreatedAt": "{{.CreatedAt}}", "Size": "{{.Size}}", "Mounts": "{{.Mounts}}", "Ports": "{{.Ports}}" }' --filter "id=$id" 2>/dev/null ) if [ -z "$data" ] ; then ph.setStatus critical ph.status "Docker id [$id] does not exist. Use -s to show all containers." ph.exit fi sState=$( _getString "$data" ".State" ) sCreated=$(_getString "$data" ".RunningFor" ) sStatus=$( _getString "$data" ".Status" ) # --- generate result test "$sState" != "running" && ph.setStatus warning test "$sState" = "exited" && ph.setStatus critical # --- output ph.status "Docker id [$id] $sState .. created $sCreated .. $sStatus" echo " NAME $( _getString "$data" ".Names" ) image : $( _getString "$data" ".Image" ) created: $( _getString "$data" ".CreatedAt" ) .. $sCreated size : $( _getString "$data" ".Size" ) mounts : $( _getString "$data" ".Mounts" ) ports : $( _getString "$data" ".Ports" | grep . || echo '(None)') " ph.exit # ----------------------------------------------------------------------