Recently, the server room where the company is located is often unable to access the website due to attacks and network faults. In order to be able to know that the website is inaccessible for the first time and quickly solve the problem, tley uses shell scripts to monitor server access conditions. Once a website cannot be accessed, an SMS alert is triggered. For more information, see the blog post on Ghost and junnan. Thank you !! First, it is necessary that the website cannot be accessed normally due to attacks and network faults in the machine room where the company server is located recently. In order to be able to know that the website cannot be accessed at the first time and quickly solve the problem, tley uses shell scripts to monitor server access conditions. Once a website cannot be accessed, an SMS alert is triggered. For more information, see the blog post on Ghost and junnan. Thank you !! First, you must have a mobile phone number that has been activated for Apsara stack. Then, add the mobile phone number that has received the alert text message to your Apsara stack friends (or you can send it to yourself) and call Apsara's free SMS interface to send the alert text message, you can also use MSN to send alarms in a more timely manner than text messages. Edit the script http. shvi/opt/http. sh #! /Bin/sh
LANG = C # list of monitored servers and ports
Server_all_list = (\
Www.qq.com: 80 \
Www.163.com: 80 \
Www.sina.com: 80 \
) Telnum = the mobile phone number of the activated Apsara Stack
Passwd = fetion password date = $ (date-d "today" + "% Y-% m-% d _ % H: % M: % S ") # Send detection information to the interface program using http post. php, the interface program is responsible for analyzing information and deciding whether to send an alarm MSN message, SMS, and email.
Send_msg_to_interface ()
{
If [[$2 = "0"] | [[$2 = "2"]; then
# Start sending alert text messages. 13500000000 is the recipient's mobile phone number
Curl-m 600-d username =$ {telnum}-d password =$ {passwd}-d sendto = 13500000000-d message = $1 http://sms.api.bz/fetion.php
Echo "'date + '% Y-% m-% d % H: % M: % s' $1"> http_down.log
# Send an msn alarm message (comment this line if you do not need an msn alarm)
# Curl-m 600-d menu = http-d date = $ date-d ip = $ server_ip-d port = $ server_port-d status = $ status http: // 127.0.0.1/monitor/interface. php
Fi;} server_all_len =$ {# server_all_list [*]}
I = 0
While [$ 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 $2 }')
Server_message = ""
If curl-m 10-G http: // $ {server_all_list [$ I]}/>/dev/null 2> & 1
Then
# Status: 0, http down 1, http OK 2, http down but ping OK
Status = 1
Echo "server $ {server_ip}, port $ {server_port} can be accessed normally! ";
Server_message = "server $ {server_ip}, port $ {server_port} can be accessed normally! ";
Else
If curl-m 30-G http: // $ {server_all_list [$ I]}/>/dev/null 2> & 1
Then
Status = 1
Echo "server $ {server_ip}, port $ {server_port} can be accessed normally! "
Server_message = "server $ {server_ip}, port $ {server_port} can be accessed normally! ";
Else
If ping-c 1 $ server_ip>/dev/null 2> & 1
Then
Status = 2
Echo "server $ {server_ip}, port $ {server_port} cannot be accessed, but it can be pinged! ";
Server_message = "server $ {server_ip}, port $ {server_port} cannot be accessed, but can be pinged! ";
Else
Status = 0
Echo "server $ {server_ip}, port $ {server_port} cannot be accessed and cannot be pinged! ";
Server_message = "server $ {server_ip}, port $ {server_port} cannot be accessed and cannot be pinged! ";
Fi
Fi
Fi
Send_msg_to_interface "$ {server_message}" "$ {status }";
Let I ++
Done interface. php (you need to set the monitoring server to make it accessible, for example, http: // 127.0.0.1/monitor/interface. php, which corresponds to the above script)
Here, a PHP sendMsg class is required to send messages to MSN through PHP. After downloading the message, extract the directory to the interface. php Directory. Include ('sendmsg/sendmsg. php ');
// HTTP server monitoring
If (isset ($ _ POST ["menu"])
{
If (htmlspecialchars ($ _ POST ["menu"]) = "http ")
{
$ Date = htmlspecialchars ($ _ POST ["date"]);
$ Ip = htmlspecialchars ($ _ POST ["ip"]);
$ Port = htmlspecialchars ($ _ POST ["port"]);
$ Status = htmlspecialchars ($ _ POST ["status"]); // status. 0 indicates that access is unavailable, 1 indicates normal, 2 indicates access is unavailable, but ping is successful.
$ Message = '';
Switch ($ status)
{
Case '0 ':
$ Message = $ ip. 'server inaccessible ';
Break;
Case '1 ':
$ Message = $ ip. 'server is working properly ';
Break;
Case '2 ':
$ Message = $ ip. 'The server cannot be accessed but can be ping ';
Break;
}
If ($ status = 0 & $ status = 1)
{
$ SendMsg = new sendMsg ();
$ SendMsg-> login ('msn account used for message sending ', 'Password ');
$ SendMsg-> createSession ('msn account used to receive message ');
$ SendMsg-> sendMessage (iconv ("GBK", "UTF-8", $ message), 'times New Roman ', 'ff0000 ');
}
}
}
Then run crontab-e and add the following statement: */5 ***** sh/opt/http. THE sh service crond restart script will be executed every five minutes. If a website fault is detected, an alarm will be triggered.
The above script has been running normally for a long time and can be used to know the server down status immediately. However, it has recently been found that alarms cannot be triggered, and the SMS interface of fetion cannot be called, instead, you can only trigger alarms by sending 139 emails. Bind your email to your mobile phone and use sendEmail to send alarm Emails: cd/opt
Wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
Tar-zxvf sendEmail-v1.56.tar.gz
Cd sendEmail-v1.56
Cp sendEmail/usr/local/bin
Chmod + x/usr/local/bin/sendEmail # How to Use sendEmail
SendEmail-f 13512345678@139.com-t 13512345678@139.com-s smtp.139.com-u "from cacti"-xu 13512345678@139.com-xp 123456-o message-charset = UTF-8-m "test mail sending"
# Explanation:-f indicates the sender's email address.
-T indicates the recipient's email address.
-S indicates the domain name or ip address of the SMTP server
-U indicates the subject of the email.
-Xu indicates the SMTP authentication user name
-Xp indicates the password for SMTP verification. (Note that this password seems to be restricted. For example, I use d! 5neyland cannot be correctly identified)
-M indicates the content of the email. If you do not include the-m parameter, you will be prompted to enter it by yourself.
-O text information character set. If this item is not added, the Chinese information sent will become garbled. The curl-m 600-d username =$ {telnum}-d password =$ {passwd}-d sendto = 13500000000-d message in the original script = $1 http://sms.api.bz/fetion.php modified: sendEmail-f 13512345678@139.com-t 13512345678@139.com-s smtp.139.com-u "from cacti"-xu 13512345678@139.com-xp 123456-o message-charset = UTF-8-m $1