wrote a Shell version Mini monitoring scripts, purely to share with you the study , be interested in learning Shell can look down. (*_*)
Total 3 a script file service.sh, daemon.sh, tcp.sh
Implemented a number of basic functions: port monitoring, survival monitoring, off-line mail alarm, re-launch email notification
will be 3 files in a single directory
settings: In tcp.sh you can set the email notification address, and the number of messages
Run: ./service.sh Start
650) this.width=650; "title=" q1.jpg "src=" http://s3.51cto.com/wyfs02/M01/59/97/wKiom1TYiFiCEPAzAADZn1sFlDM579.jpg "alt=" Wkiom1tyificepazaadzn1sfldm579.jpg "/>
The main file code is as follows:
tcp.sh file
#!/bin/bashlang=c# Email address settings fromaddress= "[email protected]" #From地址toaddress = "[Email protected] " #发送地址ccaddress =" [email protected] " #抄送地址 # Mail alarm from multiple times remun=2# Monitored server, port list server_all_list= (192.168.70.25:2004 192.168.70.24:80 192.168.16.56:80 ) ############# #################################################### #采用mail决定发送报警消息. Send_msg () {/bin/grep "$server _ip" mun.tmp > /dev/null 2>&1if [ $? -eq 0 ]then mun=$ (grep "$server _ip" mun.tmp | awk -F ': ' ' {print $2} ' if [ $mun -lt $remun ];then echo $messages | mail -c $ccaddress -s $server _ip is down " $toaddress -- -f $fromaddress newmun=$[$mun + 1] / bin/sed -i ' s/' $server _ip ': ' $mun '/' $server _ip ': ' $newmun '/g' mun.tmp fielse echo $server _ip:1 ' >> mun.tmp echo "$messages" | mail -c $ccaddress -s "$server _ip is down" $toaddress -- -f $fromaddress # echo "$ Messages "# echo " $server _ip "fi} #重置邮件已发送次数记录reset_mun () {/bin/sed -i '/' $server _ip '/d ' mun.tmpecho "server $server _ip online" | mail -c $ccaddress -s "$ Server_ip server online " $toaddress -- -f $fromaddress}########## #server_all_len =${#server_all_list [*]}i=0while [ $i -lt $server _all_len ]do server_ip=$ (echo ${server_all_list[$i]} | awk -f ': ' ' {print $1} ') server_port=$ (echo ${server_all_list[$i]} | awk -f ': ' ' {print    &NB} ')sp; #status: 0,http down 1,http ok 2,http down but ping ok if nc -vv -z -w 10 $server _ip $server _port > /dev/null 2>&1 then status=1 messages= " server $server _ip,port $server _port can ' t access! ' else if ping -c 1 $server _ip > /dev/null 2>&1 then status=2 messages= "server $server _ip,port $ server_port cannot access,but ping is connected! " else status=0 messages= "server $server _ip,port $server _port and ping cannot access! " fi fi if [ $status -eq 1 ];then grep "$server _ip" Mun.tmp > /dev/null 2>&1 if [ $? -eq 0 ];then reset_mun fi else send_msg fi let i++done
daemon.sh
#!/bin/shdir= ' pwd ' while truedo/bin/sh $dir/tcp.sh >/dev/null 2>&1 sleep 180done
service.sh
#!/bin/shdir= ' pwd ' Start () {ps -ef |grep -v grep|grep daemon.sh > / Dev/null 2>&1if [ $? -eq 0 ];thenexitelse/usr/bin/nohup /bin/sh $dir/daemon.sh 2>&1 > /dev/null &fi}stop () {pid=$ (ps -ef |grep -v grep|grep daemon.sh|awk ' {print $2} ') kill $PID}reload () { Stopstart}status () {ps -ef |grep -v grep|grep daemon.sh > /dev/null 2>&1if [ $? -eq 0 ];thenecho "Daemon.sh is running ..." elseecho "Daemon.sh is stop" fi}case "$" in start) start && exit 0 $1 ;; stop) stop && exit 0 $1 ;; status) status && exit 0 $1 ;; reload) reload && exit 0 $1 ;; *) echo $ "Usage: $0 { Start|stop|status|reload} " exit 2esac
This article is from the "itdept" blog, make sure to keep this source http://itdept.blog.51cto.com/1034307/1613115
Shell Mini-Version Monitoring script