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

47 篇文章 0 订阅
20 篇文章 0 订阅

 

前言

前几天,群里有人说用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


 

 

.程序说明

function_sendEmail                     发送email139邮箱,报警

function_change_ip                     浮动更改ip,添加路由,修改dbarp

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

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

 

基本思想有两条

1. 通过curl检测故障

2. 通过浮动修改(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

 

 

 

声明:本文档可以随意更改,但必须署名原作者

作者:凤凰舞者 qq:578989855

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对您的问题,我可以为您提供如下 redis_master.sh 脚本配置: ``` #!/bin/bash # redis_master.sh: redis master failover script for keepalived REDIS_CONF="/etc/redis/redis.conf" REDIS_CLI="/usr/local/bin/redis-cli" REDIS_PID=`cat /var/run/redis.pid` # promote the slave as the new master promote_new_master() { # get the IP address of the slave that will be promoted new_master_ip=$(/sbin/ip addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}') # promote the slave $REDIS_CLI SLAVEOF NO ONE # update the redis configuration file to reflect the new master sed -i "s/^\(slaveof\).*/\1 $new_master_ip 6379/" $REDIS_CONF # restart redis /etc/init.d/redis-server restart } # check if redis master is running if [ -z "$REDIS_PID" ]; then # redis master is not running, promote the slave promote_new_master else # redis master is running, check if it is responding redis_status=`$REDIS_CLI PING` if [ "$redis_status" != "PONG" ]; then # redis is not responding, promote the slave promote_new_master fi fi ``` 这是用于 Redis 双主模式下 keepalived 自动切换的脚本,当 keepalived 检测到 master 节点宕机后,会执行该脚本将 slave 节点提升为新的 master 节点。 该脚本的作用是检测当前 Redis 是否处于 master 节点状态,如果是,则检测 Redis 是否能够正常响应,如果不能,则将 slave 节点提升为新的 master 节点。如果 Redis 不处于 master 节点状态,则直接将 slave 节点提升为新的 master 节点。 需要注意的是,该脚本需要在 Redis 的 master 节点上运行,同时需要配置 keepalived 的 notify_master 和 notify_backup 选项,以便在 Redis 切换自动执行该脚本。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值