How to use Shell script to monitor server online status and email alarm
This article mainly explains "how to use Shell script to monitor server online status and email alarm". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "how to use Shell scripts to monitor server online status and email alarm"!
For the server, the online rate is very important, problems can be solved in time, but the system administrator can not always stay next to the computer, through the script monitoring website problems timely notify the administrator through mail, if it is 139mailbox can also be free SMS notification.
Note: sending mail directly through the system is easy to be intercepted. You can use mail to connect to a third party smtp to send mail.
Shell script implementation code:
The code is as follows:
#! / bin/bash
# set-x
While true
Do
List= (www.yisu.com s.jb51.net)
Mail=jmj@jb51.net
Date=$ (date-d "today" + "% Y-%m-%d-%H:%M:%S")
ITunes 0
Id=$ {# list [*]}
While [$I-lt $id]
Do
If ping-C1 ${list [$I]} > / dev/null
Then
Echo $date: server ${list [$I]} can ping.
Else
If curl-m 10 ${list [$I]} > / dev/null
Then
Echo $date: server ${list [$I]} ping is not available and can open a web page.
Else
Echo "Hello, according to the system monitoring server ${list [$I]} can not be accessed and ping is not available, please deal with it in time! time of failure: $date" | mail-s "server ${list [$I]} cannot be connected! time of failure: $date" $mail
Until
Date=$ (date-d "today" + "% Y-%m-%d-%H:%M:%S")
Ping-C1 ${list [$I]} > / dev/null & & echo "Congratulations! server ${list [$I]} has returned to normal, recovery time: $date" | mail-s "server ${list [$I]} has returned to normal! recovery time: $date" $mail
Do
Sleep 5
Done
Fi
Fi
Let iTunes +
Done
Sleep 60
Done
At this point, I believe you have a deeper understanding of "how to use Shell scripts to monitor server online status and email alarm". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!