实现功能介绍:
利用shell程序及http服务巧妙的实现监控nginx代理节点状态检查,然后通过web界面实时刷新显示结果,是不是有些吃惊这样高大上的程序?那就赶紧看看吧!
to用人单位:此课程可以体现学生shell编程功力,以及对nginx proxy企业实战及驾驭的能力。
不同的同学的三个实现方法分享,各位看官,你们看看哪个同学的更好,请评论。
第一个实现脚本:youjinyi 视频下载看地址:http://down.51cto.com/data/1914201
#!/bin/sh port=80 conf_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=80 html_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 true do checkrs output sleep 2 done
第三个实现脚本:刘磊 下载看讲解视频:http://down.51cto.com/data/1914011
#!/bin/bash rs_arr=( 10.0.0.11 10.0.0.22 10.0.0.33 ) file_location=/var/html/test.html function 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 true do cat >> $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> eof for ((i=0;i<${#rs_arr[*]};i++)); do auto_html $i ${rs_arr[$i]} done cat >> $file_location <<eof </table> eof sleep 2 > $file_location done } main
本文来自老男孩教育19期学生课后作业分享。
时间: 2024-12-29 23:06:30