스크립트에서 서버의 생존 여부를 체크하기 위해 간편하게 ping 명령어를 통해
OS 정보 및 online 상태를 확인한다.
근데 shutdown 상태/방화벽 차단/사용하지 않는 IP 등으로 ping을 치면 응답이 지연되어
그만큼 스크립트 결과도 늦게 나오게 된다.
예를 들어, 다음과 같이 for 문만 활용하여 통신을 확인할 시에는
각 IP 마다 최소 1초가 걸리고, 순차적으로 처리하게 되어 수 많은 대역을 체크하게되면
스크립트 구동시간이 오래 걸리게 된다.
[root@abc ~/job/script]# for i in `echo {1..200}`; do ping -c1 -W1 192.168.230.$i; done PING 192.168.230.1 (192.168.230.1) 56(84) bytes of data. 64 bytes from 192.168.230.1: icmp_seq=1 ttl=255 time=0.148 ms
--- 192.168.230.1 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.148/0.148/0.148/0.000 ms PING 192.168.230.2 (192.168.230.2) 56(84) bytes of data. 64 bytes from 192.168.230.2: icmp_seq=1 ttl=255 time=0.103 ms
--- 192.168.230.2 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.103/0.103/0.103/0.000 ms PING 192.168.230.3 (192.168.230.3) 56(84) bytes of data. 64 bytes from 192.168.230.3: icmp_seq=1 ttl=255 time=0.109 ms
--- 192.168.230.3 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.109/0.109/0.109/0.000 ms PING 192.168.230.4 (192.168.230.4) 56(84) bytes of data. |
이를 해결하기 위해 xargs 명령어와 같이 사용하면 병렬로 처리가 가능하다
echo $(seq 1 255) | xargs -P255 -I% -d" " ping -W 1 -c 1 192.168.230.%
실제 구동중인 프로세스 리스트를 확인하면 다음과 같이 차이점을 볼 수 있다.
for i in `echo {1..200}`; do ping -c1 -W1 192.168.230.$i; done
root 104536 0.0 0.0 108920 2284 pts/7 S 09:52 0:01 | | \_ -bash root 6307 0.0 0.0 103256 748 pts/7 T 11:06 0:00 | | \_ ping -c1 -W1 192.168.230.8 root 17987 0.0 0.0 103256 748 pts/7 S+ 11:10 0:00 | | \_ ping -c1 -W1 192.168.230.45 |
echo $(seq 1 255) | xargs -P255 -I% -d" " ping -W 1 -c 1 192.168.230.%
root 104535 0.0 0.0 165736 1820 pts/7 S 09:52 0:00 | | \_ su - root 104536 0.0 0.0 108920 2292 pts/7 S 09:52 0:01 | | \_ -bash root 6307 0.0 0.0 103256 748 pts/7 T 11:06 0:00 | | \_ ping -c1 -W1 192.168.230.8 root 19498 0.0 0.0 101360 692 pts/7 S+ 11:10 0:00 | | \_ xargs -P255 -I% -d ping -W 1 -c 1 192.168.230.% root 19503 0.0 0.0 103256 744 pts/7 S+ 11:10 0:00 | | \_ ping -W 1 -c 1 192.168.230.4 root 19504 0.0 0.0 103256 748 pts/7 S+ 11:10 0:00 | | \_ ping -W 1 -c 1 192.168.230.5 root 19505 0.0 0.0 103256 744 pts/7 S+ 11:10 0:00 | | \_ ping -W 1 -c 1 192.168.230.6 root 19506 0.0 0.0 103256 748 pts/7 S+ 11:10 0:00 | | \_ ping -W 1 -c 1 192.168.230.7 root 19507 0.0 0.0 103256 748 pts/7 S+ 11:10 0:00 | | \_ ping -W 1 -c 1 192.168.230.8 root 19508 0.0 0.0 103256 748 pts/7 S+ 11:10 0:00 | | \_ ping -W 1 -c 1 192.168.230.9 root 19509 0.0 0.0 103256 748 pts/7 S+ 11:10 0:00 | | \_ ping -W 1 -c 1 192.168.230.10 root 19518 0.0 0.0 103256 748 pts/7 S+ 11:10 0:00 | | \_ ping -W 1 -c 1 192.168.230.19 root 19519 0.0 0.0 103256 744 pts/7 S+ 11:10 0:00 | | \_ ping -W 1 -c 1 192.168.230.20 root 19544 0.0 0.0 103256 748 pts/7 S+ 11:10 0:00 | | \_ ping -W 1 -c 1 192.168.230.45 root 19545 0.0 0.0 103256 748 pts/7 S+ 11:10 0:00 | | \_ ping -W 1 -c 1 192.168.230.46 root 19546 0.0 0.0 103256 748 pts/7 S+ 11:10 0:00 | | \_ ping -W 1 -c 1 192.168.230.47 root 19547 0.0 0.0 103256 744 pts/7 S+ 11:10 0:00 | | \_ ping -W 1 -c 1 192.168.230.48 root 19548 0.0 0.0 103256 744 pts/7 S+ 11:10 0:00 | | \_ ping -W 1 -c 1 192.168.230.49 root 19558 0.0 0.0 103256 744 pts/7 S+ 11:10 0:00 | | \_ ping -W 1 -c 1 192.168.230.59 root 19559 0.0 0.0 103256 744 pts/7 S+ 11:10 0:00 | | \_ ping -W 1 -c 1 192.168.230.60 root 19578 0.0 0.0 103256 744 pts/7 S+ 11:10 0:00 | | \_ ping -W 1 -c 1 192.168.230.79 root 19579 0.0 0.0 103256 744 pts/7 S+ 11:10 0:00 | | \_ ping -W 1 -c 1 192.168.230.80 root 19586 0.0 0.0 103256 748 pts/7 S+ 11:10 0:00 | | \_ ping -W 1 -c 1 192.168.230.87 root 19587 0.0 0.0 103256 748 pts/7 S+ 11:10 0:00 | | \_ ping -W 1 -c 1 192.168.230.88 root 19594 0.0 0.0 103256 748 pts/7 S+ 11:10 0:00 | | \_ ping -W 1 -c 1 192.168.230.95
...........
root 19748 0.0 0.0 103256 744 pts/7 S+ 11:10 0:00 | | \_ ping -W 1 -c 1 192.168.230.247 root 19749 0.0 0.0 103256 748 pts/7 S+ 11:10 0:00 | | \_ ping -W 1 -c 1 192.168.230.248 root 19750 0.0 0.0 103256 744 pts/7 S+ 11:10 0:00 | | \_ ping -W 1 -c 1 192.168.230.249 root 19752 0.0 0.0 103256 744 pts/7 S+ 11:10 0:00 | | \_ ping -W 1 -c 1 192.168.230.250 root 19753 0.0 0.0 103256 748 pts/7 S+ 11:10 0:00 | | \_ ping -W 1 -c 1 192.168.230.251
|
완료 시간은 비교할 수 없을 정도로 많이 차이가 난다..
이를 응용해 여러 작업처리의 소요시간을 효과적으로 개선할 수 있을 것으로 본다.
참고로 ping 명령어 외의 통신 확인을 위해 fping 명령어를 써도 괜찮다!
# fping -g 192.168.230.230/24