Last active
November 10, 2015 11:47
-
-
Save momota/fa4294af95ff63d79019 to your computer and use it in GitHub Desktop.
ping.shの並行処理版。IPアドレスを改行区切りのファイルに列挙し第一引数へ、ping間隔を第二引数へ。
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
#!/bin/bash -eu | |
# ---------------------------------------------------------------------- | |
# argument validation and initialize | |
# | |
if [ $# -eq 2 ]; then | |
echo -e "*** if you want to stop this script, exec --- kill -TERM -$$ ---" | |
ip_list=$1 | |
interval=$2 | |
else | |
echo -e "USAGE:\n $ $0 <IP_LIST_FILE> <PING_INTERVAL[sec]>\n" | |
exit 1 | |
fi | |
log_dir=./log | |
[ ! -e $log_dir ] && /bin/mkdir -p $log_dir | |
# ---------------------------------------------------------------------- | |
# define functions | |
# | |
is_alive_ping() | |
{ | |
host=$1 | |
int=$2 | |
dir=$3 | |
log_file="${dir}/ping_`/bin/hostname -i`_to_${host}.log" | |
echo $log_file | |
error_count=0 | |
i=1 | |
while true; | |
do | |
/bin/ping -c 1 -i $int -w 1 $host > /dev/null 2>&1 | |
if [ $? -eq 0 ]; then | |
echo -e "$i\t\e[32mOK --- `/bin/date "+%b %d %R:%S.%N"` : Node ( $host ) is up.\e[m" >> $log_file | |
else | |
echo -e "$i\t\e[31mNG --- `/bin/date "+%b %d %R:%S.%N"` : Node ( $host ) is down.\e[m" >> $log_file | |
error_count=$((error_count + 1)) | |
fi | |
i=$((i + 1)) | |
sleep $int | |
done | |
exit 0 | |
} | |
trap_ctrl_c() | |
{ | |
echo -e "\n***** end ping *****\n" | |
exit 0 | |
} | |
# ---------------------------------------------------------------------- | |
# main | |
# | |
trap trap_ctrl_c SIGINT | |
export -f is_alive_ping | |
hosts_num=`/usr/bin/wc -l $ip_list | /usr/bin/cut -d ' ' -f 1` | |
cat $ip_list | xargs -n 1 -P $hosts_num -I % bash -c "is_alive_ping % $interval $log_dir" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment