1
0
Fork 0
scripts-admin-quickndirty-p.../nagios/check_file_age.sh

160 lines
3.8 KiB
Bash
Executable file

#!/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:"
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 :
$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] ...
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
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).
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
#
# Gestion des paramètres
#
while getopts hw:c:W:C:b:f: f; do
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
;;
'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"
# mémo : 'label'=value[UOM];[warn];[crit];[min];[max]
#OUTPUT_PERFDATA=$( printf "%s'port%d'=%d;%s;%s;0;" \
# "$( 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 )"
FILES_NUMBER_AT_CRIT_AGE="$( find "$FIND_BASEDIR" -name "$FIND_NAME_REGEXP" -type f -mtime "-$RANGE_CRITICAL_AGE" -printf "a\n" | wc -l )"
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
# (pas de perfdata dans ce script)
#printf "|%s\n" "$OUTPUT_PERFDATA"
printf "\n"
# on supprime les retours à la ligne
exit $OUTPUT_EXIT_STATUS