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:
- `/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)

View file

@ -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