使用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脚本监控网站运行状态


相关内容

热门资讯

AI开源公开信签署企业增至50... IT之家 7 月 26 日消息,英伟达、微软、IBM、Meta 等 25 家美国科技企业本周(7 月...
南阳快手短视频代运营企业的服务... 在短视频平台快速发展的背景下,企业通过快手等渠道进行内容运营已成为常见的市场动作。对于希望借助短视频...
普通人该如何跟上科技发展的步伐 这个被称作科技的词汇, 听起来既有宏大之感, 又颇为遥远, 好似仅仅归属于在实验室里专心致志钻研的科...
原创 王... 菲尔兹奖名单一出,邓煜和王虹两位华人数学家共同获奖,本应是一件令人振奋的喜讯。然而,王虹那段仅3分1...
《万物》杂志首个阅读空间亮相正... 26日,由国内知名的青少年家庭科普期刊《万物》杂志与广州正佳自然科学探索中心联合打造的阅读互动空间—...
从康复机器人到脑机接口 智能康... 天津北方网讯:对脑卒中患者来说,重新抬起手臂、迈出一步,往往需要持续的康复训练,逐步恢复运动功能;对...
原创 苹... 当苹果、谷歌、斯坦福、麻省理工都无法留住一位年轻的中国AI天才时,究竟是什么力量吸引他毅然回国?为何...
为什么新能源车供应链要本地化? 当前汽车产业变革加速,电动化成为核心发展方向。车企为了在激烈市场中站稳脚跟,必须优化生产结构。其中,...
携程罚单的真正影响,不是酒店变... 文 | 听风译码 7月25日,市场监管总局对携程开出51.79亿元天价罚单。 全网叫好。 但如果你...
自己管理自己,我国成功发射两颗... 7月24日消息,力箭一号遥十五运载火箭今日在东风商业航天创新试验区成功发射,以“一箭5星”方式将5颗...