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

Merge branch 'docker-checks' into 'master'

Docker checks: remove emoji icons; handle "null" in license info; show rootless or not

See merge request !233
parents 9adc3877 23f3327b
No related branches found
No related tags found
1 merge request!233Docker checks: remove emoji icons; handle "null" in license info; show rootless or not
......@@ -14,13 +14,14 @@
# 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
# 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
# ======================================================================
. $(dirname $0)/inc_pluginfunctions
. $(dirname $0)/inc_dockerfunctions.sh
self_APPVERSION=1.2
self_APPVERSION=1.3
# ----------------------------------------------------------------------
# FUNCTIONS
......@@ -117,6 +118,7 @@ iImages=$( _getString "$data" ".Images" )
sVersion=$( _getString "$data" ".ServerVersion")
sLicense=$( _getString "$data" ".ProductLicense" )
# --- generate result
if [ "$iCTotal" -eq "0" ]; then
......@@ -148,7 +150,15 @@ ph.perfadd "images" "$iImages"
# --- output
ph.status "Docker $sVersion ($sLicense) .. containers: $iCTotal running: $iCRunning paused: $iCPaused stopped: $iCStopped .. images: $iImages"
sStatus="Docker $sVersion ("
test -n "$sLicense" && sStatus+="$sLicense, "
test "$_is_docker_rootless" -eq "0" && sStatus+="as root"
test "$_is_docker_rootless" -eq "1" && sStatus+="rootless"
sStatus+=") .. "
sStatus+="containers: $iCTotal running: $iCRunning paused: $iCPaused stopped: $iCStopped .. "
sStatus+="images: $iImages"
ph.status "$sStatus"
echo "$out"
if [ $bOptContainers -eq 1 ] && [ "$iCTotal" -gt "0" ]; then
......@@ -159,20 +169,21 @@ if [ $bOptContainers -eq 1 ] && [ "$iCTotal" -gt "0" ]; then
sName=$( _getString "$line" ".Names" )
sState=$( _getString "$line" ".State" )
sStatus=$( _getString "$line" ".Status" )
sIco="🔶"
grep "exited" <<< "${sState}" >/dev/null && sIco="❌"
grep "running" <<< "${sState}" >/dev/null && sIco="✅"
sIco=
# Icinga ui does not show these characters
# sIco="🔶"
# grep "exited" <<< "${sState}" >/dev/null && sIco="❌"
# grep "running" <<< "${sState}" >/dev/null && sIco="✅"
printf " %-14s %-40s %-20s\n" "$sIco $sState" "$sName" "$sStatus"
# echo "$line" | jq
done
echo
fi
# 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 )
test $bOptDebug -eq 1 && ( echo; echo "DEBUG: full docker system infos as json"; echo "$data" | jq ; _debugInfos)
ph.exit
......
......@@ -15,13 +15,28 @@ This check sends performance data.
icingaclient ALL=(ALL) NOPASSWD:SETENV: /usr/bin/docker
```
## Includes
Additional needed files in the current folder that this check can run:
* inc_dockerfunctions.sh
* inc_pluginfunctions
## Rootless docker
It works with docker setups as root and can handle rootless docker instances.
To bring it up and running with an unpriviledged icinga user it must be able to access docker. Copy the docker binary eg. from /home/dockeruser/bin/docker to /usr/bin/. Then use the parameter `-p /usr/bin`.
If DOCKER_HOST is not set then the docker socket is detected from a running docker instance and will be fetched from the process list.
## Syntax
```txt
______________________________________________________________________
CHECK_DOCKER_INFO
v1.2
v1.3
(c) Institute for Medical Education - University of Bern
Licence: GNU GPL 3
......@@ -81,7 +96,7 @@ None.
`$ ./check_docker_info` returns
```txt
OK: Docker 20.10.14 (Community Engine) .. containers: 2 running: 2 paused: 0 stopped: 0 .. images: 5
OK: Docker 20.10.14 (Community Engine, rootless) .. containers: 2 running: 2 paused: 0 stopped: 0 .. images: 5
All containers are running
|containers-running=2;;;0;2 containers-paused=0;;;0;2 containers-stopped=0;;;0;2 images=33;;
```
......
......@@ -17,6 +17,9 @@
. $(dirname $0)/inc_pluginfunctions
_is_docker_rootless=0
_is_docker_detected=0
# ----------------------------------------------------------------------
# FUNCTIONS
# ----------------------------------------------------------------------
......@@ -32,19 +35,33 @@ function _filterJson(){
# param string json data
# param string jq filter
function _getString(){
_filterJson "$1" "$2" | tr -d '"'
_filterJson "$1" "$2" | tr -d '"' | sed "s#^null\$##"
}
# 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(){
local dockeruid=
if [ -z "$DOCKER_HOST" ]; then
_is_docker_detected=1
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
if grep "/run/user/[0-9]*" <<< "$DOCKER_HOST" >/dev/null; then
_is_docker_rootless=1
fi
}
# show some debug infos to be shown if needed.
function _debugInfos(){
echo
echo "DEBUG INFOS:"
echo "DOCKER_HOST = $DOCKER_HOST"
test "$_is_docker_detected" -eq 0 && echo "It was set in the environment."
test "$_is_docker_detected" -eq 1 && echo "It was detected from process list."
echo
}
# detect error after dicker command. It stops if
# - no content was fetched
# - content contains key "ServerErrors"
......@@ -57,6 +74,7 @@ function _detectDockererror(){
ph.exit
fi
local sSrvErrror;
sSrvErrror=$( _filterJson "$data" ".ServerErrors" )
if [ "$sSrvErrror" != "null" ] ; then
ph.setStatus unknown
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment