Shell script to monitor if the site is abnormal

Source: Internet
Author: User
Tags curl printf

The shell monitors the site for unusual script, such as an abnormal automatic email notification to the administrator.

Process:

1. Check that the Http_code returned by the Web site is equal to 200, if not 200 as an exception.

2. Check Web site access time, more than Maxloadtime (10 seconds) as an exception.

3. Send notification email, in the/tmp/monitor_load.remark record send time, in an hour does not repeat, such as an hour after the empty/tmp/monitor_load.remark.

#!/bin/bash sites= ("http://web01.example.com" "http://web02.example.com") # to monitor the site notice_email= ' me@example.co M ' # Admin Email maxloadtime=10 # access Timeout set RE                                                      Markfile= '/tmp/monitor_load.remark ' # has sent a notification email if it has been sent and will not be sent within an hour issend=0 # do you send email expire=3600 # Number of seconds to send email NO w=$ (date +%s) if [-F "$REMARKFILE"] && [-S "$REMARKFILE"]; Then remark=$ (cat $REMARKFILE) # Delete expired email delivery time record file if [$ ($NOW-$REMARK))-gt "$EXPIRE"]; Then rm-f ${remarkfile} remark= "fi else Remark=" "fi # Loops to judge each site for Si Te in ${sites[*]}; Do printf "start to load ${site}\n" site_load_time=$ (Curl-o/dev/null-s-W "Time_connect:%{time_conn Ect}\ntime_starttransfer:%{time_starttransfer}\ntime_total:%{time_total} "${site}" site_access=$ (Curl-o/dev/null-s-W%{http_code} "${site}") Time_tot al=${site_load_time##*:} printf "$ (date ' +%y-%m-%d%h:%m:%s ') \ n" printf "Site load Time\n${site_load_ti me}\n "printf" site access:${site_access}\n\n "# not send if [' $REMARK ' = ']; Then # check access if ["$time _total" = "0.000"] | | ["$site _access"!= "200"]; Then echo "Subject: ${site} can access $ (date +%y-%m-%d '%h:%m:%s)" | SendMail ${notice_email} issend=1 Else # Check load time if [' ${time_ total%%.*} "-ge ${maxloadtime}]; Then echo "Subject: ${site} load Time Total:${time_total} $ (Date +%y-%m-%d ' '%h:%m:%s ')" | SendMail ${notice_email} issend=1 fi fi done # send email PostScript Record sending time if ["$ISSEND" = "1"]; Then echo "$ (date +%s)" > $REMARKFILE fi exit 0 

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Servers/web/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.