From 460bbf010624c8bcd5226f9220ade838f3cd96d4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Th=C3=A9o?= <theo@le-filament.com>
Date: Mon, 9 May 2022 14:43:25 +0200
Subject: [PATCH] add check_docker shell script

---
 check_docker.sh | 103 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 103 insertions(+)
 create mode 100755 check_docker.sh

diff --git a/check_docker.sh b/check_docker.sh
new file mode 100755
index 0000000..51ad6a6
--- /dev/null
+++ b/check_docker.sh
@@ -0,0 +1,103 @@
+#!/bin/bash
+
+# Default threshold values.
+cpu_threshold_warning=25
+cpu_threshold_critical=50
+mem_threshold_warning=50
+mem_threshold_critical=75
+filter=''
+
+print_help() {
+    echo "usage: ${0} [OPTIONS]"
+    echo "options:"
+    echo -e " -c, --cpu <warning:critical>\t\tthreshold values for cpu usage"
+    echo -e " -f, --filter <regex>\t\t\tcontainers to check"
+    echo -e " -h, --help\t\t\t\tprint this help"
+    echo -e " -m, --memory <warning:critical>\tthreshold values for memory usage"
+}
+
+while [[ $# -gt 0 ]]; do
+    case "$1" in
+        -c|--cpu)
+            cpu_threshold_warning=`awk -F ':' '{print +$1}' <(echo $2)`
+            cpu_threshold_critical=`awk -F ':' '{print +$2}' <(echo $2)`
+            shift 2
+            ;;
+
+        -f|--filter)
+            filter=$2
+            shift 2
+            ;;
+
+        -m|--memory)
+            mem_threshold_warning=`awk -F ':' '{print +$1}' <(echo $2)`
+            mem_threshold_critical=`awk -F ':' '{print +$2}' <(echo $2)`
+            shift 2
+            ;;
+
+        --help|-h)
+            print_help
+            exit 0
+            ;;
+
+        *)
+            echo "Error while parsing argument '$1'."
+            exit 1
+            ;;
+    esac
+done
+
+# Gathering facts.
+all_containers=`sudo docker ps --format '{{.Names}}' --filter "name=${filter}" --no-trunc --quiet --all`
+running_containers=`sudo docker ps --format '{{.Names}}' --filter "name=${filter}" --no-trunc --quiet`
+running_containers_stats=`sudo docker stats --format '{{.Name}}:{{.CPUPerc}}:{{.MemPerc}}' --no-stream`
+
+not_running_containers=`comm -23 <(echo $all_containers) <(echo $running_containers)`
+
+# Format data.
+output=''
+perf=''
+
+cpu_scale=";${cpu_threshold_warning};${cpu_threshold_critical};0;100"
+mem_scale=";${mem_threshold_warning};${mem_threshold_critical};0;100"
+
+# Running containers.
+for container in ${running_containers}; do
+    output+=" OK: ${container} status is running"
+done
+# Not running containers.
+for container in ${not_running_containers}; do
+    output+=" NOK: ${container} status is not running"
+done
+
+# CPU and memory statistics for running containers.
+for container_stats in ${running_containers_stats}; do
+    name=`awk -F ':' '{print $1}' <(echo $container_stats)`
+    [[ ! $name =~ ${filter} ]] && continue
+    cpu=`awk -F ':' '{print +$2}' <(echo $container_stats)`
+    mem=`awk -F ':' '{print +$3}' <(echo $container_stats)`
+
+    status='OK'
+    [[ ${cpu%.*} -ge $cpu_threshold_warning ]] && status='WARNING'
+    [[ ${cpu%.*} -ge $cpu_threshold_critical ]] && status='CRITICAL'
+    output+=" ${status}: ${name} cpu is ${cpu}%"
+
+    status='OK'
+    [[ ${mem%.*} -ge $mem_threshold_warning ]] && status='WARNING'
+    [[ ${mem%.*} -ge $mem_threshold_critical ]] && status='CRITICAL'
+    output+=" ${status}: ${name} memory is ${mem}%"
+
+    perf+=" ${name}_cpu=${cpu}%${cpu_scale}"
+    perf+=" ${name}_mem=${mem}%${mem_scale}"
+done
+
+# Nagios OK status.
+exit_code=0
+# Nagios WARNING status.
+grep --quiet 'WARNING:' <(echo $output) && exit_code=1
+# Nagios CRITICAL status.
+grep --quiet 'NOK:\|CRITICAL:' <(echo $output) && exit_code=2
+
+# Plugin output.
+echo "${output#' '}|${perf#' '}"
+exit $exit_code
-- 
GitLab