Compare commits

...

3 commits

Author SHA1 Message Date
Benjamin Renard 675bb089d8
Introduce pre-commit hooks 2024-04-18 11:16:47 +02:00
Benjamin Renard 28d084da36
Add performance data 2024-04-18 11:14:08 +02:00
Benjamin Renard d0b3649213
Add containers counts in output message 2024-04-18 11:12:40 +02:00
3 changed files with 65 additions and 11 deletions

25
.pre-commit-config.yaml Normal file
View file

@ -0,0 +1,25 @@
# Pre-commit hooks to run tests and ensure code is cleaned.
# See https://pre-commit.com for more information
---
repos:
- repo: https://github.com/codespell-project/codespell
rev: v2.2.2
hooks:
- id: codespell
args:
- --ignore-words-list=exten
- --skip="./.*,*.csv,*.json,*.ini,*.subject,*.txt,*.html,*.log,*.conf"
- --quiet-level=2
- --ignore-regex=.*codespell-ignore$
#- --write-changes # Uncomment to write changes
exclude_types: [csv, json]
- repo: https://github.com/adrienverge/yamllint
rev: v1.32.0
hooks:
- id: yamllint
ignore: .github/
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v2.7.1
hooks:
- id: prettier
args: ["--print-width", "100"]

View file

@ -7,7 +7,7 @@ Checks are done by running Icinga/Nagios compatible check plugins inside contain
- `/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.
**Note:** The first plugin detected as installed will be used.
## Installation

View file

@ -40,6 +40,13 @@ function in_array() {
return 1
}
function implode() {
local d=${1-} f=${2-}
if shift 2; then
printf %s "$f" "${@/#/$d}"
fi
}
function usage() {
error="$1"
[ -n "$error" ] && echo "$error"
@ -132,8 +139,9 @@ fi
EXIT_CODE=0
declare -A CONTAINER_STATUS_FILE
declare -A CONTAINER_PID
declare -A UPTODATE
declare -A UP_TO_DATE
declare -A ERRORS
declare -A UNKNOWNS
CHECKED_CONTAINERS=( )
debug "List running containers..."
@ -224,23 +232,26 @@ do
rm -f ${CONTAINER_STATUS_FILE[$container]}
if [ $ex -eq 0 ]
then
UPTODATE+=( ["$container"]=$STATUS )
UP_TO_DATE+=( ["$container"]=$STATUS )
else
ERRORS+=( ["$container"]=$STATUS )
[ $ex -ge 3 ] && UNKNOWNS+=( "$container" )
fi
[ $EXIT_CODE -ge $ex ] && continue
[ $ex -gt 3 ] && ex=3
EXIT_CODE=$ex
done
NOTFOUNDS=()
if ! is_empty $ONLY_CONTAINERS
then
for container in ${ONLY_CONTAINERS[@]}
do
if ! in_array $container ${CHECKED_CONTAINERS[@]}
then
debug "$container - Not found"
ERRORS+=( ["$container"]="Not found" )
debug "$container - Container not found"
ERRORS+=( ["$container"]="Container not found" )
NOTFOUNDS+=( "$container" )
EXIT_CODE=3
fi
done
@ -248,28 +259,46 @@ fi
debug "Final exit code: $EXIT_CODE"
debug "Check containers (${#CHECKED_CONTAINERS[@]}): $( implode ", " "${CHECKED_CONTAINERS[@]}" )"
debug "Containers with errors (${#ERRORS[@]}): $( implode ", " "${!ERRORS[@]}" )"
debug "Not found containers (${#NOTFOUNDS[@]}): $( implode ", " "${NOTFOUNDS[@]}" )"
# Compute performance data
let CONTAINER_COUNTS=${#CHECKED_CONTAINERS[@]}+${#NOTFOUNDS[@]}
PERF_DATA=(
"uptodate_containers=${#UP_TO_DATE[@]};;;0;$CONTAINER_COUNTS"
"containers_with_errors=${#ERRORS[@]};1;;0;$CONTAINER_COUNTS"
"unknown_state_containers=${#UNKNOWNS[@]};;;0;$CONTAINER_COUNTS"
)
# Display check result message
case $EXIT_CODE in
0)
echo "OK - All containers are uptodate"
echo -n "OK - All ${#UP_TO_DATE[@]} container(s) are up-to-date"
;;
1)
echo "WARNING - some containers need to be updated"
echo -n "WARNING - ${#ERRORS[@]} container(s) need to be updated"
;;
2)
echo "CRITICAL - some containers need to be updated"
echo -n "CRITICAL - ${#ERRORS[@]} container(s) need to be updated"
;;
*)
echo "UNKNOWN - fail to retrieve status of some containers"
echo -n "UNKNOWN - fail to retrieve status of ${#UNKNOWNS[@]} container(s)"
;;
esac
# Add performance data
echo " |$( implode " " "${PERF_DATA[@]}" )"
# Display details, starting by errors
for container in ${!ERRORS[@]}
do
echo ${container} - ${ERRORS[${container}]}
done
for container in ${!UPTODATE[@]}
for container in ${!UP_TO_DATE[@]}
do
echo ${container} - ${UPTODATE[${container}]}
echo ${container} - ${UP_TO_DATE[${container}]}
done
exit $EXIT_CODE