Last active
August 1, 2017 20:44
-
-
Save neurogenesis/e8beab50a24a9a43730e61fe514e5a3d to your computer and use it in GitHub Desktop.
colorized pings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# TODO: | |
# - allow setting custom green/yellow threshold via CLI options | |
# - allow profile selection via CLI options | |
# - fix issue with command argument position (must follow hostname) | |
# | |
## set defauts | |
DATEFORMAT="%Y-%m-%d %H:%M:%S" | |
PING_TIME_OK=40 | |
PING_TIME_WARN=70 | |
## check for commandline options | |
while [[ $# -gt 1 ]] ; do | |
key="$1" | |
case $key in | |
-p|--profile) | |
PROFILE="$2" | |
shift # past argument | |
;; | |
*) | |
PING_HOST=$key # assume any other argument is the hostname | |
;; | |
esac | |
shift | |
done | |
# use google DNS if no other host is given | |
if [[ "$PING_HOST" = "" ]]; then | |
echo "host: '$PING_HOST'" | |
PING_HOST="8.8.8.8" | |
fi | |
case $PROFILE in | |
voip) | |
PING_TIME_OK=40 | |
PING_TIME_WARN=70 | |
;; | |
internet) | |
PING_TIME_OK=80 | |
PING_TIME_WARN=120 | |
;; | |
esac | |
function color_ping { | |
TARGET=$1 | |
echo "host: $TARGET, ok: <= $PING_TIME_OK, warn: <= $PING_TIME_WARN" | |
ping $TARGET \ | |
| awk -v host=$TARGET '{now="date -j ${DATEFORMAT}"; now|getline date; close(now); print date " - " host " - " $0}' \ | |
| awk -v ping_ok=$PING_TIME_OK -v ping_warn=$PING_TIME_WARN -F'[ =]' \ | |
' | |
function green(str) {printf("%s%s%s", "\033[1;32m", str, "\033[0m")} | |
function yellow(str) {printf("%s%s%s", "\033[1;33m", str, "\033[0m")} | |
function red(str) {printf("%s%s%s", "\033[1;31m", str, "\033[0m")} | |
function timeout(str) {printf("%s%s%s", "\033[1;41m", str, "\033[0m")} | |
{ | |
ping_time = $20; | |
if ($0 ~ /PING/) | |
print $0; | |
else if ($0 ~ /timeout/) | |
print timeout($0); | |
else if (ping_time <= ping_ok) | |
print green($0); | |
else if (ping_time <= ping_warn) | |
print yellow($0); | |
else if (ping_time > ping_warn) | |
print red($0); | |
else | |
print $0; | |
} | |
' | |
} | |
color_ping $PING_HOST; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example uses...
-p|--profile
= use a predetermined profile for ping reply thresholds