Develop shell script to check nginx real-combat sharing
First, this script implements the function:
1, automatic check nginx under the agent node is normal
2, through the page display status, the problematic node gives the page alarm and sound alarm.
3, Add new node, page automatically load new node, no need to modify the program.
Second, the Guardian check script
[[email protected] extra]# cd /server/scripts/[[email protected] scripts]# vi nginx_check.sh#!/bin/bash# oldboy training 21 zhangyao# defined Variablesnginxdir=/application/nginxextrapath= $NginxDir/conf/extrascriptdir=/server/scriptsstatuslog=$ scriptdir/status.logstatushtml= $NginxDir/html/status/status.htmlstatushtmlori= $NginxDir/html/status/ status.html.ori# judge some files [ -d $NginxDir ] | | exit 1[ -d $ScriptDir ] | | mkdir -p $ScriptDir [ -f $StatusLog ] | | touch $StatusLog [ -f $StatusHtml ] | | touch $StatusHtml # defined check url functionsfunction check_url () {status= ' curl -s $2/check.html ' if [ "$status" == "OK" ] then echo "$1 $2 up" >> $StatusLogelse &NBSP;&NBSP;&NBSP;&NBsp; echo "$1 $2 down" >> $StatusLogfi}# defined list url and check functionsfunction check () {> $StatusLog cd $ExtraPath for file in ' ls ' #首先遍历extra目录下的所有文件, then traverse the IP line of each file and pass the parameters to Check_url do url= (' awk -f ' [ ]+ ' '/server/ {print $3} ' $file ') for i in ${url[*]} do check_url $file $i donedone}# defined html table format functionsfunction table () { #将表格的一行语句累加后一次性插入html文件char = "<tr bgcolor=" ><th>$2</th>< Th>$3</th><th>$4</th><th>$5</th></tr> "sum=" $sum "$char"}function HTML () {INDEX=1&NBSP;&NBSP; #表格最左侧的一列, initial value 1flag=0sum= "" #行语句初始值null/bin/cp $StatusHtmlOri $StatusHtml #将status html file initialization while read line #一行行读入 $StatusLog file, formatted as dynamic_pools 10.0.0.6:80 up do array_line= ($line) if [ "${array_line[2"} " == " Up " ] then table "#90EE90" $Index ${array_ line[*]} #将颜色参数, index value and other parameters to the table function else table #FF0000 $Index ${array_line[*]} ((flag++) #down情况下flag会计数 fi ((index++)) done< $StatusLog [ $flag -eq 0 ] | | \ #如果flag不为0, there must be a down machine, add a voice alarm function, modeled zabbix sum= $sum "<audio id=" Clicksound " autoplay=" autoplay "><source src=" Warning.mp3 " type=" audio/mpeg "></audio>" sed -i "/c0c0c0/a $sum" $StatusHtml #将sum语句插入html文件}# defined main Functionsfunction main () {while true do check html sleep 5done}main
Iii. Related Documents
Status.html.ori
<!doctype html>
Status.html
<!doctype html> Four, nginx.conf site configuration
worker_processes 1;events { worker_connections 1024;} http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; include extra/static_pools; include extra/dynamic_pools; server { listen 80; server_name www.etiantian.org; location / { if ($http _user_agent ~* "MSIE") { rewrite ^/ http://10.0.0.6/ie.html; } root html; index index.html index.htm; } location /image/ { proxy_pass http://static_pools; include proxy.conf; } location /dynamic/ { proxy_pass http ://dynamic_pools; include proxy.conf; }&nbSp; }}extra/dynamic_pools contains files Upstream dynamic_pools { server 10.0.0.6:80 weight=5;} Extra/static_pools contains files upstream static_pools { server 10.0.0.5:80 weight=5; server 10.0.0.6:80 weight=5;}
Site Health Check file: check.html
Ok
Five, the effect
Normal status effect:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6D/B8/wKioL1VqrMqQQ_R-AAUXeGAKVhU542.jpg "style=" float: none; "title=" old boy It Effect education 0016321.jpg "alt=" wkiol1vqrmqqq_r-aauxegakvhu542.jpg "/> Alarm effect node fault entry is red, and there is a sound alarm (can also implement mail, SMS alarm)
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6D/BD/wKiom1VqqzShouizAAUSDTZkFbk159.jpg "style=" float: none; "title=" QQ picture 20150531143033.png "alt=" Wkiom1vqqzshouizaausdtzkfbk159.jpg "/>
Thank you! This article is from the 21-period student Zhang Yao!
This article is from the "Old boy Linux ops" blog, please be sure to keep this source http://oldboy.blog.51cto.com/2561410/1656844
Develop shell script to check nginx real-combat sharing