test System version: CentOS 6.4
role: ping Background monitoring multiple hosts, according to the number of drops to take appropriate action
use: to monitor the host as a parameter, can be run in the foreground or in the background, CTRL + C end, the background will need to run
Kill-2 $ (cat. Mypid) Ends (. Mypid is the PID of the script master process)
Demonstrate:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7F/18/wKiom1cTPfThjzIgAAAeOPWoAgQ936.png "style=" float: none; "title=" 1.png "alt=" Wkiom1ctpfthjzigaaaeopwoagq936.png "/>
Running in the background
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7F/16/wKioL1cTPrHhdy_NAAAwMCbttcE629.png "style=" float: none; "title=" 3.png "alt=" Wkiol1ctprhhdy_naaawmcbttce629.png "/>
Use Pstree-ap to view related processes
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7F/18/wKiom1cTPvKisnxzAACFtH-4-Qs520.png "title=" 2.png " alt= "Wkiom1ctpvkisnxzaacfth-4-qs520.png"/>
Monitoring post-execution logs
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7F/16/wKioL1cTP_TiJfAlAAB1uaZUP9c485.png "title=" 4.png " alt= "Wkiol1ctp_tijfalaab1uazup9c485.png"/>
Code area:
#!/bin/bashmail= ' [email protected] ' #定义丢包个数failed_packages_sum =2ping_timeout=5mypid=$ $timestamp () {echo -n "$ (date +"%y-%m-%d %h:%m:%s ") "} #符合条件后的操作函数, you can define additional action action () {echo ---------- -----$ (timestamp)--------------- >>${1}.logtraceroute -n -m 11 -q 2 $ 1 >>${1}.log mail -s "Ping $1 failed " $MAIL <${1}.log}exec_ping () {local stop_exec=false local failed_sum=0 trap ' stop_exec=true ' 20while ! $stop _execdoping -c 1 -w $ping _ timeout $1 &>/dev/null && failed_sum=0 | | ( failed_sum+=1 ) [[ $failed _sum -eq $failed _packages_sum ]] & & action $1done}stop=false# only accept this signal, the script will be able to properly end all background functions trap ' stop=true ' 2echo $MYPID >.mypid# background Run monitoring function, and record Pidwhile [[ $# -ne 0 ]]doexec_ping $1 &pids_arry= ( ${pids_arry[@]} $! ) shiftdonewhile ! $STOP; do sleep 1; donefor ( index=0; index<${# pids_arry[@]}; index++ )) Dokill -20 ${pids_arry[index]}done
Accessories Download Source Code
This article is from the "retrograde person" blog, please be sure to keep this source http://lingyi.blog.51cto.com/2837715/1764760
Script Application 14: Ping monitors multiple host drops