Skip to content

Instantly share code, notes, and snippets.

@halvener
Last active June 4, 2022 21:18
Show Gist options
  • Save halvener/ba3f660cbfdaaa71476f to your computer and use it in GitHub Desktop.
Save halvener/ba3f660cbfdaaa71476f to your computer and use it in GitHub Desktop.
Network Availability Listing Logger
#!/bin/bash
shopt -s -o nounset
#########################
# Trap exit to display end time
trap ctrl_c INT SIGINT
trap display_stats SIGUSR1
# Display stats on exit
function ctrl_c() {
echo
end_date=$(date)
log_info "===== Finished nall at ${end_date} ===== "
start_s=$(date -d "${start_date}" +%s)
end_s=$(date -d "${end_date}" +%s)
log_info "Ran for $(( (end_s - start_s) )) seconds"
display_stats
exit 0
}
#########################
# Variables
declare -a labels
declare -a ipaddresses
declare -a statuses
declare -a prevstatuses
# Color Codes
RED='\E[31;49m'
GREEN='\E[32;49m'
DEFAULT='\E[00;49m'
CONF_DIR=~/.nall
CONF_FILE=${CONF_DIR}/nall.conf
LOGGING=enabled
LOGFILE=${CONF_DIR}/nall-results.log
VERBOSITY=0
labels=( " Home DHCP " " Gateway IP " " Modem IP " " Cox IP " " Google DNS " )
ipaddresses=( "192.168.0.10" "192.168.0.1" "192.168.100.1" "68.10.8.165" "8.8.8.8" )
statuses=()
prevstatuses=()
offline_count=()
total_count=()
SLEEP_INTERVAL=1
temp_sleep=$SLEEP_INTERVAL
#########################
# Functions
# Ensure CONF_DIR exists and all arrays are initialized
init() {
if [[ ! -d "${CONF_DIR}" ]]; then
mkdir -p "${CONF_DIR}"
fi
if [[ -e "$LOGFILE" ]]; then
cat "$LOGFILE" >> "${LOGFILE}.archive"
rm -f "$LOGFILE"
fi
# Set offline/total counts to 0 to start
for ((i = 0; i < ${#ipaddresses[@]}; i++)); do
offline_count[$i]=0
total_count[$i]=0
statuses[$i]="INIT"
prevstatuses[$i]="INIT"
done
}
# Allow for log data to be displayed if required
log_info() {
echo "$*" >> $LOGFILE
[[ $VERBOSITY -gt 0 ]] && echo "$*"
}
# Display a header row of all nodes to status
display_headers() {
IFS='%'
clear
echo -e "Testing every ${temp_sleep}s: ($(date))\n"
for label in "${labels[@]}"; do
echo -e "|${label}\c"
done
echo "|"
unset IFS
}
# output packet status to log file
display_stats() {
for ((i = 0; i < ${#ipaddresses[@]}; i++)); do
local packet_loss=$(echo "scale=2; ${offline_count[$i]} / ${total_count[$i]} * 100" | bc -l)
log_info "${labels[$i]} offline for ${offline_count[$i]} / ${total_count[$i]} packets (${packet_loss}% loss)"
done
}
query_servers() {
for ((i = 0; i < ${#ipaddresses[@]}; i++)); do
prevstatuses[$i]=${statuses[$i]}
ping "${ipaddresses[$i]}" -W 1 -c 1 > /dev/null 2>&1
ping_status=$?
if [[ ${ping_status} -eq 0 ]]; then
statuses[$i]="up"
else
statuses[$i]="down"
((offline_count[i]++))
fi
((total_count[i]++))
if [[ $LOGGING == "enabled" ]]; then
if [[ "${prevstatuses[$i]}" != "${statuses[$i]}" ]] && \
[[ "${prevstatuses[$i]}" != "INIT" ]]; then
log_info "$(date): Host ${labels[$i]} changed from ${prevstatuses[$i]} to ${statuses[$i]}"
fi
fi
done
}
display_status() {
local returnstatus=0
for ((i = 0; i < ${#statuses[@]}; i++)); do
status=${statuses[$i]}
if [[ "${status}" == "up" ]]; then
echo -en "| ${GREEN}up${DEFAULT} "
elif [[ "${status}" == "down" ]]; then
echo -en "| ${RED}down${DEFAULT} "
returnstatus=1
else
echo -en "| ${RED}UNKN${DEFAULT} "
returnstatus=1
fi
done
echo -en "|\r"
return $returnstatus
}
version() {
cat <<-_EOF
#########################
# Network Availability Listing Logger
v1.01
_EOF
}
usage() {
version
cat <<-_EOF
Usage: null
-d|--dir Change nall directory
--geometry
-h|--help
-l|--list List program defaults
--logging Enable logging (default)
--no-logging Do not do logging
-s|--sleep Update sleep interval
-v|--verbose
--list-domains List of domains to monitor
-? Use defaults
_EOF
}
#########################
# Parse Arguments
while [[ $# -gt 0 ]]; do
# Parse through arguments
case "$1" in
-d|--dir) CONF_DIR="$2"
shift
;;
-h|--help) usage
exit 1
;;
--list) echo "LOGGING: $LOGGING"
echo "VERBOSITY: $VERBOSITY"
echo "SLEEP_INTERVAL: $SLEEP_INTERVAL"
exit 0
;;
--list-domains) for ((i = 0; i < ${#ipaddresses[@]}; i++)); do
echo "* ${labels[$i]}: ${ipaddresses[$i]}"
done
exit 0
;;
--logging) LOGGING=enabled
;;
--no-logging) LOGGING=disabled
;;
-s|--sleep) SLEEP_INTERVAL="$2"
shift
;;
-v|--verbose) VERBOSITY=1
;;
-vv) set -x
;;
esac
shift
done
#########################
# Main
init
# Display headers
start_date=$(date)
log_info "===== Started nall at ${start_date} ===== "
while true; do
query_servers
display_headers
display_status
# If any board is offline, only sleep for 1 second to know when it comes back online.
# Otherwise, sleep at the user defined interval
if [[ $? -eq 0 ]]; then
temp_sleep=${SLEEP_INTERVAL}
else
temp_sleep=1
fi
sleep ${temp_sleep}
done
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment