From 84dc3f6755b645ff4ab4f2e1cda073b5cdfce68c Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Thu, 18 Jan 2024 11:26:06 +0100
Subject: [PATCH] add docker info with count of containers

---
 check_docker_info                   | 115 ++++++++++++++++++++++++++++
 docs/20_Checks/_index.md            |   1 +
 docs/20_Checks/check_docker_info.md |  85 ++++++++++++++++++++
 3 files changed, 201 insertions(+)
 create mode 100755 check_docker_info
 create mode 100644 docs/20_Checks/check_docker_info.md

diff --git a/check_docker_info b/check_docker_info
new file mode 100755
index 0000000..db2fe9e
--- /dev/null
+++ b/check_docker_info
@@ -0,0 +1,115 @@
+#!/bin/bash
+# ======================================================================
+#
+# Check DOCKER INFOS
+#
+# requirements:
+# - docker
+#
+# ----------------------------------------------------------------------
+# Cli docs:
+# https://docs.docker.com/engine/reference/commandline/docker/
+# ----------------------------------------------------------------------
+# 2024-01-18  v1.0  <axel.hahn@unibe.ch>      init
+# ======================================================================
+
+
+. $(dirname $0)/inc_pluginfunctions
+self_APPVERSION=1.0
+
+# ----------------------------------------------------------------------
+# FUNCTIONS
+# ----------------------------------------------------------------------
+
+
+# show help
+function _usage(){
+    local _self=$( basename $0 )
+    cat <<EOH
+$( ph.showImlHelpHeader )
+
+Show docker version and count of containers total and by its status.
+
+USAGE:
+  $_self [OPTIONS]
+
+OPTIONS:
+  -h  this help
+
+EXAMPLES:
+  $_self
+
+EOH
+}
+
+# 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 '"'
+}
+
+
+# ----------------------------------------------------------------------
+# MAIN
+# ----------------------------------------------------------------------
+
+ph.hasParamoption "h" "$@"; bOptHelp=$?
+
+if [ $bOptHelp -eq 0 ]; then
+    _usage
+    exit 0
+fi
+
+ph.require "docker"
+ph.require "jq"
+
+
+# --- get data
+
+data=$( docker system info --format "{{ json . }}" )
+
+typeset -i iCTotal; 
+iCTotal=$(  _getString "$data" ".Containers" )
+iCRunning=$(_getString "$data" ".ContainersRunning" )
+iCPaused=$( _getString "$data" ".ContainersPaused" )
+iCStopped=$(_getString "$data" ".ContainersStopped" )
+iImages=$(  _getString "$data" ".Images" )
+sVersion=$( _getString "$data" ".ServerVersion")
+sLicense=$( _getString "$data" ".ProductLicense" )
+
+# --- generate result
+
+if [ "$iCRunning" -eq "0" ]; then
+    ph.setStatus critical
+    out+="No container is running"
+else
+    if [ "$iCRunning" -ne "$iCTotal" ]; then
+        ph.setStatus warning
+        out+="Not all containers are running"
+    else
+        out+="All containers are running"
+    fi
+fi
+
+ph.perfadd "containers-running"   "$iCRunning" "" "" 0 "$iCTotal"
+ph.perfadd "containers-paused"    "$iCPaused"  "" "" 0 "$iCTotal"
+ph.perfadd "containers-stopped"   "$iCStopped" "" "" 0 "$iCTotal"
+ph.perfadd "images"   "$iImages"
+
+# --- output
+
+ph.status "Docker $sVersion ($sLicense) .. containers: $iCTotal running: $iCRunning paused: $iCPaused stopped: $iCStopped .. images: $iImages"
+echo "$out"
+
+
+ph.exit
+
+# ----------------------------------------------------------------------
diff --git a/docs/20_Checks/_index.md b/docs/20_Checks/_index.md
index 5cb5fc2..720e28e 100644
--- a/docs/20_Checks/_index.md
+++ b/docs/20_Checks/_index.md
@@ -20,6 +20,7 @@ There is one include script used by all checks:
 * [check_cronstatus](check_cronstatus.md)
 * [check_disk-io](check_disk-io.md)
 * [check_dns_responsetime](check_dns_responsetime.md)
+* [check_docker_info](check_docker_info.md)
 * [check_eol](check_eol.md)
 * [check_fs_errors](check_fs_errors.md)
 * [check_fs_writable](check_fs_writable.md)
diff --git a/docs/20_Checks/check_docker_info.md b/docs/20_Checks/check_docker_info.md
new file mode 100644
index 0000000..2a7d4f3
--- /dev/null
+++ b/docs/20_Checks/check_docker_info.md
@@ -0,0 +1,85 @@
+# CHECK_DOCKER_INFO
+
+## Introduction
+
+**check_docker_info_** shows the docker version and count of containers.
+You get the count of containers by state running, paused or stopped.
+This check sends performance data.
+
+## Requirements
+
+* `docker` Docker must be installed
+
+## Syntax
+
+```txt
+______________________________________________________________________
+
+CHECK_DISK-IO 
+v1.1
+
+Check dis io and latency
+
+(c) Institute for Medical Education - University of Bern
+Licence: GNU GPL 3
+______________________________________________________________________
+
+Disk infos based on /sys/block/[NAME]/stat
+See https://www.kernel.org/doc/Documentation/block/stat.txt
+and https://www.kernel.org/doc/Documentation/iostats.txt
+
+The system data are counters that are difficult to read.
+The output of this check for each value a delta value per second since 
+last check.
+
+SYNTAX:
+check_disk-io -m MODE
+
+OPTIONS:
+
+    -m MODE        set mode for type of output (required)
+    -h or --help   show this help.
+
+PARAMETERS:
+
+    MODE
+        io         read I/Os, write I/Os, discard I/0s
+        ticks      read ticks, write ticks, discard ticks
+        wait       total wait time for all requests
+
+EXAMPLE:
+check_disk-io -m io
+
+```
+
+### Parameters
+
+`-m <MODE>` where MODE is a string to define kind of output.
+
+## Examples
+
+`$ check_disk-io -m io` returns
+
+```txt
+OK: Disk data ... read I/Os, write I/Os, discard I/0s, number of I/Os currently in flight 
+
+--- sda
+              disk-sda-ReadIO:          0 
+             disk-sda-WriteIO:          0 
+           disk-sda-DiscardIO:          0 
+            disk-sda-FlightIO:          0 
+
+--- nvme0n1
+          disk-nvme0n1-ReadIO:          3 
+         disk-nvme0n1-WriteIO:         16 
+       disk-nvme0n1-DiscardIO:          4 
+        disk-nvme0n1-FlightIO:          0 
+
+--- TOTAL
+                       ReadIO:          3 
+                      WriteIO:         16 
+                    DiscardIO:          4 
+                     FlightIO:          0 
+
+ |readio=3;; writeio=16;; discardio=4;; flightio=0;;
+```
-- 
GitLab