failover 故障检测、报警、自动切换 程序 monitor_nginx.sh 用于对web server (Nginx) 监测与故障处理

本文介绍了一个用于Nginx故障转移的Shell脚本,该脚本能够监测Nginx状态并在检测到故障时自动发送报警邮件,并通过修改IP地址实现热备切换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

前言

前几天,群里有人说用shell程序实现failover,所以今天自己写一个shell程序 monitor_nginx.sh

failover,fault detection and switching ,故障转移与切换程序(shell)

一.程序功能

用于检测web server (Nginx)是否宕掉,如果是用短信报警(sendmail139邮箱报警)并自动故障处理,浮动改热备机IP,实现故障转移与自动切换

二.系统架构

如图所示

172.29.141.112 上部署 Nginx 与 fastcgi(对外开放)

172.29.141.113 上部署 Nginx 与 fastcgi (作为热备)

172.29.141.115 上部署 MySQL

三. 程序源码

cat monitor_nginx.sh


[c-sharp] view plain copy print ?
  1. #!/bin/bash
  2. #author longxibendi
  3. #blog http://blog.youkuaiyun.com/longxibendi
  4. #function failover the web server and send message to SA
  5. # send email to longxibendi@139.com
  6. function_sendEmail ()
  7. {
  8. /usr/local/bin/sendEmail -f monitor_sys@163.com -t longxibendi@139.com -s smtp.163.com -u "web sever down" -xu monitor_sys -xp 123456789 -m "` date ` "
  9. }
  10. # change ip address
  11. function_change_ip ()
  12. {
  13. /sbin/ifconfig eth0 172.29.141.112 broadcast 172.29.141.255 netmask 255.255.255.0 up
  14. /sbin/route add -host 172.29.141.112 dev eth0
  15. /sbin/arping -I eth0 -c 3 -s 172.29.141.112 172.29.141.115
  16. ### /sbin/arping is to add arp list in 172.29.141.115
  17. }
  18. # to start local nginx web server
  19. function_start_nginx_server ()
  20. {
  21. ulimit -SHn 65535
  22. /usr/local/webserver/php/sbin/php-fpm start
  23. /usr/local/webserver/nginx/sbin/nginx
  24. }
  25. # main function
  26. function_main ()
  27. {
  28. while true
  29. do
  30. http_status_code=`curl -o /dev/null -s -w %{http_code} http://172.29.141.112/wordpress/`
  31. # echo "$http_status_code";
  32. if [ ${http_status_code} != 200 ] ;then
  33. function_sendEmail ;
  34. function_change_ip ;
  35. function_start_nginx_server ;
  36. fi
  37. # execute curl per 3 seconds
  38. sleep 3
  39. done
  40. }
  41. function_main ;

四.程序说明

function_sendEmail 发送email到139邮箱,报警

function_change_ip 浮动更改ip,添加路由,修改db的arp表

function_start_nginx_server 启动本机nginx服务器,这一步也可以调整,如果本机的nginx已启动的话

function_main 主函数,通过curl 网址得到状态码

基本思想有两条

<!--[if !supportLists]-->1.<!--[endif]-->通过curl检测故障

<!--[if !supportLists]-->2.<!--[endif]-->通过浮动修改(ifconfig) ip 实现故障切换(failover)

五.如何部署

在172.29.141.113上部署。如果在112上部署,不需要function_change_ip这个函数。这是一种简单的解决方案,也可以用keepalive,如果PV很大,可能需要用lvs+keepalive或者硬件负载均衡器。

六.参考资料

1. man curl 这里给出重要的参数

-w/--write-out <format>

Defines what to display on stdout after a completed and success-

ful operation. The format is a string that may contain plain

text mixed with any number of variables. The string can be spec-

ified as "string", to get read from a particular file you spec-

ify it "@filename" and to tell curl to read the format from

stdin you write "@-".

The variables present in the output format will be substituted

by the value or text that curl thinks fit, as described below.

All variables are specified like %{variable_name} and to output

a normal % you just write them like %%. You can output a newline

by using /n, a carriage return with /r and a tab space with /t.

NOTE: The %-letter is a special letter in the win32-environment,

where all occurrences of % must be doubled when using this

option.

Available variables are at this point:

url_effective The URL that was fetched last. This is mostly

meaningful if you’ve told curl to follow loca-

tion: headers.

http_code The numerical code that was found in the last

retrieved HTTP(S) page.

http_connect The numerical code that was found in the last

response (from a proxy) to a curl CONNECT

request. (Added in 7.12.4)

time_total The total time, in seconds, that the full opera-

tion lasted. The time will be displayed with mil-

lisecond resolution.

time_namelookup

The time, in seconds, it took from the start

until the name resolving was completed.

time_connect The time, in seconds, it took from the start

until the connect to the remote host (or proxy)

was completed.

time_pretransfer

The time, in seconds, it took from the start

until the file transfer is just about to begin.

This includes all pre-transfer commands and nego-

tiations that are specific to the particular pro-

tocol(s) involved.

time_redirect The time, in seconds, it took for all redirection

steps include name lookup, connect, pretransfer

and transfer before final transaction was

started. time_redirect shows the complete execu-

tion time for multiple redirections. (Added in

7.12.3)

time_starttransfer

The time, in seconds, it took from the start

until the first byte is just about to be trans-

ferred. This includes time_pretransfer and also

the time the server needs to calculate the

result.

size_download The total amount of bytes that were downloaded.

size_upload The total amount of bytes that were uploaded.

size_header The total amount of bytes of the downloaded head-

ers.

size_request The total amount of bytes that were sent in the

HTTP request.

speed_download The average download speed that curl measured for

the complete download.

speed_upload The average upload speed that curl measured for

the complete upload.

content_type The Content-Type of the requested document, if

there was any.

num_connects Number of new connects made in the recent trans-

fer. (Added in 7.12.3)

num_redirects Number of redirects that were followed in the

request. (Added in 7.12.3)

ftp_entry_path The initial path libcurl ended up in when logging

on to the remote FTP server. (Added in 7.15.4)

2.HTTP状态码参考

http://baike.baidu.com/view/1790469.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值