Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Use shell scripts to monitor the running status of a website

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

I haven't written a blog for a long time. I've cleaned up almost all the blogs I've written before. It's good to write some when I want to write.

Shell monitors the status of the website / tomcat and relies on the return status code to determine the status. If 200302 is returned, the status is considered normal, otherwise the tomcat/nginx/LB/Haproxy/apache is considered to be dead. The script is implemented as follows:

1. Create a site folder and write all the addresses to be monitored into the http_site file.

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 #

two。 Write shell script to realize the monitoring function, use curl to visit the website, filter out the returned status code as a judgment condition, and send an email alarm if the return status code is not 200UniG302.

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 +% Ymuri% mmury% d` / `date +% T`

Failurefile=/root/script/check_http/history/ `date +% Ymuri% mmury% d` / `date +% T` _ failure

Mkdir / root/script/check_http/history/ `date +% Ymuri% mmurf% 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]] | | [[$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 UTFMUF 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. Add scheduled tasks to run every 5 minutes

Crontab-e

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

5. The test script has been running for several days after it has been written, and the effect is good. Share it with you.

In order to verify the effect, several tomcat were stopped at 23:12 on June 14. After stopping, the script detected that tomcat was not running. A file record of _ failure was generated and an email was sent to achieve the alarm effect.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report