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

Merge branch 'docker-checks' into 'master'

Docker checks

See merge request !230
parents 066bf511 ea97b5fd
No related branches found
No related tags found
1 merge request!230Docker checks
......@@ -5,17 +5,21 @@
#
# requirements:
# - docker
# - sudo permissions on docker command
#
# ----------------------------------------------------------------------
# Cli docs:
# https://docs.docker.com/engine/reference/commandline/docker/
# ----------------------------------------------------------------------
# 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
self_APPVERSION=1.0
. $(dirname $0)/inc_dockerfunctions.sh
self_APPVERSION=1.1
# ----------------------------------------------------------------------
# FUNCTIONS
......@@ -30,38 +34,39 @@ $( ph.showImlHelpHeader )
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:
$_self [OPTIONS]
OPTIONS:
-h this help
-d Debug; Flag: show all docker system infos
EXAMPLES:
$_self
Show Status of all containers
EOH
}
# filter json data with jq
# param string json data
# param string jq filter
function _filterJson(){
echo "$1" | jq "$2"
}
$_self -d
Show Status of all containers and json with all docker system infos
# 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 '"'
EOH
}
# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------
ph.hasParamoption "h" "$@"; bOptHelp=$?
ph.hasParamoption "d" "$@"; bOptDebug=$?
if [ $bOptHelp -eq 0 ]; then
_usage
......@@ -71,10 +76,12 @@ fi
ph.require "docker"
ph.require "jq"
_detectDockerenv
# --- get data
data=$( docker system info --format "{{ json . }}" )
data=$( sudo -n --preserve-env docker system info --format '{{ json . }}' )
_detectDockererror "$data"
typeset -i iCTotal;
iCTotal=$( _getString "$data" ".Containers" )
......@@ -87,17 +94,21 @@ sLicense=$( _getString "$data" ".ProductLicense" )
# --- generate result
if [ "$iCTotal" -eq "0" ]; then
out+="No container was created yet."
else
if [ "$iCRunning" -eq "0" ]; then
ph.setStatus critical
out+="No container is running"
else
if [ "$iCRunning" -ne "$iCTotal" ]; then
ph.setStatus warning
ph.setStatus critical
out+="Not all containers are running"
else
out+="All containers are running"
fi
fi
fi
ph.perfadd "containers-running" "$iCRunning" "" "" 0 "$iCTotal"
ph.perfadd "containers-paused" "$iCPaused" "" "" 0 "$iCTotal"
......@@ -109,6 +120,8 @@ ph.perfadd "images" "$iImages"
ph.status "Docker $sVersion ($sLicense) .. containers: $iCTotal running: $iCRunning paused: $iCPaused stopped: $iCStopped .. images: $iImages"
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
......
......@@ -9,6 +9,11 @@ This check sends performance data.
## Requirements
* `docker` Docker must be installed
* sudo permissions on docker command
```txt
icingaclient ALL=(ALL) NOPASSWD:SETENV: /usr/bin/docker
```
## Syntax
......@@ -16,7 +21,7 @@ This check sends performance data.
______________________________________________________________________
CHECK_DOCKER_INFO
v1.0
v1.1
(c) Institute for Medical Education - University of Bern
Licence: GNU GPL 3
......@@ -26,14 +31,29 @@ ______________________________________________________________________
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:
check_docker_info [OPTIONS]
OPTIONS:
-h this help
-d Debug; Flag: show all docker system infos
EXAMPLES:
check_docker_info
Show Status of all containers
check_docker_info -d
Show Status of all containers and json with all docker system infos
```
......@@ -50,3 +70,17 @@ OK: Docker 20.10.14 (Community Engine) .. containers: 2 running: 2 paused: 0 sto
All containers are running
|containers-running=2;;;0;2 containers-paused=0;;;0;2 containers-stopped=0;;;0;2 images=33;;
```
## Troubleshooting
### Missing environment
```txt
sudo: sorry, you are not allowed to preserve the environment
UNKNOWN: No data. Unable to fetch Docker information.
```
Solution: you did not set SETENV in the sudoers config file
icingaclient ALL=(ALL) NOPASSWD:**SETENV:** /usr/bin/docker
#!/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