From a4639f158d07eb826151447336c75d13b8d0ee4b Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Sun, 14 Jul 2024 19:47:16 +0200 Subject: [PATCH] Add check cron mode --- README.md | 6 ++-- check_container_upgrade | 68 +++++++++++++++++++++++------------------ 2 files changed, 43 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 9d1c457..cfa17e9 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,8 @@ Monitoring plugin to check if containers are upgradable. By default all running Checks are done by running Icinga/Nagios compatible check plugins inside containers. These plugins are listed inside the `CHECK_PLUGINS` associative array (on top of the file) and by default, the following plugin are declared: -- `/usr/lib/nagios/plugins/check_apt`: for Debian based image, provide by the `monitoring-plugins-basic` debian package -- `/usr/lib/nagios/plugins/check_apk`: for Alpine based image, see [project](https://gitea.zionetrix.net/bn8/check_apk) for install instructions +- `/usr/lib/nagios/plugins/check_apt`: for Debian based image, provide by the `monitoring-plugins-basic` debian package +- `/usr/lib/nagios/plugins/check_apk`: for Alpine based image, see [project](https://gitea.zionetrix.net/bn8/check_apk) for install instructions **Note:** The first plugin detected as installed will be used. @@ -39,6 +39,8 @@ Usage : check_container_upgrade [-d] [-E /path/to/engine] [container1,...] rebuilt on status file. --deploy-cron Start in deploy cron mode: deploy containers known as rebuilt in status file. + --check-cron Start in check cron node: check if containers need to be updated and + trigger their rebuild. -d Debug mode -l Log file -C Console logging (even if log file is specify) diff --git a/check_container_upgrade b/check_container_upgrade index 83cab82..4d4d4bc 100755 --- a/check_container_upgrade +++ b/check_container_upgrade @@ -19,6 +19,7 @@ REBUILD=0 REBUILD_DATA_DIR="/var/log/$(basename "$0")" REBUILD_CRON=0 DEPLOY_CRON=0 +CHECK_CRON=0 declare -rA CHECK_PLUGINS=( ["/usr/lib/nagios/plugins/check_apt"]="/usr/lib/nagios/plugins/check_apt -u -U -t 60 -l" ["/usr/lib/nagios/plugins/check_apk"]="/usr/lib/nagios/plugins/check_apk" @@ -161,6 +162,8 @@ Usage : $(basename "$0") [-d] [-E /path/to/engine] [container1,...] rebuilt on status file. --deploy-cron Start in deploy cron mode: deploy containers known as rebuilt in status file. + --check-cron Start in check cron node: check if containers need to be updated and + trigger their rebuild. -d Debug mode -l Log file -C Console logging (even if log file is specify) @@ -223,6 +226,9 @@ while [[ $idx -le $# ]]; do --deploy-cron) DEPLOY_CRON=1 ;; + --check-cron) + CHECK_CRON=1 + ;; *) ONLY_CONTAINERS+=( "$OPT" ) ;; @@ -448,33 +454,35 @@ debug "Upgradable containers (${#UPGRADABLE_CONTAINERS[@]}): $( implode ", " "${ debug "Containers with errors (${#ERRORS[@]}): $( implode ", " "${!ERRORS[@]}" )" debug "Not found containers (${#NOTFOUNDS[@]}): $( implode ", " "${NOTFOUNDS[@]}" )" -# Compute performance data -(( CONTAINER_COUNTS=${#CHECKED_CONTAINERS[@]}+${#NOTFOUNDS[@]} )) -PERF_DATA=( - "uptodate_containers=${#UP_TO_DATE[@]};;;0;$CONTAINER_COUNTS" - "upgradable_containers=${#UPGRADABLE_CONTAINERS[@]};;;0;$CONTAINER_COUNTS" - "containers_with_errors=${#ERRORS[@]};1;;0;$CONTAINER_COUNTS" - "unknown_state_containers=${#UNKNOWNS[@]};;;0;$CONTAINER_COUNTS" -) +if [[ $CHECK_CRON -eq 0 ]]; then + # Compute performance data + (( CONTAINER_COUNTS=${#CHECKED_CONTAINERS[@]}+${#NOTFOUNDS[@]} )) + PERF_DATA=( + "uptodate_containers=${#UP_TO_DATE[@]};;;0;$CONTAINER_COUNTS" + "upgradable_containers=${#UPGRADABLE_CONTAINERS[@]};;;0;$CONTAINER_COUNTS" + "containers_with_errors=${#ERRORS[@]};1;;0;$CONTAINER_COUNTS" + "unknown_state_containers=${#UNKNOWNS[@]};;;0;$CONTAINER_COUNTS" + ) -# Compute performance data as string -PERF_DATA_TXT="$( implode " " "${PERF_DATA[@]}" )" + # Compute performance data as string + PERF_DATA_TXT="$( implode " " "${PERF_DATA[@]}" )" -# Display check result message -case $EXIT_CODE in - 0) - message "OK - All ${#UP_TO_DATE[@]} container(s) are up-to-date |$PERF_DATA_TXT" - ;; - 1) - message "WARNING - ${#ERRORS[@]} container(s) need to be updated |$PERF_DATA_TXT" - ;; - 2) - message "CRITICAL - ${#ERRORS[@]} container(s) need to be updated |$PERF_DATA_TXT" - ;; - *) - message "UNKNOWN - fail to retrieve status of ${#UNKNOWNS[@]} container(s) |$PERF_DATA_TXT" - ;; -esac + # Display check result message + case $EXIT_CODE in + 0) + message "OK - All ${#UP_TO_DATE[@]} container(s) are up-to-date |$PERF_DATA_TXT" + ;; + 1) + message "WARNING - ${#ERRORS[@]} container(s) need to be updated |$PERF_DATA_TXT" + ;; + 2) + message "CRITICAL - ${#ERRORS[@]} container(s) need to be updated |$PERF_DATA_TXT" + ;; + *) + message "UNKNOWN - fail to retrieve status of ${#UNKNOWNS[@]} container(s) |$PERF_DATA_TXT" + ;; + esac +fi # Trigger container build (if need, enabled and docker compose file is provided) if [[ $REBUILD -eq 1 ]]; then @@ -544,7 +552,7 @@ if [[ $REBUILD -eq 1 ]]; then done # Handle rebuilt containers - if [[ ${#REBUILT_CONTAINERS[@]} -gt 0 ]]; then + if [[ ${#REBUILT_CONTAINERS[@]} -gt 0 ]] && [[ $CHECK_CRON -eq 0 ]]; then message message "Some containers are ready to be recreated and restarted." message "Run the following command to do it:" @@ -560,9 +568,11 @@ for container in "${!ERRORS[@]}"; do message "${container}" - "${ERRORS[${container}]}" done -for container in "${!UP_TO_DATE[@]}"; do - message "${container}" - "${UP_TO_DATE[${container}]}" -done +if [[ $CHECK_CRON -eq 0 ]]; then + for container in "${!UP_TO_DATE[@]}"; do + message "${container}" - "${UP_TO_DATE[${container}]}" + done +fi exit $EXIT_CODE