zoukankan      html  css  js  c++  java
  • 通过脚本实现对web的健康检查

    前面的文章中(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 $1/index.html|awk 'NR==1{print $2}'`
      return $rs
    }
    #定义函数new_row用于根据固定样式产生html表格框架
    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
    }
    #定义函数auto_html通过并调用new_row冰箱其传递参数填充表格内容
    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
    }
    
    while true
    do
    #产生头部部分
    cat >> $file_location <<eof
    <h4>The Status Of RS :</h4>
    <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 #每一次循环晴空一次html文件
    done

    2、修改配置文件

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

    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
    upstream  web_pools {
            server 172.25.254.134:80 weight=5;
            server 172.25.254.135:80 weight=5;
    }
        server {
            listen       80;
            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、 浏览器访问看结果

    看test.html

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

    查看test.html

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

    6、 恢复

    [root@web1 image]# systemctl start httpd

    查看test.html

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

  • 相关阅读:
    IIS7运行.NET Framework 4 报500错误
    祝大家新年快乐,兔年行大运
    生成高清缩略图; 添加图片、文字水印; 图片、文字水印透明
    NHibernate中使用Guid作为主键、项目中NHibernate与Log4net共存
    使用split进行大数据分割时内存溢出解决方案
    about server.MapPath
    Lucene 如何实现高性能 GroupBy <一>
    理解委托(delegate)及为什么要使用委托
    观亚运会开幕式有感
    c#中的new、override
  • 原文地址:https://www.cnblogs.com/zyxnhr/p/10727577.html
Copyright © 2011-2022 走看看