Skip to content
Snippets Groups Projects
Commit b40c4838 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

detect DOCKER; use sudo; add debug

parent c2e18fe9
No related branches found
No related tags found
1 merge request!230Docker checks
...@@ -5,17 +5,21 @@ ...@@ -5,17 +5,21 @@
# #
# requirements: # requirements:
# - docker # - docker
# - sudo permissions on docker command
# #
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# Cli docs: # Cli docs:
# https://docs.docker.com/engine/reference/commandline/docker/ # https://docs.docker.com/engine/reference/commandline/docker/
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# 2024-01-18 v1.0 <axel.hahn@unibe.ch> init # 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 . $(dirname $0)/inc_pluginfunctions
self_APPVERSION=1.0 . $(dirname $0)/inc_dockerfunctions.sh
self_APPVERSION=1.1
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# FUNCTIONS # FUNCTIONS
...@@ -30,38 +34,39 @@ $( ph.showImlHelpHeader ) ...@@ -30,38 +34,39 @@ $( ph.showImlHelpHeader )
Show docker version and count of containers total and by its status. 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: USAGE:
$_self [OPTIONS] $_self [OPTIONS]
OPTIONS: OPTIONS:
-h this help -h this help
-d Debug; Flag: show all docker system infos
EXAMPLES: EXAMPLES:
$_self $_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 EOH
# 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 '"'
} }
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# MAIN # MAIN
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
ph.hasParamoption "h" "$@"; bOptHelp=$? ph.hasParamoption "h" "$@"; bOptHelp=$?
ph.hasParamoption "d" "$@"; bOptDebug=$?
if [ $bOptHelp -eq 0 ]; then if [ $bOptHelp -eq 0 ]; then
_usage _usage
...@@ -71,10 +76,12 @@ fi ...@@ -71,10 +76,12 @@ fi
ph.require "docker" ph.require "docker"
ph.require "jq" ph.require "jq"
_detectDockerenv
# --- get data # --- get data
data=$( docker system info --format "{{ json . }}" ) data=$( sudo -n --preserve-env docker system info --format '{{ json . }}' )
_detectDockererror "$data"
typeset -i iCTotal; typeset -i iCTotal;
iCTotal=$( _getString "$data" ".Containers" ) iCTotal=$( _getString "$data" ".Containers" )
...@@ -87,15 +94,19 @@ sLicense=$( _getString "$data" ".ProductLicense" ) ...@@ -87,15 +94,19 @@ sLicense=$( _getString "$data" ".ProductLicense" )
# --- generate result # --- generate result
if [ "$iCRunning" -eq "0" ]; then if [ "$iCTotal" -eq "0" ]; then
ph.setStatus critical out+="No container was created yet."
out+="No container is running"
else else
if [ "$iCRunning" -ne "$iCTotal" ]; then if [ "$iCRunning" -eq "0" ]; then
ph.setStatus warning ph.setStatus critical
out+="Not all containers are running" out+="No container is running"
else 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
fi fi
...@@ -109,6 +120,8 @@ ph.perfadd "images" "$iImages" ...@@ -109,6 +120,8 @@ ph.perfadd "images" "$iImages"
ph.status "Docker $sVersion ($sLicense) .. containers: $iCTotal running: $iCRunning paused: $iCPaused stopped: $iCStopped .. images: $iImages" ph.status "Docker $sVersion ($sLicense) .. containers: $iCTotal running: $iCRunning paused: $iCPaused stopped: $iCStopped .. images: $iImages"
echo "$out" 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 ph.exit
......
#!/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
}
# ----------------------------------------------------------------------
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment