From e23e780a20b920e576291283d6229e11bb0d5603 Mon Sep 17 00:00:00 2001 From: Scott Fryer <60462088+steelhead31@users.noreply.github.com> Date: Wed, 27 Nov 2024 14:12:16 +0000 Subject: [PATCH] UnixPB: Add Docker Overlay Check Plugin Script For Nagios (#3825) * UnixPB: Add Docker Overlay Check Script For Nagios * Update ansible/playbooks/AdoptOpenJDK_Unix_Playbook/roles/Nagios_Plugins/tasks/additional_plugins/check_docker_overlay2_size.sh Co-authored-by: Stewart X Addison <6487691+sxa@users.noreply.github.com> * Update ansible/playbooks/AdoptOpenJDK_Unix_Playbook/roles/Nagios_Plugins/tasks/additional_plugins/check_docker_overlay2_size.sh Co-authored-by: Stewart X Addison <6487691+sxa@users.noreply.github.com> --------- Co-authored-by: Stewart X Addison <6487691+sxa@users.noreply.github.com> --- .../check_docker_overlay2_size.sh | 66 +++++++++++++++++++ .../roles/Nagios_Plugins/tasks/main.yml | 6 ++ 2 files changed, 72 insertions(+) create mode 100644 ansible/playbooks/AdoptOpenJDK_Unix_Playbook/roles/Nagios_Plugins/tasks/additional_plugins/check_docker_overlay2_size.sh diff --git a/ansible/playbooks/AdoptOpenJDK_Unix_Playbook/roles/Nagios_Plugins/tasks/additional_plugins/check_docker_overlay2_size.sh b/ansible/playbooks/AdoptOpenJDK_Unix_Playbook/roles/Nagios_Plugins/tasks/additional_plugins/check_docker_overlay2_size.sh new file mode 100644 index 0000000000..3293aa96e3 --- /dev/null +++ b/ansible/playbooks/AdoptOpenJDK_Unix_Playbook/roles/Nagios_Plugins/tasks/additional_plugins/check_docker_overlay2_size.sh @@ -0,0 +1,66 @@ +#!/bin/bash + +# Nagios plugin to check the size of /var/lib/docker/overlay2 +# Allows passing thresholds as parameters + +# Default thresholds (in GB) +DEFAULT_WARN_THRESHOLD=20 +DEFAULT_CRIT_THRESHOLD=40 + +# Folder to check +TARGET_DIR="/var/lib/docker/overlay2/" + +# Function to display usage +usage() { + echo "Usage: $0 -w -c " + echo " -w: Warning threshold in GB (default: $DEFAULT_WARN_THRESHOLD)" + echo " -c: Critical threshold in GB (default: $DEFAULT_CRIT_THRESHOLD)" + exit 3 +} + +# Parse command-line arguments +while getopts "w:c:" opt; do + case $opt in + w) WARN_THRESHOLD=$OPTARG ;; + c) CRIT_THRESHOLD=$OPTARG ;; + *) usage ;; + esac +done + +# Set default thresholds if not provided +WARN_THRESHOLD=${WARN_THRESHOLD:-$DEFAULT_WARN_THRESHOLD} +CRIT_THRESHOLD=${CRIT_THRESHOLD:-$DEFAULT_CRIT_THRESHOLD} + +# Convert GB to bytes +WARN_THRESHOLD_BYTES=$((WARN_THRESHOLD * 1024 * 1024 * 1024)) +CRIT_THRESHOLD_BYTES=$((CRIT_THRESHOLD * 1024 * 1024 * 1024)) + +# Check if the directory exists +if [[ ! -d "$TARGET_DIR" ]]; then + echo "CRITICAL: Directory $TARGET_DIR does not exist." + exit 2 +fi + +# Get the size of the folder in bytes +FOLDER_SIZE_BYTES=$(du -sb "$TARGET_DIR" 2>/dev/null | awk '{print $1}') + +# Handle error if du fails +if [[ -z "$FOLDER_SIZE_BYTES" ]]; then + echo "CRITICAL: Failed to determine the size of $TARGET_DIR." + exit 2 +fi + +# Convert size to GB for display +FOLDER_SIZE_GB=$(echo "scale=2; $FOLDER_SIZE_BYTES / (1024 * 1024 * 1024)" | bc) + +# Determine Nagios status +if (( FOLDER_SIZE_BYTES > CRIT_THRESHOLD_BYTES )); then + echo "CRITICAL: $TARGET_DIR size is ${FOLDER_SIZE_GB}GB (Threshold: >${CRIT_THRESHOLD}GB)" + exit 2 +elif (( FOLDER_SIZE_BYTES > WARN_THRESHOLD_BYTES )); then + echo "WARNING: $TARGET_DIR size is ${FOLDER_SIZE_GB}GB (Threshold: >${WARN_THRESHOLD}GB)" + exit 1 +else + echo "OK: $TARGET_DIR size is ${FOLDER_SIZE_GB}GB (Threshold: <=${WARN_THRESHOLD}GB)" + exit 0 +fi diff --git a/ansible/playbooks/AdoptOpenJDK_Unix_Playbook/roles/Nagios_Plugins/tasks/main.yml b/ansible/playbooks/AdoptOpenJDK_Unix_Playbook/roles/Nagios_Plugins/tasks/main.yml index ba6be0fbc8..13c7552c50 100644 --- a/ansible/playbooks/AdoptOpenJDK_Unix_Playbook/roles/Nagios_Plugins/tasks/main.yml +++ b/ansible/playbooks/AdoptOpenJDK_Unix_Playbook/roles/Nagios_Plugins/tasks/main.yml @@ -107,3 +107,9 @@ src: roles/Nagios_Plugins/tasks/additional_plugins/check_container_spaces.sh dest: /usr/local/nagios/libexec/check_container_spaces.sh mode: 0755 + +- name: Copy Docker Overlay2 Size Plugin + copy: + src: roles/Nagios_Plugins/tasks/additional_plugins/check_docker_overlay2_size.sh + dest: /usr/local/nagios/libexec/check_docker_overlay2_size.sh + mode: 0755