1. 程式人生 > >failover 故障檢測、報警、自動切換 程式 monitor_nginx.sh 用於對web server (Nginx) 監測與故障處理

failover 故障檢測、報警、自動切換 程式 monitor_nginx.sh 用於對web server (Nginx) 監測與故障處理

前言

前幾天,群裡有人說用shell程式實現failover,所以今天自己寫一個shell程式 monitor_nginx.sh 

failover,faultdetection andswitching,故障轉移與切換程式(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 stringthatmaycontainplain

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

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

ifyit"@filename"andtotell curl to read the format from

stdin you write "@-".

The variables present in the output format willbesubstituted

bythevalue or text that curl thinks fit, as described below.

All variables are specified like %{variable_name} and tooutput

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 %mustbe doubledwhenusingthis

option.

Available variables are at this point:

url_effectiveTheURLthatwasfetched last. This is mostly

meaningful if you’ve told curltofollow loca-

tion: headers.

http_codeThenumericalcodethatwas found in the last

retrieved HTTP(S) page.

http_connectThe numerical code that wasfoundinthelast

response(fromaproxy)toacurlCONNECT

request. (Added in 7.12.4)

time_totalThe total time, in seconds, that the fullopera-

tion lasted. The time will be displayed with mil-

lisecond resolution.

time_namelookup

The time, in seconds,ittookfromthestart

until the name resolving was completed.

time_connectThetime,inseconds,ittook from the start

until the connect to the remote host(orproxy)

was completed.

time_pretransfer

Thetime,inseconds,ittook from the start

until the file transfer is just abouttobegin.

This includes all pre-transfer commands and nego-

tiations that are specific to the particular pro-

tocol(s) involved.

time_redirectThe time, in seconds, it took for all redirection

steps include name lookup,connect,pretransfer

andtransferbeforefinaltransactionwas

started. time_redirect shows the completeexecu-

tiontimeformultiple redirections. (Added in

7.12.3)

time_starttransfer

The time, in seconds,ittookfromthestart

untilthefirst byte is just about to be trans-

ferred. This includes time_pretransferandalso

thetimetheserverneedstocalculatethe

result.

size_downloadThe total amount of bytes that were downloaded.

size_uploadThe total amount of bytes that were uploaded.

size_headerThe total amount of bytes of the downloaded head-

ers.

size_requestThetotalamount of bytes that were sent in the

HTTP request.

speed_download The average download speed that curl measured for

the complete download.

speed_uploadTheaverageupload speed that curl measured for

the complete upload.

content_typeThe Content-Type of therequesteddocument,if

there was any.

num_connectsNumberof new connects made in the recent trans-

fer. (Added in 7.12.3)

num_redirectsNumber of redirects thatwerefollowedinthe

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狀態碼參考

宣告:本文件可以隨意更改,但必須署名原作者

作者:鳳凰舞者 qq:578989855