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

152 lines
3.5 KiB
Bash
Executable file

#!/bin/sh
# Petit script custom pour vérifier le nombre de checks
# dans chaque catégorie
# GPL v3+
# Stop at the first non-catched error
set -e
# Include check_range() and STATE_* vars
. "$( dirname "$0" )/utils.sh"
# Default values
RANGE_WARNING="1:50"
RANGE_CRITICAL="1:100"
# Output
OUTPUT_EXIT_STATUS=$STATE_OK
OUTPUT_DETAIL_WARNING=""
OUTPUT_DETAIL_CRITICAL=""
OUTPUT_PERFDATA=""
# Some preconf.
# TODO: check to make it available as an command line option, if
# not security-ill
SHINKEN_ARBITER_PATH="/usr/local/bin/shinken-arbiter"
SHINKEN_ARBITER_OPTIONS="-v -c /etc/shinken/nagios.cfg -c /etc/shinken/shinken-specific.cfg"
#
# Fonction d'aide
#
usage() {
cat <<EOF
Usage :
$0 [-w warning_range] [-c critical_range] -t category/type [[-w...] -t type] ...
Note: Since the category/type is checked against the lastest ranges given, order
of the arguments are important.
Default values:
warning_range: $RANGE_WARNING
critical_range: $RANGE_CRITICAL
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 $SHINKEN_ARBITER_PATH >/dev/null 2>&1 ; then
echo "UNKNOWN 'shinken-arbiter' not found in path : $PATH"
exit $STATE_UNKNOWN
fi
# Get the values
LISTING="$( $SHINKEN_ARBITER_PATH $SHINKEN_ARBITER_OPTIONS | sed -n 's/^\tChecked \([0-9]\+\) \(.\+\)$/\2\t\1/p' | sort )"
OUTPUT_PERFDATA_REMNANT="$( printf "%s" "$LISTING" | sed 's/\t/=/g' | tr '\n' ' ' )"
#
# Gestion des paramètres
#
while getopts hw:c:t: f; do
case "$f" in
'h')
usage
exit
;;
'w')
if check_range_syntax "$OPTARG" >/dev/null; then
RANGE_WARNING="$OPTARG"
else
echo "UNKNOWN: invalid range."
exit 3
fi
;;
'c')
if check_range_syntax "$OPTARG" >/dev/null; then
RANGE_CRITICAL="$OPTARG"
else
echo "UNKNOWN: invalid range."
exit 3
fi
;;
't')
# 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
CATEGORY="$OPTARG"
CPT=$( printf "%s\n" "$LISTING" | grep -- "^$CATEGORY[[:space:]]" | cut -f 2 )
if [ -z "$CPT" ]; then
printf "UNKNOWN: unknown category $CATEGORY"
exit $STATE_UNKNOWN
fi
# mémo : 'label'=value[UOM];[warn];[crit];[min];[max]
OUTPUT_PERFDATA=$( printf "%s'%s'=%d;%s;%s;0;" \
"$( test -n "$OUTPUT_PERFDATA" && echo "$OUTPUT_PERFDATA " )" \
"$CATEGORY" \
"$CPT" \
"$RANGE_WARNING" \
"$RANGE_CRITICAL" )
# We remove the item from the "remnants"
OUTPUT_PERFDATA_REMNANT="$( printf "%s" "$OUTPUT_PERFDATA_REMNANT" | sed "s/[[:space:]]*$CATEGORY=[0-9]\+[[:space:]]*/ /" )"
# Check value against critical & warning range
if check_range "$CPT" "$RANGE_CRITICAL"; then
OUTPUT_EXIT_STATUS=$STATE_CRITICAL
OUTPUT_DETAIL_CRITICAL="$OUTPUT_DETAIL_CRITICAL Cat:$CATEGORY($CPT)"
elif check_range "$CPT" "$RANGE_WARNING"; then
if [ "$OUTPUT_EXIT_STATUS" -eq 0 ]; then
OUTPUT_EXIT_STATUS=$STATE_WARNING
fi
OUTPUT_DETAIL_WARNING="$OUTPUT_DETAIL_WARNING Cat:$CATEGORY($CPT)"
fi
;;
\?)
usage
exit 1
;;
esac
done
OUTPUT_PERFDATA="$OUTPUT_PERFDATA $OUTPUT_PERFDATA_REMNANT"
case "$OUTPUT_EXIT_STATUS" in
"$STATE_OK")
printf "OK"
;;
"$STATE_WARNING")
printf "WARNING %s" "$OUTPUT_DETAIL_WARNING"
;;
"$STATE_CRITICAL")
printf "CRITICAL %s" "$OUTPUT_DETAIL_CRITICAL"
;;
*)
printf "UNKNOWN"
;;
esac
printf "|%s\n" "$OUTPUT_PERFDATA"
# on supprime les retours à la ligne
exit $OUTPUT_EXIT_STATUS