開發程式實現nginx代理節點狀態檢查及WEB介面展示

來源:互聯網
上載者:User

標籤:shell   nginx   linux營運   老男孩教育   

實現功能介紹:

利用shell程式及http服務巧妙的實現監控nginx代理節點狀態檢查,然後通過web介面即時重新整理顯示結果,是不是有些吃驚這樣高大上的程式?那就趕緊看看吧!
to用人單位:此課程可以體現學生shell編程功力,以及對nginx proxy企業實戰及駕馭的能力。

 

不同的同學的三個實現方法分享,各位看官,你們看看哪個同學的更好,請評論。


第一個實現指令碼:youjinyi 視頻下載看地址:http://down.51cto.com/data/1914201

#!/bin/shport=80conf_path="/application/nginx/conf"conf_file="nginx.conf"html_path="/application/nginx/html"html_file="monitor.html"RS=($(grep WEB-A "$conf_path/$conf_file"|grep -v ‘#‘|awk -F"[ :]+" ‘{print $2}‘))function proxy_delRs(){   local ip=$1 sed -i ‘/‘$ip‘/s/\(.*\);/\1 down;/g‘ "$conf_path/$conf_file" &> /dev/null [ $? -eq 0 ] && return 0 || return 1}function proxy_addRs(){ local ip=$1 sed -i ‘/‘$ip‘/s/\(.*\)down;/\1;/g‘ "$conf_path/$conf_file" &> /dev/null [ $? -eq 0 ] && return 0 || return 1}function proxy_getWebServerType(){ local ip=$1 local srvType=$(curl -I -s $ip|awk ‘/^Server/{print $2}‘|cut -b1-5) if [ "$srvType" == "Apach" ];then  return 1 elif [ "$srvType" == "nginx" ];then  return 0 else  return 2 fi}function proxy_getRsStatus(){ local ip=$1 if cat $conf_path/$conf_file|grep "$ip:$port\(.*\)down;" &>/dev/null;then  return 0 else  return 1 fi}function proxy_checkRsHealth(){ local ip=$1 if [ "$(nmap $ip -p $port|awk ‘/80/{print $2}‘)" == "open" ];then  return 0 else   return 1 fi}function proxy_checkHtml(){  if [ ! -f "$html_path/$html_file" ];then  proxy_htmlInit fi}function proxy_sendStatToHtml(){ local rs=$1 local string=$2 if [ $string == "Good" ];then  sed -i /‘<td id=‘${rs}‘_stat‘/s#.*#‘    <td id=‘$rs‘_stat align="center" bgcolor="green"> Good </td>‘#g $html_path/$html_file  &>/dev/null else  sed -i /‘<td id=‘${rs}‘_stat‘/s#.*#‘    <td id=‘$rs‘_stat align="center" bgcolor="red"> Bad </td>‘#g $html_path/$html_file &>/dev/null fi  proxy_getWebServerType $rs case $? in 0)  sed -i /‘<td id=‘${rs}‘_type‘/s#.*#‘    <td id=‘${rs}‘_type align="center"> Nginx </td>‘#g $html_path/$html_file  &>/dev/null ;; 1)  sed -i /‘<td id=‘${rs}‘_type‘/s#.*#‘    <td id=‘${rs}‘_type align="center"> Apache </td>‘#g $html_path/$html_file  &>/dev/null ;; 2)  sed -i /‘<td id=‘${rs}‘_type‘/s#.*#‘    <td id=‘${rs}‘_type align="center"> Unknow </td>‘#g $html_path/$html_file  &>/dev/null ;; *) ;; esac}function proxy_htmlInit(){ echo  ‘<html> <head><h4 align="center"><font size="14"  style="color:grey"> Cluster Web Service Health Check</front></h4>  <meta http-equiv="refresh" content="1">   <style>   tr,td{font-size:28px;   }   </style>  </head> <body>  <table border=1 align="center">   <tr size=20>    <td bgcolor="Cyan" align="center"> Real Server Type </td>    <td bgcolor="Cyan" > Real Server Host </td>    <td bgcolor="Cyan" align="center"> Real Server Status </td>   </tr>‘ > $html_path/$html_file  for rs in ${RS[@]} do proxy_getWebServerType $rs case $? in 0) echo ‘   <tr size=20>      <td id=‘${rs}_type‘ align="center"> Nginx </td>      <td align="center"> ‘$rs‘ </td>‘ >> $html_path/$html_file ;; 1) echo ‘   <tr size=20>      <td id=‘${rs}_type‘ align="center"> Apache </td>      <td align="center"> ‘$rs‘</td>‘ >> $html_path/$html_file ;; 2) echo ‘   <tr size=20>      <td id=‘${rs}_type‘ align="center"> Unknow </td>      <td align="center"> ‘$rs‘</td>‘ >> $html_path/$html_file ;; *) ;; esac  if proxy_checkRsHealth $rs;then echo ‘   <td id=‘${rs}_stat‘ align="center" bgcolor="green"> Good </td>   </tr>‘ >> $html_path/$html_file else echo ‘   <td id=‘${rs}_stat‘ align="center" bgcolor="red"> Bad </td>   </tr>‘ >> $html_path/$html_file fi done echo ‘  </table> </body></html>‘ >> $html_path/$html_file}function proxy_checkHealthHandler(){ proxy_checkHtml for rs in ${RS[@]} do  if proxy_checkRsHealth $rs;then   if proxy_getRsStatus $rs;then    proxy_addRs $rs    proxy_sendStatToHtml $rs "Good"   fi   else   if ! proxy_getRsStatus $rs;then    proxy_delRs $rs    proxy_sendStatToHtml $rs "Bad"   fi   fi done}function main(){ proxy_htmlInit while true do  proxy_checkHealthHandler  sleep 1 done}main

第二個同學實現指令碼:王碩 下載看講解視頻地址:http://down.51cto.com/data/1914011

#!/bin/bash#Author: Stanley Wang#mail: #Version: 1.0#Description: This is a script for nginx proxy health check.####def vars##########RS=(  172.16.1.191  172.16.1.192)PORT=80html_file="/var/html/www/index.html"declare -a RSTATUS###main##############function checkrs(){  local I=0  for ((I=0;I<${#RS[*]};I++))  do    RSTATUS[$I]=`nmap ${RS[$I]} -p $PORT|grep "open"|wc -l`  done}function output(){    if [ ${RSTATUS[0]} -eq 0 ];then      #echo "${RS[$i]} is down!"      sed -i ‘22 s/.*/<td align="center" bgcolor="red"><font size="15">Down!<\/font><\/td>/g‘ $html_file    elif [ ${RSTATUS[0]} -eq 1 ];then      #echo "${RS[$i]} is OK!"      sed -i ‘22 s/.*/<td align="center" bgcolor="green"><font size="15">OK!<\/font><\/td>/g‘ $html_file    fi    if [ ${RSTATUS[1]} -eq 0 ];then      #echo "${RS[$i]} is down!"      sed -i ‘28 s/.*/<td align="center" bgcolor="red"><font size="15">Down!<\/font><\/td>/g‘ $html_file    elif [ ${RSTATUS[1]} -eq 1 ];then      #echo "${RS[$i]} is OK!"      sed -i ‘28 s/.*/<td align="center" bgcolor="green"><font size="15">OK!<\/font><\/td>/g‘ $html_file    fi}while truedo  checkrs  outputsleep 2done

第三個實現指令碼:劉磊 下載看講解視頻:http://down.51cto.com/data/1914011

#!/bin/bashrs_arr=(     10.0.0.11    10.0.0.22    10.0.0.33    )file_location=/var/html/test.htmlfunction web_result {    rs=`curl -I -s $1|awk ‘NR==1{print $2}‘`    return $rs}function new_row {cat >> $file_location <<eof<tr><td bgcolor="$4">$1</td><td bgcolor="$4">$2</td><td bgcolor="$4">$3</td></tr>eof}function auto_html {    web_result $2    rs=$?    if [ $rs -eq 200 ]    then new_row $1 $2 up green    else new_row $1 $2 down red    fi}main(){while truedocat >> $file_location <<eof<h4>he Status Of RS :</h4><meta http-equiv="refresh" content="1"><table border="1"><tr> <th>NO:</th><th>IP:</th><th>Status:</th></tr>eoffor ((i=0;i<${#rs_arr[*]};i++)); do    auto_html $i ${rs_arr[$i]}donecat >> $file_location <<eof</table>eofsleep 2> $file_locationdone}main

 本文來自老男孩教育19期學生課後作業分享。

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

開發程式實現nginx代理節點狀態檢查及WEB介面展示

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.