使用shell脚本监控网站运行状态
admin
2023-03-20 02:01:29
0

前言:好久没有写博客了,上来把之前写的博客几乎全都清理掉了,想写的时候写上一些,蛮不错。


shell监控网站/tomcat状态,依靠返回状态码来进行判断,返回200,302认为状态是正常的,否则认为tomcat/nginx/LB/Haproxy/apache挂掉了,脚本实现如下:

1. 创建一个站点文件夹,吧需要监控的地址都写到http_site文件里面

vim http_site


### Nginx site begin ###

http://192.168.129.86:38020

http://192.168.129.86:38021

### Nginx site end ###

### LB site begin ###

http://192.168.2.30:38020

http://192.168.2.30:38024/38025task

### LB site end ###

### Web site begin ###

http://192.168.129.91:8030

http://192.168.129.93:8030

### Web site end ###

### Task site begin ###

http://192.168.129.95:8032/38023task

http://192.168.129.95:8033/38027task

### Task site end ###

### Mobile site  begin ###

http://192.168.129.92:8030

http://192.168.129.92:8040

### Mobile site  end ###


2. 编写shell脚本实现监控功能,使用curl访问网站,过滤出返回的状态码当做判断条件,如有返回状态码非200/302则发送邮件报警

vim check_site.sh


#!/bin/bash

mysite=/root/script/check_http/http_site

check_status=/root/script/check_http/temp_status

historyfile=/root/script/check_http/history/`date  +%Y-%m-%d`/`date +%T`

failurefile=/root/script/check_http/history/`date  +%Y-%m-%d`/`date +%T`_failure

mkdir /root/script/check_http/history/`date  +%Y-%m-%d` &>/dev/null

for  site in `grep -v  -E "^#|^$"  $mysite`

do

       curl -s  -I  --connect-timeout 3 -m 5  $site  | grep  "HTTP/1.1"  | awk '{print $2}'  >  $check_status

       status=`cat $check_status`

       if [[ $status -eq 200 ]] || [[ $status -eq 302 ]]

       then

               echo  "###########################"             >>$historyfile

               echo  "http_site $site  Access Successful"      >>$historyfile

       else

               echo  "###########################"             >>$historyfile

               echo  "http_site $site  Access Failure"         >>$historyfile

       fi

done

grep "Access Failure" $historyfile  &>/dev/null

if      [ $? -eq 0 ]

then

       echo -e "\n\nThe following tomcat is not started !!!\n"  >> $failurefile

       echo -e "Please check the services !!!\n"  >> $failurefile

       echo -e "#############################################\n"  >> $failurefile

       grep "Access Failure" $historyfile  >> $failurefile

       echo -e "\n#############################################" >> $failurefile

       mail -s "SFA_Liby_Tomcat_Check !!!"  baiyongjie@winchannel.net   misterbyj@163.com tangzhiyu@winchannel.net   <  $failurefile

fi

3. 配置报警邮箱


vim /etc/mail.rc

set hold

set append

set ask

set crt

set dot

set keep

set emptybox

set indentprefix="> "

set quote

set sendcharsets=iso-8859-1,utf-8

set showname

set showto

set newmail=nopoll

set autocollapse

ignore received in-reply-to message-id references

ignore mime-version content-transfer-encoding

fwdretain subject date from to

set bsdcompat

set from=15600970600@163.com

set smtp=smtp.163.com

set smtp-auth-user=15600970600@163.com smtp-auth-password=Password smtp-auth=login

4.添加计划任务,每5分钟运行一次

crontab -e

*/5 * * * * /bin/bash  /root/script/check_http/check_site.sh


5. 测试脚本,写好以后已经运行好几天了,效果还不错,分享给大家

为了验证效果,当时停了几个tomcat,6月14号23:12分停掉的,停掉后运行脚本检测到有tomcat没有运行,会生成 _failure文件记录,并发出邮件,达到报警效果

使用shell脚本监控网站运行状态

使用shell脚本监控网站运行状态


相关内容

热门资讯

五一票房冠军是惊悚片,“下沉市... 【文/新潮观鱼】今年“五一”档,有一个有意思的现象:一部看起来没有“爆款相”,演员和导演都没有很大票...
“几轮博弈后,特朗普发现:中国... 【文/观察者网 王一】当地时间5月9日,英国《金融时报》发长文分析称,在美国与中国围绕贸易、科技、地...
拒不付款,还对监控嚣张“比耶” 近日,广东梅州丰顺县一名男子加油逃单后,同行人对监控嚣张“比耶”。今天(5月10日),南都N视频记者...
演员张静初,面临被起诉 近日,多位在演员张静初直播间购买过“优思益”产品的网友表示,目前仍未拿到退款,陷入退货无门的境地。5...
90分钟16条,特朗普的社媒账... 周六下午,特朗普将他的Truth Social账号变成了“AI垃圾工厂”,在短短90分钟内发布了16...
我,22岁,确诊为“妈” 又是一年母亲节。每年今天,大家都试图再次看见母亲,讨论她们的付出与局限,以“成为母亲”为界限,划分她...
重达11000克拉!缅甸发现“... 【环球网报道】美国哥伦比亚广播公司、英国天空新闻网等媒体援引缅甸官方媒体8日消息报道称,缅甸发现一块...
玻璃缝隙漏水怎么处理 1、检查窗户玻璃附近墙体的防水层,重新做防水层。2、查找原先的密封物,检查门窗与墙体的密封度,如果密...
热水器侧面的缝隙漏水可以修好吗 问题:热水器侧面的缝隙漏水可以修好吗回答:如果热水器侧面出现缝隙漏水的情况基本上都是可以修好的。首先...
洗面盆和台面缝隙漏水怎么办 洗菜盆漏水的修补方法1、如果是洗菜盆和台面衔接松动:如果是因为洗菜盆安装时没安装好,与台面缝隙过大,...