開發shell指令碼檢查Nginx實戰分享

來源:互聯網
上載者:User

標籤:shell   老男孩教育   企業面試   

開發shell指令碼檢查Nginx實戰分享


一、本指令碼實現功能:

1、自動檢查Nginx下面的代理節點是否正常

2、通過頁面顯示狀態,有問題的節點給出頁面警示及聲音警示。

3、增加新節點,頁面自動載入新節點,無需修改程式。


二、守護檢查指令碼

[[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     echo "$1 $2 down" >>$StatusLogfi}# Defined List URL and Check Functionsfunction check(){>$StatusLog    cd $ExtraPath  for file in `ls`  #首先遍曆extra目錄下的所有檔案,然後遍曆每個檔案的IP行,將參數傳給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="$1"><th>$2</th><th>$3</th><th>$4</th><th>$5</th></tr>"sum="$sum""$char"}function html(){Index=1  #表格最左側的一列,初始值為1flag=0sum=""   #行語句初始值null/bin/cp $StatusHtmlOri $StatusHtml  #將status html檔案初始化while read line  #一行行讀入$StatusLog檔案,格式為dynamic_pools 10.0.0.6:80 up   do     array_line=($line)     if [ "${array_line[2]}" == "up" ]       then         table "#90EE90" $Index ${array_line[*]} #將顏色參數、index值及其他參數傳給table函數     else    table "#FF0000" $Index ${array_line[*]} ((flag++)) #down情況下flag會計數     fi     ((Index++))done<$StatusLog[ $flag -eq 0 ] ||\  #如果flag不為0,肯定有down機器,增加一個語音警示的功能,仿照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

三、相關檔案

status.html.ori

<!doctype html><html><head><meta charset="utf-8"><meta http-equiv="refresh" content="5"><title>Nginx http upstream check status</title></head><body><div align="center">  <table width="1171" height="682" border="1">    <tr>      <td background="20150516194115.jpg" ><table align="center" style="background-color:white" cellspacing="0" cellpadding="3" border="1">  <tr bgcolor="#C0C0C0"><th>Index</th><th>Upstream</th><th>Name</th><th>Status</th></tr></td>    </tr>  </table></div></body></html>

status.html

<!doctype html><html><head><meta charset="utf-8"><meta http-equiv="refresh" content="5"><title>Nginx http upstream check status</title></head><body><div align="center">  <table width="1171" height="682" border="1">    <tr>      <td background="20150516194115.jpg" ><table align="center" style="background-color:white" cellspacing="0" cellpadding="3" border="1">  <tr bgcolor="#C0C0C0"><th>Index</th><th>Upstream</th><th>Name</th><th>Status</th></tr></td><tr bgcolor=#90EE90><th>1</th><th>dynamic_pools</th><th>10.0.0.6:80</th><th>up</th></tr><tr bgcolor=#90EE90><th>2</th><th>static_pools</th><th>10.0.0.5:80</th><th>up</th></tr><tr bgcolor=#90EE90><th>3</th><th>static_pools</th><th>10.0.0.6:80</th><th>up</th></tr>    </tr>  </table></div></body></html>

四、nginx.conf網站配置

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;       }    }}extra/dynamic_pools包含檔案upstream dynamic_pools {    server 10.0.0.6:80 weight=5;}extra/static_pools包含檔案upstream static_pools {    server 10.0.0.5:80 weight=5;    server 10.0.0.6:80 weight=5;}

網站下健全狀態檢查檔案:check.html

ok

五、效果

正常狀態效果:

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/6D/B8/wKioL1VqrMqQQ_R-AAUXeGAKVhU542.jpg" style="float:none;" title="老男孩IT實效教育0016321.jpg" alt="wKioL1VqrMqQQ_R-AAUXeGAKVhU542.jpg" />警示效果節點故障條目變紅,並且有聲音警示(也可以實現郵件、簡訊警示)

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/6D/BD/wKiom1VqqzShouizAAUSDTZkFbk159.jpg" style="float:none;" title="QQ圖片20150531143033.png" alt="wKiom1VqqzShouizAAUSDTZkFbk159.jpg" />

謝謝大家!本文內容來自21期學員張耀!

本文出自 “老男孩linux營運” 部落格,請務必保留此出處http://oldboy.blog.51cto.com/2561410/1656844

開發shell指令碼檢查Nginx實戰分享

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.