I am looking to build a script where I can monitor a file of IPs with ping command when Up or Down.
I have found two excellent methods on stackoverflow and i am trying to combine them but whatever I do it does not work. I am reading man shell to learn for future as well but I think I am missing something and cannot make it work.
Script 1:
I cannot seem to find the script i found on stackoverflow but found the same under this resource: (Bash and Ping) section in: https://jmanteau.fr/posts/the-facets-of-ping/#check-if-many-hosts-are-alive
This amazing script can ping very fast multiple hosts in parallel
#!/bin/bash
argc=$#
if [ $# -lt 1 ]
then
echo "Usage: $0 <ip-list-file>"
exit 1
fi
hosts=$1
function customping
{
DATE=$(date '+%d/%m/%Y %H:%M:%S')
ping -c 1 -W 1 $1 >/dev/null 2>&1 && echo "$DATE Node $1 is UP" || echo -e "\033[1;31m $DATE Node $1 is DOWN \033[0m"
# ping -c 1 -W 1 $1 >/dev/null 2>&1 && echo "$DATE Node $1 is UP" || echo "$DATE Node $1 is DOWN"
# sleep 0.01s
}
T="$(date +%s%N)"
DEFAULT_NO_OF_PROC=8
noofproc=$DEFAULT_NO_OF_PROC
if [ -n "$2" ] #user-set no. of process instead of default
then
noofproc=$2
echo "Max processes: $noofproc"
fi
export -f customping && cat $hosts | xargs -n 1 -P $noofproc -I{} bash -c 'customping {}' \;
Script 2:
https://stackoverflow.com/a/4708831/19313640
This amazing script loops through the IPs and shows if down or up (monitoring)
function check_health {
set 192.168.10.1 192.168.10.2 192.168.10.3 192.168.10.4 192.168.10.5 192.168.10.6 192.168.10.7 192.168.10.8 192.168.10.9 192.168.10.10 192.168.10.11 192.168.10.12 192.168.10.13
trap exit 2
for ipnumber in "$@"; do
DATE=$(date '+%d/%m/%Y %H:%M:%S')
ping -c 1 -t 1 $ipnumber > /dev/null
[ $? -eq 0 ] && echo -e "|\033[1;36m $DATE \033[0m" "|\033[1;32m Node |"$ipnumber "| UP \033[0m" | column -t -s "|"
done
while true; do
i=1
for ipnumber in "$@"; do
statusname=up$i
laststatus=${!statusname:-0}
ping -c 1 -t 1 $ipnumber > /dev/null
ok=$?
eval $statusname=$ok
if [ ${!statusname} -ne $laststatus ]; then
# echo $DATE Status changed for $ipnumber
DATE=$(date '+%d/%m/%Y %H:%M:%S')
if [ $ok -eq 0 ]; then
echo -e "|\033[1;36m $DATE \033[0m" "|\033[1;32m Node |"$ipnumber "| UP \033[0m" | column -t -s "|"
else
echo -e "|\033[1;36m $DATE \033[0m" "|\033[1;31m Node |"$ipnumber "| DOWN \033[0m" | column -t -s "|"
fi
fi
i=$(($i + 1))
done
# sleep 1
done
}
So my question is how to put these 2 scripts together and make it completely in parallel as first script and by reading a file instead of "set" in second script, but also with the monitor capabilities of the second script.
Edit: If it is complicated to make this work at least how can i make the second script read a file as argument as the first script does?
I hope I was thorough and gave enough info of what i am trying to do.
Thank you.
Update:
Hello again, I have manage to made it work in a messy code.
#!/bin/bash
trap exit 2
argc=$#
if [ $# -lt 1 ]
then
echo "Usage: $0 <ip-list-file>"
exit 1
fi
hosts=$1
function check_live {
trap exit 2
DATE=$(date '+%d/%m/%Y %H:%M:%S')
ping -c 1 -t 1 $1 > /dev/null
[ $? -eq 0 ] && echo -e "|\033[1;36m $DATE \033[0m" "|\033[1;32m Node |"$1 "| UP \033[0m" | column -t -s "|"
# sleep 1
}
function check_health {
trap exit 2
# DATE=$(date '+%d/%m/%Y %H:%M:%S')
# ping -c 1 -t 1 $1 > /dev/null
# [ $? -eq 0 ] && echo -e "|\033[1;36m $DATE \033[0m" "|\033[1;32m Node |"$1 "| UP \033[0m" | column -t -s "|"
# sleep 3
while true; do
# while read line; do
# i="$i $line"
i=1
for ipnumber in "$@"; do
statusname=up$i
laststatus=${!statusname:-0}
ping -c 1 -t 1 $ipnumber > /dev/null
ok=$?
eval $statusname=$ok
if [ ${!statusname} -ne $laststatus ]; then
# echo $DATE Status changed for $ipnumber
DATE=$(date '+%d/%m/%Y %H:%M:%S')
if [ $ok -eq 0 ]; then
echo -e "|\033[1;36m $DATE \033[0m" "|\033[1;32m Node |"$ipnumber "| UP \033[0m" | column -t -s "|"
else
echo -e "|\033[1;36m $DATE \033[0m" "|\033[1;31m Node |"$ipnumber "| DOWN \033[0m" | column -t -s "|"
fi
fi
i=$(($i + 1))
done
# sleep 1
done
}
function duck_art {
textred=$(tput setaf 1)
textcyan=$(tput setaf 12)
textyellow=$(tput setaf 11)
textpurple=$(tput setaf 4)
textpink=$(tput setaf 5)
textwhite=$(tput setaf 7)
textgray=$(tput setaf 8)
textgreen=$(tput setaf 10)
echo -e ${textpink} ================================================================
cat <<EOM
${textyellow}
EOM
cat << "EOF"
__ __
___( o)> DuckLab <(o )___
\ <_. ) Monitor ( ._> /
`---' `---'
EOF
echo -e ${textpink} ================================================================
echo -e ${textyellow} " Press <CTRL+C> to exit. "
echo -e ${textpink} ================================================================
echo -e "\033[1;36m $internal_ip \033[0m" " ${textpink}| " "\033[1;36m $my_name \033[0m" " ${textpink}| " "\033[1;36m $external_ip \033[0m"
echo -e ${textpink} ================================================================
}
external_ip=$(curl -s ifconfig.me)
internal_ip=$(ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1')
my_name=$(hostname)
function multi_process_live {
T="$(date +%s%N)"
DEFAULT_NO_OF_PROC=8
noofproc=$DEFAULT_NO_OF_PROC
if [ -n "$2" ] #user-set no. of process instead of default
then
noofproc=$2
echo "Max processes: $noofproc"
fi
export -f check_live && cat $hosts | xargs -n 1 -P $noofproc -I{} bash -c 'check_live {}' \; 2>/dev/null
}
function multi_process_health {
T="$(date +%s%N)"
DEFAULT_NO_OF_PROC=8
noofproc=$DEFAULT_NO_OF_PROC
if [ -n "$2" ] #user-set no. of process instead of default
then
noofproc=$2
echo "Max processes: $noofproc"
fi
export -f check_health && cat $hosts | xargs -n 1 -P $noofproc -I{} bash -c 'check_health {}' \; 2>/dev/null
}
# ================================ End of fucntions ================================
# ================================ Start of Script =================================
clear
duck_art
multi_process_live
multi_process_health
# ================================ End of Script ===================================
First function which checks for live hosts works. Second function which loops shows initial output but doesnt loop correctly through each line of the file to monitor which hosts are UP or Down and print output. It does that only for the second line, which I assume it does not read each line of the file correctly.
Any ideas, improvements and suggestions to make this work and learn are highly valued.
Thank you.