设为首页 收藏本站
查看: 999|回复: 0

[经验分享] 开发shell脚本检查Nginx实战分享

[复制链接]
累计签到:85 天
连续签到:1 天
发表于 2019-2-13 00:16:40 | 显示全部楼层 |阅读模式
开发shell脚本检查Nginx实战分享
一、本脚本实现功能:
1、自动检查Nginx下面的代理节点是否正常
2、通过页面显示状态,有问题的节点给出页面报警及声音报警。
3、增加新节点,页面自动载入新节点,无需修改程序。

二、守护检查脚本
[root@lb01 extra]# cd /server/scripts/[root@lb01 scripts]# vi nginx_check.sh#!/bin/bash# oldboy training 21 zhangyao# Defined variablesNginxDir=/application/nginxExtraPath=$NginxDir/conf/extraScriptDir=/server/scriptsStatusLog=$ScriptDir/status.logStatusHtml=$NginxDir/html/status/status.htmlStatusHtmlOri=$NginxDir/html/status/status.html.ori# Judge some files [ -d $NginxDir ] ||exit 1[ -d $ScriptDir ] ||mkdir -p $ScriptDir[ -f $StatusLog ] ||touch $StatusLog[ -f $StatusHtml ] ||touch $StatusHtml# Defined Check URL Functionsfunction check_url(){        status=`curl -s $2/check.html`        if [ "$status" == "OK" ]          then             echo "$1 $2 up" >>$StatusLog        else             echo "$1 $2 down" >>$StatusLog        fi}# Defined List URL and Check Functionsfunction check(){        >$StatusLog            cd $ExtraPath          for file in `ls`  #首先遍历extra目录下的所有文件,然后遍历每个文件的IP行,将参数传给check_url          do            url=(`awk -F "[ ]+" '/server/ {print $3}' $file`)            for i in ${url
  • }              do                check_url $file $i            done        done}# Defined Html Table Format Functionsfunction table(){  #将表格的一行语句累加后一次性插入html文件        char="<tr bgcolor="$1"><th>$2</th><th>$3</th><th>$4</th><th>$5</th></tr>"        sum="$sum""$char"}function html(){        Index=1  #表格最左侧的一列,初始值为1        flag=0        sum=""   #行语句初始值null        /bin/cp $StatusHtmlOri $StatusHtml  #将status html文件初始化        while read line  #一行行读入$StatusLog文件,格式为dynamic_pools 10.0.0.6:80 up           do             array_line=($line)             if [ "${array_line[2]}" == "up" ]               then                 table "#90EE90" $Index ${array_line
  • } #将颜色参数、index值及其他参数传给table函数             else                    table "#FF0000" $Index ${array_line
  • }                 ((flag++)) #down情况下flag会计数             fi             ((Index++))        done<$StatusLog        [ $flag -eq 0 ] ||\  #如果flag不为0,肯定有down机器,增加一个语音报警的功能,仿照zabbix        sum=$sum"<audio id="clickSound" autoplay="autoplay"><source src="warning.mp3" type="audio/mpeg"></audio>"        sed -i "/C0C0C0/a $sum" $StatusHtml  #将sum语句插入html文件}# Defined Main Functionsfunction main(){        while true          do            check            html            sleep 5        done}main

    三、相关文件
    status.html.ori
    <!doctype html><html><head><meta charset="utf-8"><meta http-equiv="refresh" content="5"><title>Nginx http upstream check status</title></head><body><div align="center">  <table width="1171" height="682" border="1">    <tr>      <td background="20150516194115.jpg" ><table align="center" style="background-color:white" cellspacing="0" cellpadding="3" border="1">  <tr bgcolor="#C0C0C0"><th>Index</th><th>Upstream</th><th>Name</th><th>Status</th></tr></td>    </tr>  </table></div></body></html>

    status.html
    <!doctype html><html><head><meta charset="utf-8"><meta http-equiv="refresh" content="5"><title>Nginx http upstream check status</title></head><body><div align="center">  <table width="1171" height="682" border="1">    <tr>      <td background="20150516194115.jpg" ><table align="center" style="background-color:white" cellspacing="0" cellpadding="3" border="1">  <tr bgcolor="#C0C0C0"><th>Index</th><th>Upstream</th><th>Name</th><th>Status</th></tr></td><tr bgcolor=#90EE90><th>1</th><th>dynamic_pools</th><th>10.0.0.6:80</th><th>up</th></tr><tr bgcolor=#90EE90><th>2</th><th>static_pools</th><th>10.0.0.5:80</th><th>up</th></tr><tr bgcolor=#90EE90><th>3</th><th>static_pools</th><th>10.0.0.6:80</th><th>up</th></tr>    </tr>  </table></div></body></html>

    四、nginx.conf站点配置
    worker_processes  1;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    sendfile        on;    keepalive_timeout  65;    include extra/static_pools;    include extra/dynamic_pools;    server {        listen       80;        server_name   www.etiantian.org;        location / {        if ($http_user_agent ~* "MSIE")          {             rewrite ^/ http://10.0.0.6/ie.html;            }            root   html;            index  index.html index.htm;        }       location /p_w_picpath/ {         proxy_pass  http://static_pools;        include proxy.conf;       }       location /dynamic/ {        proxy_pass  http://dynamic_pools;        include proxy.conf;       }    }}extra/dynamic_pools包含文件upstream dynamic_pools {    server 10.0.0.6:80 weight=5;}extra/static_pools包含文件upstream static_pools {    server 10.0.0.5:80 weight=5;    server 10.0.0.6:80 weight=5;}

    站点下健康检查文件:check.html
    ok
    五、效果
    正常状态效果:
    报警效果节点故障条目变红,并且有声音报警(也可以实现邮件、短信报警)


  • 运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
    2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
    3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
    4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
    5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
    6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
    7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
    8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

    所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-672238-1-1.html 上篇帖子: 你所不知道的TIME_WAIT和CLOSE_WAIT 下篇帖子: IT从业者的绘图指南(PROCESSON版)
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    扫码加入运维网微信交流群X

    扫码加入运维网微信交流群

    扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

    扫描微信二维码查看详情

    客服E-mail:kefu@iyunv.com 客服QQ:1061981298


    QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


    提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


    本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



    合作伙伴: 青云cloud

    快速回复 返回顶部 返回列表