通过脚本实现对web的健康检查

时间:2021-05-28 09:09:55

前面的文章中(https://www.cnblogs.com/zyxnhr/p/10707932.html),通过nginx的第三方模块实现对web端的一个监控,现在通过一个脚本实现对第三方的监控

脚本实现web的健康检查

1、编写脚本

[root@lb01 ~]# vim /script/nginx_check.sh

#!/bin/bash
#定义需要监控的节点
rs_arr=(
172.25.254.134
172.25.254.135
)
file_location=/usr/local/nginx/html/test.html
#定义函数web_result用于检测RS节点的web服务状态
function web_result {
rs=`curl -I -s $/index.html|awk 'NR==1{print $2}'`
return $rs
}
#定义函数new_row用于根据固定样式产生html表格框架
function new_row {
cat >> $file_location <<eof
<tr>
<td bgcolor="$4">$</td>
<td bgcolor="$4">$</td>
<td bgcolor="$4">$</td>
</tr>
eof
}
#定义函数auto_html通过并调用new_row冰箱其传递参数填充表格内容
function auto_html {
web_result $
rs=$?
if [ $rs -eq ]
then
new_row $ $ up green
else
new_row $ $ down red
fi
} while true
do
#产生头部部分
cat >> $file_location <<eof
<h4>The Status Of RS :</h4>
<table border="">
<tr>
<th>NO:</th>
<th>IP:</th>
<th>Status:</th>
</tr>
eof #循环产生每个节点的表格信息
for ((i=;i<${#rs_arr[*]};i++));do
auto_html $i ${rs_arr[$i]}
done #产生表格结尾部分
cat >> $file_location <<eof
</table>
eof sleep
>$file_location #每一次循环晴空一次html文件
done

2、修改配置文件

[root@lb01 ~]# vim /usr/local/nginx/conf/nginx.conf

worker_processes  ;
events {
worker_connections ;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout ;
upstream web_pools {
server 172.25.254.134: weight=;
server 172.25.254.135: weight=;
}
server {
listen ;
server_name www.lbtest.com;
location / {
root html;
index test.html index.htm;
# proxy_set_header Host $host;
# proxy_pass http://web_pools;
}
}
}

3、执行脚本

[root@lb01 ~]# sh /script/nginx_check.sh

4、 浏览器访问看结果

通过脚本实现对web的健康检查

看test.html

[root@lb01 ~]# watch -n  "cat /usr/local/nginx/html/test.html"
<h4>The Status Of RS :</h4>
<table border="">
<tr>
<th>NO:</th>
<th>IP:</th>
<th>Status:</th>
</tr>
<tr>
<td bgcolor="green"></td>
<td bgcolor="green">172.25.254.134</td>
<td bgcolor="green">up</td>
</tr>
<tr>
<td bgcolor="green"></td>
<td bgcolor="green">172.25.254.135</td>
<td bgcolor="green">up</td>
</tr>
</table>

5、关闭172.25.254.134的httpd

[root@web1 image]# systemctl stop httpd

通过脚本实现对web的健康检查

查看test.html

[root@lb01 ~]# watch -n  "cat /usr/local/nginx/html/test.html"
<h4>The Status Of RS :</h4>
<table border="">
<tr>
<th>NO:</th>
<th>IP:</th>
<th>Status:</th>
</tr>
<tr>
<td bgcolor="red"></td>
<td bgcolor="red">172.25.254.134</td>
<td bgcolor="red">down</td>
</tr>
<tr>
<td bgcolor="green"></td>
<td bgcolor="green">172.25.254.135</td>
<td bgcolor="green">up</td>
</tr>
</table>

6、 恢复

[root@web1 image]# systemctl start httpd

通过脚本实现对web的健康检查

查看test.html

[root@lb01 ~]# watch -n 1 "cat /usr/local/nginx/html/test.html"
<h4>The Status Of RS :</h4>
<table border="">
<tr>
<th>NO:</th>
<th>Status:</th>
</tr>
<tr>
<td bgcolor="green"></td>
<td bgcolor="green">172.25.254.134</td>
<td bgcolor="green">up</td>
</tr>
<tr>
<td bgcolor="green"></td>
<td bgcolor="green">172.25.254.135</td>
<td bgcolor="green">up</td>
</tr>
</table

已经实现了对web端的监控

7、添加监控节点

当需要添加节点时。只要在脚本的 rs_arr定义新的IP节点就可以了

rs_arr=(
172.25.254.134
172.25.254.135
NEW_IP
)

同时,在我们启动了脚本之后,监控到web端有变化,这时html已经发生变化,但是浏览器仍然停留在上一个页面,需要刷新才能跟新页面,这里用的是Google浏览器,有同一个自动刷新插件,设置1秒刷新一次,就保证了html的实时性!


参考:老男孩教育视频公开课https://www.bilibili.com/video/av25869969/?p=33