日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Nginx >内容正文

Nginx

开发shell脚本检查Nginx实战分享

發(fā)布時(shí)間:2025/3/21 Nginx 62 豆豆
生活随笔 收集整理的這篇文章主要介紹了 开发shell脚本检查Nginx实战分享 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

開發(fā)shell腳本檢查Nginx實(shí)戰(zhàn)分享

點(diǎn)這里查看視頻講解

一、本腳本實(shí)現(xiàn)功能:

1、自動(dòng)檢查Nginx下面的代理節(jié)點(diǎn)是否正常

2、通過頁面顯示狀態(tài),有問題的節(jié)點(diǎn)給出頁面報(bào)警及聲音報(bào)警。

3、增加新節(jié)點(diǎn),頁面自動(dòng)載入新節(jié)點(diǎn),無需修改程序。


二、守護(hù)檢查腳本

[root@lb01?extra]#?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"?]thenecho?"$1?$2?up"?>>$StatusLogelseecho?"$1?$2?down"?>>$StatusLogfi } #?Defined?List?URL?and?Check?Functions function?check(){>$StatusLog????cd?$ExtraPath??for?file?in?`ls`??#首先遍歷extra目錄下的所有文件,然后遍歷每個(gè)文件的IP行,將參數(shù)傳給check_urldourl=(`awk?-F?"[?]+"?'/server/?{print?$3}'?$file`)for?i?in?${url[*]}docheck_url?$file?$idonedone } #?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>"sum="$sum""$char" } function?html(){Index=1??#表格最左側(cè)的一列,初始值為1flag=0sum=""???#行語句初始值null/bin/cp?$StatusHtmlOri?$StatusHtml??#將status?html文件初始化while?read?line??#一行行讀入$StatusLog文件,格式為dynamic_pools?10.0.0.6:80?updoarray_line=($line)if?[?"${array_line[2]}"?==?"up"?]thentable?"#90EE90"?$Index?${array_line[*]}?#將顏色參數(shù)、index值及其他參數(shù)傳給table函數(shù)elsetable?"#FF0000"?$Index?${array_line[*]}((flag++))?#down情況下flag會(huì)計(jì)數(shù)fi((Index++))done<$StatusLog[?$flag?-eq?0?]?||\??#如果flag不為0,肯定有down機(jī)器,增加一個(gè)語音報(bào)警的功能,仿照zabbixsum=$sum"<audio?id="clickSound"?autoplay="autoplay"><source?src="warning.mp3"?type="audio/mpeg"></audio>"sed?-i?"/C0C0C0/a?$sum"?$StatusHtml??#將sum語句插入html文件 } #?Defined?Main?Functions function?main(){while?truedocheckhtmlsleep?5done } main

三、相關(guān)文件

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站點(diǎn)配置

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; }

站點(diǎn)下健康檢查文件:check.html

ok

五、效果

正常狀態(tài)效果:

報(bào)警效果節(jié)點(diǎn)故障條目變紅,并且有聲音報(bào)警(也可以實(shí)現(xiàn)郵件、短信報(bào)警)

謝謝大家!本文內(nèi)容來自21期學(xué)員張耀!

總結(jié)

以上是生活随笔為你收集整理的开发shell脚本检查Nginx实战分享的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。