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