Add check cron mode

This commit is contained in:
Benjamin Renard 2024-07-14 19:47:16 +02:00
parent fcecf75613
commit a4639f158d
2 changed files with 43 additions and 31 deletions

View file

@ -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: 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_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_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. **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. rebuilt on status file.
--deploy-cron Start in deploy cron mode: deploy containers known as rebuilt in status --deploy-cron Start in deploy cron mode: deploy containers known as rebuilt in status
file. file.
--check-cron Start in check cron node: check if containers need to be updated and
trigger their rebuild.
-d Debug mode -d Debug mode
-l Log file -l Log file
-C Console logging (even if log file is specify) -C Console logging (even if log file is specify)

View file

@ -19,6 +19,7 @@ REBUILD=0
REBUILD_DATA_DIR="/var/log/$(basename "$0")" REBUILD_DATA_DIR="/var/log/$(basename "$0")"
REBUILD_CRON=0 REBUILD_CRON=0
DEPLOY_CRON=0 DEPLOY_CRON=0
CHECK_CRON=0
declare -rA CHECK_PLUGINS=( 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_apt"]="/usr/lib/nagios/plugins/check_apt -u -U -t 60 -l"
["/usr/lib/nagios/plugins/check_apk"]="/usr/lib/nagios/plugins/check_apk" ["/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. rebuilt on status file.
--deploy-cron Start in deploy cron mode: deploy containers known as rebuilt in status --deploy-cron Start in deploy cron mode: deploy containers known as rebuilt in status
file. file.
--check-cron Start in check cron node: check if containers need to be updated and
trigger their rebuild.
-d Debug mode -d Debug mode
-l Log file -l Log file
-C Console logging (even if log file is specify) -C Console logging (even if log file is specify)
@ -223,6 +226,9 @@ while [[ $idx -le $# ]]; do
--deploy-cron) --deploy-cron)
DEPLOY_CRON=1 DEPLOY_CRON=1
;; ;;
--check-cron)
CHECK_CRON=1
;;
*) *)
ONLY_CONTAINERS+=( "$OPT" ) ONLY_CONTAINERS+=( "$OPT" )
;; ;;
@ -448,33 +454,35 @@ debug "Upgradable containers (${#UPGRADABLE_CONTAINERS[@]}): $( implode ", " "${
debug "Containers with errors (${#ERRORS[@]}): $( implode ", " "${!ERRORS[@]}" )" debug "Containers with errors (${#ERRORS[@]}): $( implode ", " "${!ERRORS[@]}" )"
debug "Not found containers (${#NOTFOUNDS[@]}): $( implode ", " "${NOTFOUNDS[@]}" )" debug "Not found containers (${#NOTFOUNDS[@]}): $( implode ", " "${NOTFOUNDS[@]}" )"
# Compute performance data if [[ $CHECK_CRON -eq 0 ]]; then
(( CONTAINER_COUNTS=${#CHECKED_CONTAINERS[@]}+${#NOTFOUNDS[@]} )) # Compute performance data
PERF_DATA=( (( CONTAINER_COUNTS=${#CHECKED_CONTAINERS[@]}+${#NOTFOUNDS[@]} ))
"uptodate_containers=${#UP_TO_DATE[@]};;;0;$CONTAINER_COUNTS" PERF_DATA=(
"upgradable_containers=${#UPGRADABLE_CONTAINERS[@]};;;0;$CONTAINER_COUNTS" "uptodate_containers=${#UP_TO_DATE[@]};;;0;$CONTAINER_COUNTS"
"containers_with_errors=${#ERRORS[@]};1;;0;$CONTAINER_COUNTS" "upgradable_containers=${#UPGRADABLE_CONTAINERS[@]};;;0;$CONTAINER_COUNTS"
"unknown_state_containers=${#UNKNOWNS[@]};;;0;$CONTAINER_COUNTS" "containers_with_errors=${#ERRORS[@]};1;;0;$CONTAINER_COUNTS"
) "unknown_state_containers=${#UNKNOWNS[@]};;;0;$CONTAINER_COUNTS"
)
# Compute performance data as string # Compute performance data as string
PERF_DATA_TXT="$( implode " " "${PERF_DATA[@]}" )" PERF_DATA_TXT="$( implode " " "${PERF_DATA[@]}" )"
# Display check result message # Display check result message
case $EXIT_CODE in case $EXIT_CODE in
0) 0)
message "OK - All ${#UP_TO_DATE[@]} container(s) are up-to-date |$PERF_DATA_TXT" message "OK - All ${#UP_TO_DATE[@]} container(s) are up-to-date |$PERF_DATA_TXT"
;; ;;
1) 1)
message "WARNING - ${#ERRORS[@]} container(s) need to be updated |$PERF_DATA_TXT" message "WARNING - ${#ERRORS[@]} container(s) need to be updated |$PERF_DATA_TXT"
;; ;;
2) 2)
message "CRITICAL - ${#ERRORS[@]} container(s) need to be updated |$PERF_DATA_TXT" 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" message "UNKNOWN - fail to retrieve status of ${#UNKNOWNS[@]} container(s) |$PERF_DATA_TXT"
;; ;;
esac esac
fi
# Trigger container build (if need, enabled and docker compose file is provided) # Trigger container build (if need, enabled and docker compose file is provided)
if [[ $REBUILD -eq 1 ]]; then if [[ $REBUILD -eq 1 ]]; then
@ -544,7 +552,7 @@ if [[ $REBUILD -eq 1 ]]; then
done done
# Handle rebuilt containers # Handle rebuilt containers
if [[ ${#REBUILT_CONTAINERS[@]} -gt 0 ]]; then if [[ ${#REBUILT_CONTAINERS[@]} -gt 0 ]] && [[ $CHECK_CRON -eq 0 ]]; then
message message
message "Some containers are ready to be recreated and restarted." message "Some containers are ready to be recreated and restarted."
message "Run the following command to do it:" message "Run the following command to do it:"
@ -560,9 +568,11 @@ for container in "${!ERRORS[@]}"; do
message "${container}" - "${ERRORS[${container}]}" message "${container}" - "${ERRORS[${container}]}"
done done
for container in "${!UP_TO_DATE[@]}"; do if [[ $CHECK_CRON -eq 0 ]]; then
message "${container}" - "${UP_TO_DATE[${container}]}" for container in "${!UP_TO_DATE[@]}"; do
done message "${container}" - "${UP_TO_DATE[${container}]}"
done
fi
exit $EXIT_CODE exit $EXIT_CODE