Shell script to monitor web site for anomalies

Source: Internet
Author: User

The content of this section:
The shell script monitors the site for abnormalities and automatically sends an email notification to the administrator if there is an exception.

The script detection process is as follows:
1, check whether the Http_code returned by the site equals 200, if not 200 is considered an exception.
2, check the site access time, more than Maxloadtime (10 seconds) as an exception.
3, after sending the notification email, in the/tmp/monitor_load.remark record send time, within one hour does not repeat sends, such as an hour after the empty/tmp/monitor_load.remark.

Code:

#!/bin/Bash # Site:www.jquerycn.cnSITES=("http://web01.example.com" "http://web02.example.com") # Sites to monitor Notice_email='[email protected]'# Admin Email maxloadtime=Ten# Access time-out setting Remarkfile='/tmp/monitor_load.remark'# No notification emails are sent on record, if sent, no longer sent within one hour issend=0# is there a send email EXPIRE=3600# Number of seconds per email sent now=$ (Date +%s)if[-F"$REMARKFILE"] && [-S"$REMARKFILE" ]; Then REMARK=$ (cat $REMARKFILE) # Delete expired email send time log fileif[$ ($NOW-$REMARK))-GT"$EXPIRE" ]; Then RM-F ${remarkfile} REMARK=""fiElseREMARK=""fi # Loop to judge each site forSiteinch${sites[*]}; Doprintf"start to load ${site}\n"Site_load_time=$ (Curl-o/dev/NULL-s-w"time_connect:%{time_connect}\ntime_starttransfer:%{time_starttransfer}\ntime_total:%{time_total}" "${site}") site_access=$ (Curl-o/dev/NULL-s-w%{http_code}"${site}") Time_Total=${site_load_time##*:} printf"$ (Date ' +%y-%m-%d%h:%m:%s ') \ n"printf"Site Load time\n${site_load_time}\n"printf"site access:${site_access}\n\n"# Not sendif["$REMARK"="" ]; Then # Check Accessif["$time _total"="0.000"] || ["$site _access"!=" $" ]; Then Echo"Subject: ${site} can access $ (date +%y-%m-%d '%h:%m:%s)"|sendmail ${notice_email} issend=1          Else# Check Load timeif["${time_total%%.*}"-GE ${maxloadtime}]; Then Echo"Subject: ${site} load Time Total:${time_total} $ (Date +%y-%m-%d '%h:%m:%s)"|sendmail ${notice_email} issend=1fi fi fi done # send e-mail record send timeif["$ISSEND"="1" ]; Then Echo"$ (date +%s)">$REMARKFILE fi Exit0 

Shell script to monitor web site for anomalies

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.