本文共 4501 字,大约阅读时间需要 15 分钟。
实现功能介绍:
利用shell程序及http服务巧妙的实现监控nginx代理节点状态检查,然后通过web界面实时刷新显示结果,是不是有些吃惊这样高大上的程序?那就赶紧看看吧!to用人单位:此课程可以体现学生shell编程功力,以及对nginx proxy企业实战及驾驭的能力。
不同的同学的三个实现方法分享,各位看官,你们看看哪个同学的更好,请评论。
第一个实现脚本:youjinyi 视频下载看地址:#!/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 /'Good '#g $html_path/$html_file &>/dev/null else sed -i /' Bad '#g $html_path/$html_file &>/dev/null fi proxy_getWebServerType $rs case $? in 0) sed -i /' Nginx '#g $html_path/$html_file &>/dev/null ;; 1) sed -i /' Apache '#g $html_path/$html_file &>/dev/null ;; 2) sed -i /' Unknow '#g $html_path/$html_file &>/dev/null ;; *) ;; esac}function proxy_htmlInit(){ echo ' Cluster Web Service Health Check
Real Server Type Real Server Host Real Server Status ' > $html_path/$html_file for rs in ${RS[@]} do proxy_getWebServerType $rs case $? in 0) echo ' Nginx '$rs' ' >> $html_path/$html_file ;; 1) echo ' Apache '$rs'' >> $html_path/$html_file ;; 2) echo ' Unknow '$rs'' >> $html_path/$html_file ;; *) ;; esac if proxy_checkRsHealth $rs;then echo ' Good ' >> $html_path/$html_file else echo ' Bad ' >> $html_path/$html_file fi done echo ' ' >> $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
第二个同学实现脚本:王硕 下载看讲解视频地址:
#!/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/.*/Down!<\/font><\/td>/g' $html_file elif [ ${RSTATUS[0]} -eq 1 ];then #echo "${RS[$i]} is OK!" sed -i '22 s/.*/ OK!<\/font><\/td>/g' $html_file fi if [ ${RSTATUS[1]} -eq 0 ];then #echo "${RS[$i]} is down!" sed -i '28 s/.*/ Down!<\/font><\/td>/g' $html_file elif [ ${RSTATUS[1]} -eq 1 ];then #echo "${RS[$i]} is OK!" sed -i '28 s/.*/ OK!<\/font><\/td>/g' $html_file fi}while truedo checkrs outputsleep 2done
第三个实现脚本:刘磊 下载看讲解视频:
#!/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 <$1 $2 $3eof}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 < he Status Of RS :
NO:IP:Status:eoffor ((i=0;i<${#rs_arr[*]};i++)); do auto_html $i ${rs_arr[$i]}donecat >> $file_location <
eofsleep 2> $file_locationdone}main
本文来自老男孩教育19期学生课后作业分享。
转载地址:http://wywda.baihongyu.com/