182 lines
5.1 KiB
Bash
Executable file
182 lines
5.1 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:"
|
|
MIN_SIZE="0c"
|
|
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] [-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
|
|
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
|
|
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.
|
|
No combination of crit/warn is made (for example, critical number of
|
|
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
|
|
}
|
|
|
|
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
|
|
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
|
|
#
|
|
while getopts hw:c:W:C:s: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
|
|
;;
|
|
|
|
's')
|
|
MIN_SIZE="$OPTARG"
|
|
;;
|
|
|
|
'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"
|
|
|
|
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 )"
|
|
|
|
# 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" \
|
|
|
|
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
|
|
|
|
printf "|%s\n" "$OUTPUT_PERFDATA"
|
|
|
|
exit $OUTPUT_EXIT_STATUS
|