脚本应用于:当服务器返回500、502、504错误时,检测错误原因,我这里只做了基础的判断。
①:当服务器返回值为500的时候,可能原因是MySQL数据库宕机了;
②:一般返回502、504一般可能比较高的原因是php的问题;
说明:其实我不想做监控,不过阿里云的磁盘IO比较“呵呵”,负载经常莫名升到很高,只好做监控通知我喽。SourceByrd's Weblog-https://note.t4x.org/code/check-web-internal-server-error/
通用版:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#!/bin/bash # Author:Byrd # Version:1.0 # Site:note.t4x.org # Contact:root#t4x.org while true do HttpCode=`curl -o /dev/null -s -w "%{http_code}" http://note.t4x.org` MysqlProcess=`ps aux | grep mysql | grep -v grep | wc -l` MysqlPort=`netstat -tunlp | grep 3306 | wc -l` Pid=/usr/local/php/var/run/php-fpm.pid # No port 3306 or mysql process if [ "$MysqlProcess" -lt 2 ] || [ "$MysqlPort" -lt 1 ] ;then /etc/init.d/mysqld start echo "Mysql server Start"|mail -s "Mysql Start Sucessful" admin#t4x.org sleep 10 fi # Server 500 error and no port 3306 if [ "$HttpCode" -ge 500 ] && [ `netstat -tunlp | grep 3306 | wc -l` -lt 1 ];then /etc/init.d/mysqld start sleep 10 echo "Mysql server Start"|mail -s "Mysql Restart ERR 500" admin#t4x.org # Server 502 error Restart process of PHP elif [ "$HttpCode" -eq 502 ];then if [ ! -f "$Pid" ];then /usr/local/php/sbin/php-fpm echo "Start PHP Process"|mail -s "Start PHP Process" admin#t4x.org else kill `cat $Pid` && /usr/local/php/sbin/php-fpm echo "Restart PHP Process"|mail -s "Restart PHP Process Error Code 502" admin#t4x.org fi # Server 504 error Restart process of PHP elif [ "$HttpCode" -eq 504 ];then if [ ! -f "$Pid" ];then /usr/local/php/sbin/php-fpm echo "Start PHP Process"|mail -s "Start PHP Process" admin#t4x.org else kill `cat $Pid` && /usr/local/php/sbin/php-fpm echo "Restart PHP Process"|mail -s "Restart PHP Process Error Code 504" admin#t4x.org fi fi sleep 10 done |
个人版:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
#!/bin/bash # Author:Byrd # Version:1.0 # Site:note.t4x.org # Contact:root#t4x.org while true do HttpErr=`curl -I note.t4x.org -s | grep Location | sed 's#^.*error.t4x.org/##g' | sed 's#.html.*$##g'` HttpCode=`curl -o /dev/null -s -w "%{http_code}" http://note.t4x.org` NginxPro=`ps aux | grep 'worker process' | grep -v grep | wc -l` NginxPort=`netstat -tunlp | grep 80 | wc -l` MysqlProcess=`ps aux | grep mysql | grep -v grep | wc -l` MysqlPort=`netstat -tunlp | grep 3306 | wc -l` Pid=/usr/local/php/var/run/php-fpm.pid Load=`uptime | sed 's#^.*erage: ##g' | awk -F '.' '{printf $1}'` # Load Value >= 2,then send mail to administrator. if [ "$Load" -ge 2 ];then echo "Server Load Value:$Load"|mail -s "Server Load Value:$Load" admin#t4x.org fi # No Open Port 80 or Web Service Process is less than 2,then send mail to administrator. if [ "$NginxPort" -lt 1 ] || [ "$NginxPro" -lt 2 ];then /usr/local/nginx/sbin/nginx echo "Web Server Start"|mail -s "Web Server Start Sucessful" root@t4x.org sleep 10 fi # Mysql Service Process is less than 2 or No Open Port 80,then send mail to administrator. if [ "$MysqlProcess" -lt 2 ] || [ "$MysqlPort" -lt 1 ] ;then /etc/init.d/mysqld start echo "Mysql server Start"|mail -s "Mysql Start Sucessful" admin#t4x.org sleep 10 fi # Returns The Value 302,Prove Website Error. if [ "$HttpCode" -eq 302 ];then # The Return Value Is Greater Than Or Equal To 500 And 3306 Port Are Not Open if [ "$HttpErr" -ge 500 ] && [ `netstat -tunlp | grep 3306 | wc -l` -lt 1 ];then /etc/init.d/mysqld start sleep 10 echo "Mysql server Start"|mail -s "Mysql Restart ERR 500" admin#t4x.org # The Return Value Is Equal To 502 And There Is No PHP Pid. elif [ "$HttpErr" -eq 502 ];then if [ ! -f "$Pid" ];then /usr/local/php/sbin/php-fpm echo "Start PHP Process"|mail -s "Start PHP Process" admin#t4x.org else kill -USR2 `cat $Pid` echo "Restart PHP Process"|mail -s "Restart PHP Process Error Code 502" admin#t4x.org fi # The Return Value Is Equal To 504 And There Is No PHP Pid. elif [ "$HttpErr" -eq 504 ];then if [ ! -f "$Pid" ];then /usr/local/php/sbin/php-fpm echo "Start PHP Process"|mail -s "Start PHP Process" admin#t4x.org else kill `cat $Pid` && /usr/local/php/sbin/php-fpm echo "Restart PHP Process"|mail -s "Restart PHP Process Error Code 504" admin#t4x.org fi fi fi sleep 30 done |
个人版修正:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
#!/bin/bash # Author:Byrd # Version:1.0 # Site:note.t4x.org # Contact:root#t4x.org while true do HttpErr=`curl -I note.t4x.org -s | grep Location | sed 's#^.*error.t4x.org/##g' | sed 's#.html.*$##g'` HttpCode=`curl -o /dev/null -s -w "%{http_code}" http://note.t4x.org` NginxPro=`ps aux | grep 'worker process' | grep -v grep | wc -l` NginxPort=`netstat -tunlp | grep 80 | wc -l` MysqlPro=`ps aux | grep mysql | grep -v grep | wc -l` MysqlPort=`netstat -tunlp | grep 3306 | wc -l` Pid=/usr/local/php/var/run/php-fpm.pid Webmail=root@t4x.org LoadValog=/byrd/logs/uptime.txt WebIP=`awk '{print $1}' /usr/local/nginx/logs/access.log | sort | uniq -c | sort -rn -k1 | head -20` # Load Value >= 1,then send mail to administrator. Load=`uptime | sed 's#^.*erage: ##g' | awk -F '.' '{printf $1}'` if [ "$Load" -ge 1 ];then top -b | head -118 >$LoadValog iostat -x 1 5 >>$LoadValog $WebIP >>$LoadValog mail -s "Server load value:$Load" $Webmail < $LoadValog # echo "Server load value:$Load"|mail -s "Server load value:$Load" root@t4x.org fi # No Open Port 80 or Web Service Process is less than 2,then send mail to administrator. if [ "$NginxPort" -lt 1 ] || [ "$NginxPro" -lt 2 ];then /usr/local/nginx/sbin/nginx echo "Web Server Start"|mail -s "Web Server Start Sucessful" root@t4x.org sleep 10 fi # Mysql Service Process is less than 2 or No Open Port 3306,then send mail to administrator. if [ "$MysqlPro" -lt 2 ] || [ "$MysqlPort" -lt 1 ] ;then /etc/init.d/mysqld start echo "Mysql server Start"|mail -s "Mysql Start Sucessful" root@t4x.org sleep 10 fi # Returns The Value 302,Prove Website Error. if [ "$HttpCode" -eq 302 ];then # The Return Value Is Greater Than Or Equal To 500 And 3306 Port Are Not Open if [ "$HttpErr" -ge 500 ] && [ `netstat -tunlp | grep 3306 | wc -l` -lt 1 ];then /etc/init.d/mysqld start sleep 10 echo "Mysql server Start"|mail -s "Mysql Restart ERR 500" root@t4x.org # The Return Value Is Equal To 502 And There Is No PHP Process. elif [ "$HttpErr" -eq 502 ];then if [ ! -f "$Pid" ];then /usr/local/php/sbin/php-fpm echo "Start PHP Process"|mail -s "Start PHP Process" root@t4x.org else kill -USR2 `cat $Pid` echo "Restart PHP Process"|mail -s "Restart PHP Process Error Code 502" root@t4x.org fi # The Return Value Is Equal To 504 And There Is No PHP Process. elif [ "$HttpErr" -eq 504 ];then if [ ! -f "$Pid" ];then /usr/local/php/sbin/php-fpm echo "Start PHP Process"|mail -s "Start PHP Process" root@t4x.org else kill `cat $Pid` && /usr/local/php/sbin/php-fpm echo "Restart PHP Process"|mail -s "Restart PHP Process Error Code 504" root@t4x.org fi fi fi sleep 30 done |
个人版说明:因为我的错误页面全部做了302转向,因此需要独立做判断。SourceByrd's Weblog-https://note.t4x.org/code/check-web-internal-server-error/ SourceByrd's Weblog-https://note.t4x.org/code/check-web-internal-server-error/
申明:除非注明Byrd's Blog内容均为原创,未经许可禁止转载!详情请阅读版权申明!