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

113 lines
2.3 KiB
Bash
Raw Permalink Normal View History

#!/bin/sh
# Petit script pour vérifier qu'une IP souhaitée est en tête des IP configurées
# GPL v3+ (copyright chl-dev@bugness.org)
# Initialisation
IGNORED_ADDRESS="127.0.0. ::1 fe80:"
# 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 [-x xxxxx] [-x yyyy] [-4 nnn.nnn.nnn.nnn] [-6 nnnn:nnnn:nnnn::nnnn]
WARNING: order of arguments are important.
-x : address (or start of address) which must be ignored. Often '127.0' and 'fe80'.
-4 : IPv4 address which must be first
-6 : IPv6 address which must be first
Default values:
-x "$IGNORED_ADDRESS"
EOF
}
#
# Gestion des paramètres
#
while getopts h4:6:x: f; do
case "$f" in
'h')
usage
exit
;;
'x')
IGNORED_ADDRESS="$OPTARG"
;;
'4')
IPV4="$OPTARG";
LISTING="$( ip -4 addr show | sed -n 's/^[[:space:]]*inet \([0-9.]\+\)\/[0-9].*/\1/p' )"
for i in $IGNORED_ADDRESS; do
LISTING="$( echo "$LISTING" | grep -v "^$i" || true )"
done
FIRST="$( echo "$LISTING" | head -n 1 )"
if [ "$FIRST" != "$IPV4" ]; then
OUTPUT_EXIT_STATUS=2
OUTPUT_DETAIL_CRITICAL="$OUTPUT_DETAIL_CRITICAL ('$FIRST' instead of '$IPV4')"
else
OUTPUT_DETAIL_OK="$OUTPUT_DETAIL_OK ('$FIRST')"
fi
;;
'6')
IPV6="$OPTARG";
LISTING="$( ip -6 addr show | sed -n 's/^[[:space:]]*inet6 \([a-f0-9:]\+\)\/[0-9].*/\1/p' )"
for i in $IGNORED_ADDRESS; do
LISTING="$( echo "$LISTING" | grep -v "^$i" || true )"
done
FIRST="$( echo "$LISTING" | head -n 1 )"
if [ "$FIRST" != "$IPV6" ]; then
OUTPUT_EXIT_STATUS=2
OUTPUT_DETAIL_CRITICAL="$OUTPUT_DETAIL_CRITICAL ('$FIRST' instead of '$IPV6')"
else
OUTPUT_DETAIL_OK="$OUTPUT_DETAIL_OK ('$FIRST')"
fi
;;
\?)
usage
exit 1
;;
esac
done
case "$OUTPUT_EXIT_STATUS" in
'0')
printf "OK %s" "$OUTPUT_DETAIL_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