diff --git a/check_docker_info b/check_docker_info index 2d0ec8325dfddb6f455481a1fa8465c195375b75..11278c516c773692a452a121f0e44b9a3181301d 100755 --- a/check_docker_info +++ b/check_docker_info @@ -17,13 +17,14 @@ # 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 # 2024-01-29 v1.4 <axel.hahn@unibe.ch> add --target option +# 2024-02-13 v1.5 <axel.hahn@unibe.ch> update docker ps --format # ====================================================================== . $(dirname $0)/inc_pluginfunctions . $(dirname $0)/inc_dockerfunctions.sh -self_APPVERSION=1.4 +self_APPVERSION=1.5 # ---------------------------------------------------------------------- # FUNCTIONS @@ -176,7 +177,9 @@ echo "$out" if [ $bOptContainers -eq 1 ] && [ "$iCTotal" -gt "0" ]; then echo echo "Containers:" - containers=$( sudo -n --preserve-env docker ps --all --format "{{ json . }}" --all 2>/dev/null ) + # containers=$( sudo -n --preserve-env docker ps --all --format "{{ json . }}" --all 2>/dev/null ) + containers=$( sudo -n --preserve-env docker ps --all --format '{"Names": "{{.Names}}", "State": "{{.State}}", "Status": "{{.Status}}" }' 2>/dev/null ) + echo "$containers" | while read -r line; do sName=$( _getString "$line" ".Names" ) sState=$( _getString "$line" ".State" ) diff --git a/check_dockercontainer_info b/check_dockercontainer_info new file mode 100755 index 0000000000000000000000000000000000000000..1ac4e1f472b09d1d1139125fb839ec3081e0e5ac --- /dev/null +++ b/check_dockercontainer_info @@ -0,0 +1,163 @@ +#!/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 + +# ----------------------------------------------------------------------