Last active
August 26, 2016 15:47
-
-
Save jandahl/0bbdd1a40ba4308eeae9c4c8b93fe090 to your computer and use it in GitHub Desktop.
Uptime-and-downtime-logger
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 | |
scriptFile=$(basename "${0}") | |
scriptName="Uptime-and-downtime-logger" | |
scriptVersion="2016-08-26 JAGR" | |
defaultPingTimer=4 | |
targetIP=${1} | |
if [ -z "${2}" ]; then | |
pingTimer=${defaultPingTimer} | |
else | |
pingTimer=${2} | |
fi | |
function aboutMe() { | |
echo -e "\n\t${scriptName}, v ${scriptVersion}" | |
echo -e "\n\tKeeps pinging every ${Emphasize}${pingTimer}${ColorOff} seconds" | |
echo -e "\n\tTimer can be adjusted by a second argument (in seconds)." | |
echo -e "\n\tExamples:" | |
echo -e "\n\t${scriptFile}${Emphasize} 10.20.30.40${ColorOff}" | |
echo -e "\n\t${scriptFile} 10.20.30.40 ${Emphasize}30${ColorOff}" | |
} | |
function colorInit() { | |
ColorOff=$'\e[0m' # Text Reset | |
BWhite=$'\e[1;37m' # Bold White | |
BRed=$'\e[1;31m' # Bold Red | |
upEmphasis=$'\e[1;35;42m' # Bold Red | |
LGray=$'\e[0;37m' # Light Gray | |
if [ -z "$Diminish" ]; then | |
Diminish=${LGray}; | |
fi | |
if [ -z "$Emphasize" ]; then | |
Emphasize=${BRed}; | |
fi | |
} | |
function isUp() { | |
# echo "isUp invoked" | |
until ! ping -c 1 -W 1 "${targetIP}" > /dev/null | |
do | |
printf "." | |
sleep ${pingTimer}; | |
done | |
echo -e "\n${Emphasize}${targetIP}${ColorOff} went ${Emphasize}down${ColorOff} at ${Emphasize}$(date)${ColorOff}!" | |
isDown | |
} | |
function isDown() { | |
# echo "isDown invoked" | |
until ping -c 1 -W 1 "${targetIP}" > /dev/null | |
do | |
printf "." | |
sleep ${pingTimer}; | |
done | |
echo -e "\n${upEmphasis}${targetIP}${ColorOff} came ${upEmphasis}up${ColorOff} at ${upEmphasis}$(date)${ColorOff}!" | |
isUp | |
} | |
function main() { | |
echo "Invoked $(date). Pinging every ${pingTimer} seconds" | |
if ping -c 1 -W 1 "${targetIP}" > /dev/null; then | |
echo -e "\n${upEmphasis}${targetIP}${ColorOff} was ${upEmphasis}up${ColorOff} at ${upEmphasis}$(date)${ColorOff}!" | |
isUp | |
else | |
echo -e "\n${Emphasize}${targetIP}${ColorOff} was ${Emphasize}down${ColorOff} at ${Emphasize}$(date)${ColorOff}!" | |
isDown | |
fi | |
} | |
colorInit | |
if [ "$#" -lt 1 ]; then | |
aboutMe; | |
exit | |
else | |
main; | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment