Monitor whether the running service is normal and send an email alert
Monitoring Service, which is a task placed on crontab. I do not like to run it in the background using an endless loop.
Therefore, three cycles are executed each time. If any of the services is down, an alarm is triggered.
The effect of the three rewrite methods is the same.
#! /Bin/bash
Nmap-p 6065 10.31.6.6 | egrep "6065/tcp open" &>/dev/null
Code1 = $?
Nmap-p 80 10.31.6.6 | grep "80/tcp open http">/dev/null 2> & 1
Code2 = $?
Curl http: // 10.31.6.6/test.html/> &/dev/null
Code3 = $?
My_mail (){
Mail_list = ("test@hotmail.com.cn" "test2@hotmail.com.cn ")
Now_date = 'date "+ % Y-% m-% d % T "'
For I in $ {mail_list [@]}
Do
Echo-e "webchat is down, please check; \ nip: 42.62.5.100 \ n $ now_date" | mail-s "webchat down" $ I
Done
}
N = 1
Count = 1
While (n <4 ))
Do
If [$ code1 = 0] & [$ code2 = 0] & [$ code3 = 0]
Then
N = $ ($ n + 1 ))
Sleep 2
Else
Count = $ ($ count + 1 ))
N = $ ($ n + 1 ))
Sleep 2
If [$ count-eq 3]; then
My_mail
Fi
Fi
Done
This article permanently updates the link address: