Add check_ping_by_hostname

This commit is contained in:
Salt 2021-10-15 19:17:13 -05:00
parent 85de49d8ec
commit ebfd7fbc3a
1 changed files with 38 additions and 0 deletions

38
check_ping_by_hostname Executable file
View File

@ -0,0 +1,38 @@
#!/bin/bash
#
# check_ping_by_hostname
#
# Attempts to resolve the name for and ping ourselves based on the device's
# hostname.
#
name="$HOSTNAME"
# Ensure $name is available
if [ -z "$name" ]; then
echo "CRITICAL: No hostname(?!)"
exit 2
fi
# Ensure we have the tools we need
for cmd in dig ping; do
if ! command -v "$cmd" > /dev/null 2>&1; then
echo "UNKNOWN: Missing command for check: $cmd"
exit 3
fi
done
# Ensure we can dig ourselves
if [ -z "$(dig +short "$name")" ]; then
echo "CRITICAL: Could not resolve hostname or found empty record: $name"
exit 2
fi
# Ping ourselves
if ! ping "$name" -c 1 > /dev/null 2>&1; then
echo "CRITICAL: Could not ping self"
exit 2
else
echo "OK"
exit 0
fi