开发程序实现nginx代理节点状态检查及WEB界面展示
实现功能介绍:
利用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=$1sed -i '/'$ip'/s/\(.*\);/\1 down;/g' "$conf_path/$conf_file" &> /dev/null[ $? -eq 0 ] && return 0 || return 1 } function proxy_addRs() {local ip=$1sed -i '/'$ip'/s/\(.*\)down;/\1;/g' "$conf_path/$conf_file" &> /dev/null[ $? -eq 0 ] && return 0 || return 1 } function proxy_getWebServerType() {local ip=$1local srvType=$(curl -I -s $ip|awk '/^Server/{print $2}'|cut -b1-5)if [ "$srvType" == "Apach" ];thenreturn 1elif [ "$srvType" == "nginx" ];thenreturn 0elsereturn 2fi } function proxy_getRsStatus() {local ip=$1if cat $conf_path/$conf_file|grep "$ip:$port\(.*\)down;" &>/dev/null;thenreturn 0elsereturn 1fi } function proxy_checkRsHealth() {local ip=$1if [ "$(nmap $ip -p $port|awk '/80/{print $2}')" == "open" ];thenreturn 0else return 1fi } function proxy_checkHtml() {if [ ! -f "$html_path/$html_file" ];thenproxy_htmlInitfi } function proxy_sendStatToHtml() {local rs=$1local string=$2if [ $string == "Good" ];thensed -i /'<td id='${rs}'_stat'/s#.*#' <td id='$rs'_stat align="center" bgcolor="green"> Good </td>'#g $html_path/$html_file &>/dev/nullelsesed -i /'<td id='${rs}'_stat'/s#.*#' <td id='$rs'_stat align="center" bgcolor="red"> Bad </td>'#g $html_path/$html_file &>/dev/nullfiproxy_getWebServerType $rscase $? in0)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[@]}doproxy_getWebServerType $rscase $? in0)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;;*);;esacif proxy_checkRsHealth $rs;thenecho ' <td id='${rs}_stat' align="center" bgcolor="green"> Good </td></tr>' >> $html_path/$html_fileelseecho ' <td id='${rs}_stat' align="center" bgcolor="red"> Bad </td></tr>' >> $html_path/$html_filefidoneecho ' </table></body> </html>' >> $html_path/$html_file } function proxy_checkHealthHandler() {proxy_checkHtmlfor rs in ${RS[@]}doif proxy_checkRsHealth $rs;thenif proxy_getRsStatus $rs;thenproxy_addRs $rsproxy_sendStatToHtml $rs "Good"fi elseif ! proxy_getRsStatus $rs;thenproxy_delRs $rsproxy_sendStatToHtml $rs "Bad"fi fidone } function main() {proxy_htmlInitwhile truedoproxy_checkHealthHandlersleep 1done } 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.191172.16.1.192 ) PORT=80 html_file="/var/html/www/index.html" declare -a RSTATUS ###main############## function checkrs(){local I=0for ((I=0;I<${#RS[*]};I++))doRSTATUS[$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_fileelif [ ${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_filefiif [ ${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_fileelif [ ${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_filefi } while true docheckrsoutput sleep 2 done
第三个实现脚本:刘磊 下载看讲解视频:http://down.51cto.com/data/1912248
#!/bin/bash rs_arr=( 10.0.0.1110.0.0.2210.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 $2rs=$?if [ $rs -eq 200 ]thennew_row $1 $2 up greenelsenew_row $1 $2 down redfi } 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++)); doauto_html $i ${rs_arr[$i]} done cat >> $file_location <<eof </table> eof sleep 2 > $file_location done } main
转载于:https://blog.51cto.com/oldboy/1589685
开发程序实现nginx代理节点状态检查及WEB界面展示相关推荐
- 【网络工程】如何本地调试微信公众号开发教程(Nginx代理方法)
目录 前言 目的 通过Nginx代理实现本地调试微信公众号 实现工具 实现步骤 1.启动本地前端项目 2.首先配置Nginx 3.填写app.conf内容,把本地前端项目与域名形成映射. 4.把app ...
- nginx后端节点健康检查
一.nginx健康检查的三种方式 1.ngx_http_proxy_module 模块和ngx_http_upstream_module模块(自带)官网地址:http://nginx.org/en/d ...
- Goby 内测版1.9.314|代理全局状态切换、报告支持插件数据显示、新增47种 UDP 协议支持
上周 Log4j 大杀器的爆出,让安全圈同仁们提前过年一样热闹非凡.Goby 团队反应迅敏,凭借已打通渗透全流程的产品优势和领先的技术攻坚能力,快速在红队版中针对 Log4j 做出了应急更新,是行业内 ...
- 小程序nginx做反向代理_NGINX作为节点或Angular应用程序的反向代理
小程序nginx做反向代理 A reverse proxy is a server that retrieves resources for clients from one or more upst ...
- nginx后端节点的健康检查
简介 本文主要介绍nginx后端节点的健康检查,在此之前我们先来介绍下nignx反向代理主要使用的模块. nginx原生模块介绍 我们在使用nginx做反向代理都会使用到以下两个模块: 1.ngx_h ...
- Nginx负载均衡监控节点状态
利用第三方插件监控(淘宝开发的Tengine) 模块:nginx_upstream_check_module 实现web界面 下载补丁包 wget https://codeload.github.co ...
- 服务器状态显示down,Eureka心跳健康检查机制和Spring boot admin 节点状态一直为DOWN的排查(忽略某一个节点的健康检查)...
运行阶段执行健康检查的目的是为了从Eureka服务器注册表中识别并删除不可访问的微服务,Eureka 服务器并不是向客户端发送心跳请求,而是反过来,Eureka 客户端将心跳发送到Eureka服务器, ...
- linux检查nginx 状态,Nginx配置检测服务状态的实现方法,
Nginx配置检测服务状态的实现方法, 1. 查看是否安装检查状态模块: [root@localhost ~]# nginx -V nginx version: nginx/1.12.2 built ...
- 海康摄像头web无插件3.2,vue开发,Nginx代理IIS服务器
在vue中实现海康摄像头播放,采用海康web无插件3.2开发包,采用Nginx代理IIS服务器实现: 1 摄像头要求,支持websocket 2 Nginx反向代理的结构 3 vue前端显示视频流代码 ...
最新文章
- matlab中clc,close,close all,clear,clear all作用区别
- Rsession: R sessions wrapping for Java
- linux下各种Raid介绍
- python多线程没用_python的多线程到底有没有用?
- 只适合小模型小训练集的交叉验证
- getHibernateTemplate()的用法 (转)
- eclipse中编辑properties文件无法看到中文
- python软件是免费的吗-7年程序员贡献出来的10大Python开源免费工具!
- 禁止解析PHP、限制user_agent、php相关配置
- mxonline实战3,编写首页及用户登录页面1
- JavaScript案例三:动态显示时间
- 03、动态代理--CGLib引入增强
- vs2013代码模板设置
- 【Pygame】在 Pygame 屏幕中添加文字
- 自从上了 Prometheus 监控,睡觉真香!
- (转)C# 解析 json
- 在.net中使用GAC
- Atitit callback回调机制大总结 目录 1. 回调机制=Call back,	1 1.1. 1.为什么需要Call back	2 1.2. 模块之间的调用,调用的方式分为几种:1)同步调
- iscsi对oracle发起程序,windows_2008+Oracle_11g_R2_配置完整版
- 计算机硬盘中有许多碎片,电脑磁盘碎片整理有什么用(需要经常清理吗)