开发shell脚本检查Nginx实战分享
2015-05-31 14:25
561 查看
开发shell脚本检查Nginx实战分享
一、本脚本实现功能:
1、自动检查Nginx下面的代理节点是否正常
2、通过页面显示状态,有问题的节点给出页面报警及声音报警。
3、增加新节点,页面自动载入新节点,无需修改程序。
二、守护检查脚本
# cd /server/scripts/ [root@lb01 scripts]# vi nginx_check.sh #!/bin/bash # oldboy training 21 zhangyao # Defined variables NginxDir=/application/nginx ExtraPath=$NginxDir/conf/extra ScriptDir=/server/scripts StatusLog=$ScriptDir/status.log StatusHtml=$NginxDir/html/status/status.html StatusHtmlOri=$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 Functions function 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 Functions function 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 Functions function table(){ #将表格的一行语句累加后一次性插入html文件 char="<tr bgcolor="$1"><th>$2</th><th>$3</th><th>$4</th><th>$5</th></tr>]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 /image/ { 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三、相关文件
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>
ok
五、效果
正常状态效果:
报警效果节点故障条目变红,并且有声音报警(也可以实现邮件、短信报警)
谢谢大家!本文内容来自21期学员张耀!
阅读更多相关文章推荐
- 开发shell脚本检查Nginx实战分享
- Linux系统shell脚本编程——生产实战案例(批量检查在线IP与开放端口)
- shell脚本检查统计nginx访问日志access.log
- shell脚本:nginx反向代理节点状态检查
- 使用shell脚本对Nginx日志进行切分
- linux实战(三)----执行shell脚本----详细步骤讲解
- Shell脚本检查IP格式及mysql操作实例
- iOS开发进阶 - 使用shell脚本自动打包上传到fir.im上
- 自己常用的一些shell脚本分享
- 运维经验分享(六)-- 深究crontab不能正确执行Shell脚本的问题(二)
- Nginx和PHP-FPM的启动、重启、停止脚本分享
- Shell脚本模拟多线程功能分享
- 【课程分享】基于plusgantt的项目管理系统实战开发(Spring3+JDBC+RMI的架构、自己定义工作流)
- nginx中shell脚本封锁采集IP地址
- shell脚本实现同时多台远程主机执行命令的代码分享
- 开启、停止 nginx 的shell脚本
- 分享一个shell脚本的坑:grep匹配+wc取值 在脚本执行后的结果与手动执行结果不一致
- 一个监控Squid运行进程数并自动重启的简洁Shell脚本分享
- 检查mysql主从同步结构中的从数据库服务器的状态-脚本shell
- 分享脚本,同一个问题,php,python,shell的写法