With the need to monitor some of the Apache servers that require high availability, in addition to professional monitoring and alerting devices, it is a good idea to write a script that automatically monitors Apache status on the Apache server at low cost. Search on the Internet a lot of similar scripts, but because of the larger limitations, there are some imperfections, so I wrote one.
Scripting features and Features
1. Ability to monitor the availability of Apache servers every once in a while (since this script directly simulates client access, the "availability" here refers to a surly normal accessibility)
2, in the presence of inaccessible circumstances, can automatically restart the Apache service (forced restart)
3, after the restart is still not normal access, the automatic implementation of further operations (such as sending mail, restart the server, etc.)
The following text will give the specific use of this script and comments, you can manually modify according to their own situation (need to understand a certain bash shell programming).
Script content:
#!/bin/bash url= "HTTP://127.0.0.1/" Curlit () {curl–connect-timeout 15–max-time 20–head–silent "$URL" | grep ' 200′#
The above 15 is the connection timeout, if access to localhost HTTP service more than 15s still does not respond correctly to the 200-head code, it is judged inaccessible. Doit () {if! Curlit then # if localhost Apache service does not return 200 headers correctly, an exception occurs. Execute the following command: Sleep top-n 1-b >>/var/log/apachemonitor.log # The top command content is written to date to file for reference/usr/bin/killall-9 apache2 &&am P /usr/bin/killall-9 php5-cgi &&/usr/bin/killall-9 httpd &&/usr/bin/killall-9 http &&/usr/bin/ killall-9 Apache &&/usr/bin/killall-9 php-cgi >/dev/null # is compatible, killing a variety of Apache processes. You can modify the sleep 2/etc/init.d/apache2 Start >/dev/null/etc/init.d/httpd start >/dev/null # Compatibility based on the features of your Apache service, and execute two AP
Ache Restart command, can be modified as needed. echo $ (date) "Apache restart" >>/var/log/apachemonitor.log # Write log Sleep 30 # wait 30 seconds after reboot completes, and then try again if! Curlit; Then # If you still cannot access, then: Echo $ (date) failed! Now Reboot computer! ">>/var/log/apachemonitor.log # write to Apache still restart invalid log Reboot # reboot machine. Actually restarting the entire server is a nonOften compelled to do so. I do not propose.
Everyone according to the need to modify their own, such as text messages, e-mail alarm or something. Fi Sleep 180 fi 300 # 5 minutes after the script is run (prevents the server from restarting because Apache is not starting to cause a miscalculation) while true;
Do # main circulation body doit >/dev/null sleep done
How to use
the use of the method is very flexible, in short, let the above script after the boot has been done. Because the script uses the Curl command, you also need to install the Curl environment. How to Install Curl:
Select the following two commands according to the distribution:
Yum Install Curl
apt-get Install Curl
If you want to start it automatically, you can write the script to the rc.local.