From ebfd7fbc3a8c22d5ba092b239e34f790847ef745 Mon Sep 17 00:00:00 2001 From: Salt Date: Fri, 15 Oct 2021 19:17:13 -0500 Subject: [PATCH] Add check_ping_by_hostname --- check_ping_by_hostname | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100755 check_ping_by_hostname diff --git a/check_ping_by_hostname b/check_ping_by_hostname new file mode 100755 index 0000000..ae35854 --- /dev/null +++ b/check_ping_by_hostname @@ -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