2019-07-23 22:28:09 +02:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
# Petit script custom pour vérifier l'âge de certains fichiers
|
|
|
|
# GPL v3+
|
|
|
|
|
|
|
|
# Default values
|
|
|
|
RANGE_WARNING_AGE="7"
|
|
|
|
RANGE_CRITICAL_AGE="30"
|
|
|
|
RANGE_WARNING_FILES_NUMBER="1:"
|
|
|
|
RANGE_CRITICAL_FILES_NUMBER="1:"
|
2021-03-06 18:15:14 +01:00
|
|
|
MIN_SIZE="0c"
|
2019-07-23 22:28:09 +02:00
|
|
|
FIND_BASEDIR="/"
|
|
|
|
|
|
|
|
# Output
|
|
|
|
OUTPUT_EXIT_STATUS=0
|
|
|
|
OUTPUT_DETAIL_WARNING=""
|
|
|
|
OUTPUT_DETAIL_CRITICAL=""
|
|
|
|
OUTPUT_PERFDATA=""
|
|
|
|
|
|
|
|
PROGPATH=$( echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,' )
|
|
|
|
REVISION="0.1"
|
|
|
|
|
|
|
|
# Stop at the first non-catched error
|
|
|
|
set -e
|
|
|
|
|
|
|
|
# Include check_range()
|
|
|
|
. $PROGPATH/utils.sh
|
|
|
|
|
|
|
|
#
|
|
|
|
# Fonction d'aide
|
|
|
|
#
|
|
|
|
usage() {
|
|
|
|
cat <<EOF
|
|
|
|
Usage :
|
2021-03-06 18:15:14 +01:00
|
|
|
$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] ...
|
2019-07-23 22:28:09 +02:00
|
|
|
|
|
|
|
Note: Since the file(s) are checked against the lastest ranges given, order
|
|
|
|
of the arguments are important.
|
|
|
|
|
|
|
|
Default values:
|
|
|
|
age_warning_range (in days): $RANGE_WARNING_AGE
|
|
|
|
age_critical_range (in days): $RANGE_CRITICAL_AGE
|
|
|
|
files_number_warning_range : $RANGE_WARNING_FILES_NUMBER
|
|
|
|
files_number_critical_range : $RANGE_CRITICAL_FILES_NUMBER
|
|
|
|
find-basedir: $FIND_BASEDIR
|
2021-03-06 18:15:14 +01:00
|
|
|
min_size: $MIN_SIZE (c/k/M/G = bytes/kibibytes/mebibytes/gibibytes. Beware of 'find' roundup, cf. man page)
|
2019-07-23 22:28:09 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
files at age warning range).
|
2021-03-06 18:15:14 +01:00
|
|
|
|
|
|
|
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'
|
2019-07-23 22:28:09 +02:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
check_range_syntax() {
|
|
|
|
check_range 0 "$1" >/dev/null 2>&1
|
|
|
|
if [ "$?" -eq "2" ]; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
# Some early checks
|
|
|
|
if ! which find >/dev/null 2>&1 ; then
|
|
|
|
echo "UNKNOWN 'find' not found."
|
|
|
|
exit 1
|
|
|
|
fi
|
2021-03-06 18:15:14 +01:00
|
|
|
if ! which tr >/dev/null 2>&1 ; then
|
|
|
|
echo "UNKNOWN 'tr' not found (not mandatory, remove perfdata if you wish)."
|
|
|
|
exit 1
|
|
|
|
fi
|
2019-07-23 22:28:09 +02:00
|
|
|
|
|
|
|
#
|
|
|
|
# Gestion des paramètres
|
|
|
|
#
|
2021-03-06 18:15:14 +01:00
|
|
|
while getopts hw:c:W:C:s:b:f: f; do
|
2019-07-23 22:28:09 +02:00
|
|
|
case "$f" in
|
|
|
|
'h')
|
|
|
|
usage
|
|
|
|
exit
|
|
|
|
;;
|
|
|
|
|
|
|
|
'w')
|
|
|
|
RANGE_WARNING_AGE="$( printf "%d" "$OPTARG" )"
|
|
|
|
;;
|
|
|
|
|
|
|
|
'c')
|
|
|
|
RANGE_CRITICAL_AGE="$( printf "%d" "$OPTARG" )"
|
|
|
|
;;
|
|
|
|
|
|
|
|
'W')
|
|
|
|
if check_range_syntax "$OPTARG" >/dev/null; then
|
|
|
|
RANGE_WARNING_FILES_NUMBER="$OPTARG"
|
|
|
|
else
|
|
|
|
echo "UNKNOWN: invalid range."
|
|
|
|
exit 3
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
|
|
|
|
'C')
|
|
|
|
if check_range_syntax "$OPTARG" >/dev/null; then
|
|
|
|
RANGE_CRITICAL_FILES_NUMBER="$OPTARG"
|
|
|
|
else
|
|
|
|
echo "UNKNOWN: invalid range."
|
|
|
|
exit 3
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
|
2021-03-06 18:15:14 +01:00
|
|
|
's')
|
|
|
|
MIN_SIZE="$OPTARG"
|
|
|
|
;;
|
|
|
|
|
2019-07-23 22:28:09 +02:00
|
|
|
'b')
|
|
|
|
# TODO : vérifier que le répertoire existe ?
|
|
|
|
FIND_BASEDIR="$OPTARG"
|
|
|
|
;;
|
|
|
|
|
|
|
|
'f')
|
|
|
|
# Ce n'est pas très propre, mais on gère tout ici plutôt que de remplir
|
|
|
|
# un buffer et de le traiter ensuite
|
|
|
|
FIND_NAME_REGEXP="$OPTARG"
|
|
|
|
|
2022-04-30 13:40:06 +02:00
|
|
|
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" 2>/dev/null | wc -l )"
|
|
|
|
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" 2>/dev/null | wc -l )"
|
2019-07-23 22:28:09 +02:00
|
|
|
|
2021-03-06 18:15:14 +01:00
|
|
|
# mémo : 'label'=value[UOM];[warn];[crit];[min];[max]
|
|
|
|
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" \
|
2019-07-23 22:28:09 +02:00
|
|
|
|
|
|
|
if check_range "$FILES_NUMBER_AT_CRIT_AGE" "$RANGE_CRITICAL_FILES_NUMBER" ; then
|
|
|
|
OUTPUT_EXIT_STATUS=2
|
|
|
|
OUTPUT_DETAIL_CRITICAL="$OUTPUT_DETAIL_CRITICAL basedir:$FIND_BASEDIR($FILES_NUMBER_AT_CRIT_AGE files at less than $RANGE_CRITICAL_AGE days)"
|
|
|
|
elif check_range "$FILES_NUMBER_AT_WARN_AGE" "$RANGE_WARNING_FILES_NUMBER"; then
|
|
|
|
if [ "$OUTPUT_EXIT_STATUS" -eq 0 ]; then
|
|
|
|
OUTPUT_EXIT_STATUS=1
|
|
|
|
fi
|
|
|
|
OUTPUT_DETAIL_WARNING="$OUTPUT_DETAIL_WARNING basedir:$FIND_BASEDIR($FILES_NUMBER_AT_WARN_AGE files at less than $RANGE_WARNING_AGE days)"
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
|
|
|
|
\?)
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
case "$OUTPUT_EXIT_STATUS" in
|
|
|
|
'0')
|
|
|
|
printf "OK"
|
|
|
|
;;
|
|
|
|
'1')
|
|
|
|
printf "WARNING %s" "$OUTPUT_DETAIL_WARNING"
|
|
|
|
;;
|
|
|
|
'2')
|
|
|
|
printf "CRITICAL %s" "$OUTPUT_DETAIL_CRITICAL"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
printf "UNKNOWN"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2021-03-06 18:15:14 +01:00
|
|
|
printf "|%s\n" "$OUTPUT_PERFDATA"
|
|
|
|
|
2019-07-23 22:28:09 +02:00
|
|
|
exit $OUTPUT_EXIT_STATUS
|