1
0
Fork 0

nagios: file-age: add min-size

+ well needed explanation...
This commit is contained in:
Chl 2021-03-06 18:15:14 +01:00
parent f27585fe99
commit b01ae66304

View file

@ -8,6 +8,7 @@ RANGE_WARNING_AGE="7"
RANGE_CRITICAL_AGE="30" RANGE_CRITICAL_AGE="30"
RANGE_WARNING_FILES_NUMBER="1:" RANGE_WARNING_FILES_NUMBER="1:"
RANGE_CRITICAL_FILES_NUMBER="1:" RANGE_CRITICAL_FILES_NUMBER="1:"
MIN_SIZE="0c"
FIND_BASEDIR="/" FIND_BASEDIR="/"
# Output # Output
@ -31,7 +32,7 @@ set -e
usage() { usage() {
cat <<EOF cat <<EOF
Usage : Usage :
$0 [-w age_warning_limit] [-c age_critical_limit] [-W files_number_warning_range] [-C files_number_critical_range] -b find-basedir -f find-name-regexp [[-w...] -f find-name-regexp] ... $0 [-w age_warning_limit] [-W files_number_warning_range] [-c age_critical_limit] [-C files_number_critical_range] [-s min_size] -b find-basedir -f find-name-regexp [[-w...] -f find-name-regexp] ...
Note: Since the file(s) are checked against the lastest ranges given, order Note: Since the file(s) are checked against the lastest ranges given, order
of the arguments are important. of the arguments are important.
@ -42,10 +43,20 @@ Default values:
files_number_warning_range : $RANGE_WARNING_FILES_NUMBER files_number_warning_range : $RANGE_WARNING_FILES_NUMBER
files_number_critical_range : $RANGE_CRITICAL_FILES_NUMBER files_number_critical_range : $RANGE_CRITICAL_FILES_NUMBER
find-basedir: $FIND_BASEDIR find-basedir: $FIND_BASEDIR
min_size: $MIN_SIZE (c/k/M/G = bytes/kibibytes/mebibytes/gibibytes. Beware of 'find' roundup, cf. man page)
The check is only done on the couple crit-crit and warn-warn. The check is only done on the couple crit-crit and warn-warn.
No combination of crit/warn is made (for example, critical number of No combination of crit/warn is made (for example, critical number of
files at age warning range). files at age warning range).
Little example :
Let's say you want to check that backup files are recent enough. For example :
- you expect between 1 and 3 files modified in the last 2 days, warning alert if not,
- you expect between 2 and 20 files modified in the last 7 days, critical alert if not.
It gives :
$0 -W 1:3 -w 2 -C 2:20 -c 7 -b /var/backups -f '*.tar.gz'
And since '-f' options are executed in sequence, you can combine several check in a single launch :
$0 -w 2 -W 1:5 -c 7 -C 5:10 -s 1500M -b /var/backups/client1/ -f 'sauv_*.tar.gz' -W 5:50 -C 25:100 -s 2M -b /var/backups/client2/ -f 'sauv_mysql_*.sql.gz'
EOF EOF
} }
@ -62,11 +73,15 @@ if ! which find >/dev/null 2>&1 ; then
echo "UNKNOWN 'find' not found." echo "UNKNOWN 'find' not found."
exit 1 exit 1
fi fi
if ! which tr >/dev/null 2>&1 ; then
echo "UNKNOWN 'tr' not found (not mandatory, remove perfdata if you wish)."
exit 1
fi
# #
# Gestion des paramètres # Gestion des paramètres
# #
while getopts hw:c:W:C:b:f: f; do while getopts hw:c:W:C:s:b:f: f; do
case "$f" in case "$f" in
'h') 'h')
usage usage
@ -99,6 +114,10 @@ while getopts hw:c:W:C:b:f: f; do
fi fi
;; ;;
's')
MIN_SIZE="$OPTARG"
;;
'b') 'b')
# TODO : vérifier que le répertoire existe ? # TODO : vérifier que le répertoire existe ?
FIND_BASEDIR="$OPTARG" FIND_BASEDIR="$OPTARG"
@ -109,16 +128,21 @@ while getopts hw:c:W:C:b:f: f; do
# un buffer et de le traiter ensuite # un buffer et de le traiter ensuite
FIND_NAME_REGEXP="$OPTARG" FIND_NAME_REGEXP="$OPTARG"
# mémo : 'label'=value[UOM];[warn];[crit];[min];[max] FILES_NUMBER_AT_WARN_AGE="$( find "$FIND_BASEDIR" -name "$FIND_NAME_REGEXP" -type f \( -size "$MIN_SIZE" -or -size "+$MIN_SIZE" \) -mtime "-$RANGE_WARNING_AGE" -printf "a\n" | wc -l )"
#OUTPUT_PERFDATA=$( printf "%s'port%d'=%d;%s;%s;0;" \ FILES_NUMBER_AT_CRIT_AGE="$( find "$FIND_BASEDIR" -name "$FIND_NAME_REGEXP" -type f \( -size "$MIN_SIZE" -or -size "+$MIN_SIZE" \) -mtime "-$RANGE_CRITICAL_AGE" -printf "a\n" | wc -l )"
# "$( test -n "$OUTPUT_PERFDATA" && echo "$OUTPUT_PERFDATA " )" \
# "$PORT_NUMBER" \
# "$CPT" \
# "$RANGE_WARNING" \
# "$RANGE_CRITICAL" )
FILES_NUMBER_AT_WARN_AGE="$( find "$FIND_BASEDIR" -name "$FIND_NAME_REGEXP" -type f -mtime "-$RANGE_WARNING_AGE" -printf "a\n" | wc -l )" # mémo : 'label'=value[UOM];[warn];[crit];[min];[max]
FILES_NUMBER_AT_CRIT_AGE="$( find "$FIND_BASEDIR" -name "$FIND_NAME_REGEXP" -type f -mtime "-$RANGE_CRITICAL_AGE" -printf "a\n" | wc -l )" OUTPUT_PERFDATA=$( printf "%s'warn%s'=%d;%s;;0;" \
"$( test -n "$OUTPUT_PERFDATA" && echo "$OUTPUT_PERFDATA " )" \
"$( echo "$FIND_BASEDIR:$FIND_NAME_REGEXP" | LANG=C tr -cd "a-zA-Z/_.-" )" \
"$FILES_NUMBER_AT_WARN_AGE" \
"$RANGE_WARNING_FILES_NUMBER" )
#"$RANGE_CRITICAL_FILES_NUMBER" )
OUTPUT_PERFDATA=$( printf "%s'crit%s'=%d;;%s;0;" \
"$( test -n "$OUTPUT_PERFDATA" && echo "$OUTPUT_PERFDATA " )" \
"$( echo "$FIND_BASEDIR:$FIND_NAME_REGEXP" | LANG=C tr -cd "a-zA-Z/_.-" )" \
"$FILES_NUMBER_AT_CRIT_AGE" \
"$RANGE_CRITICAL_FILES_NUMBER" ) #"$RANGE_WARNING_FILES_NUMBER" \
if check_range "$FILES_NUMBER_AT_CRIT_AGE" "$RANGE_CRITICAL_FILES_NUMBER" ; then if check_range "$FILES_NUMBER_AT_CRIT_AGE" "$RANGE_CRITICAL_FILES_NUMBER" ; then
OUTPUT_EXIT_STATUS=2 OUTPUT_EXIT_STATUS=2
@ -153,8 +177,6 @@ case "$OUTPUT_EXIT_STATUS" in
;; ;;
esac esac
# (pas de perfdata dans ce script) printf "|%s\n" "$OUTPUT_PERFDATA"
#printf "|%s\n" "$OUTPUT_PERFDATA"
printf "\n"
# on supprime les retours à la ligne
exit $OUTPUT_EXIT_STATUS exit $OUTPUT_EXIT_STATUS