It was discovered days later that one of my websites was offline displaying only 500 error messages. As it turned out, it was as a result of the .htaccess files. The .htaccess files did not include <IfModule XXXXX> </IfModule> around the directives. Tracking down the offending lines, it was determined that the lines were all part of the mod_headers.c module. Not having changed the .htaccess files in months to years, it has to be the work of the hosting company. After correcting the .htaccess files, all the sites came back online. Then there was round two.
After a few days, it was discovered one of the sites was offline again. This time as a result of something else, which was corrected. After this recent incident, I devised a way to monitor the servers using a CentOS 6 server. Using a bash script, mailx, and a cron task makes this possible.
This script makes use of curl to identify the header of a given website to determine if the site is online. A value of HTTP/1.1 200 OK is the desired result of [OK] for this test and any other result will return a result of [FAIL]. If the script returns a [FAIL] then an email will be sent. For mailx to work. I am not going to assume that these utilities are on the computer so, here is the command line to install mailx and curl.
yum install -y mailx curl
Create a directory for the cert, create the certs database, and hit enter to the password questions for a blank password. Go to that directory and download the cert from your site. Then add the cert to your database. Here are the steps.
mkdir /certs certutil -N -d /certs cd /certs echo -n | openssl s_client -connect yourwebsite.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > yourcert certutil -A -n yourwebsite.com -t "P,P,P" -i yourcert -d /certs
To configure mailx to work, a file must be created in the root directory as .mailrc. If you are using SSL at port 465, make note that the set smtp uses smtps. For the last value of nss-config-dir, point it to your /certs directory. In that directory is the cert8.db file, the certificate needed for SSL. If you do not use the last line, nss-config-dir, the script will not work, and there will be errors related to that specific line.
echo "set smtp=smtps://server.myhosting.com:465 set smtp-auth=login set smtp-auth-user=myname@myemailaddress.com set smtp-auth-password=mypasswordhere set ssl-verify=ignore set nss-config-dir=/certs" >> /root/.mailrc
To test the above script, the following command worked well.
echo "test message" | mailx -r therecipeintname@emailaddress.com -s "Test Subject" thesendername@emailaddress.com
Now the script.
#!/bin/bash ###################################### # USER DEFINABLE VARIABLES ###################################### mySites=( www.mywebsite.com sub.mywebsite.com anothersite.com it.megocollector.com ) ###################################### # MAIN PROGRAM ###################################### RED=$(tput setaf 1) GREEN=$(tput setaf 2) BLUE=$(tput setaf 4) NORMAL=$(tput sgr0) echo "$BLUE""Status Site" "$NORMAL" for i in "${mySites[@]}" do if [ $(curl -Is http://$i | head -1 | cut -f2 -d " ") = "200" ]; then echo "$GREEN" "[OK]" "$NORMAL" $i else echo "$RED" "[FAIL]" "$NORMAL" $i echo "$i HTTP/1.1 200 FAIL " | mailx -r therecipeintname@emailaddress.com -s "HTTP/1.1 200 FAIL" thesendername@emailaddress.com fi done
Make the script an executable with chmod +x checkmysites.sh
Now for the cron task. Using crontab -e create a task to point to your new script. Here is an example of a script to run nightly.
5 23 * * * /opt/checkmysites.sh
Alternately, you could copy the checkmysites.sh script to /etc/cron.daily where the script will run daily.
[root@server opt]# ./checkmysites.sh Status Site [OK] www.megocollector.com [OK] poetry.megocollector.com [OK] it.megocollector.com [OK] www.elvisrecords.us [root@server opt]#
Source(s)
- www.cyberciti.biz/faq/bash-for-loop-array/
- www.shellhacks.com/en/Check-a-Website-Availability-from-the-Linux-Command-Line
- stackoverflow.com/questions/5506176/bash-echo-something-to-right-end-of-window-right-aligned
- stackoverflow.com/questions/1296979/how-set-the-from-email-address-for-mailx-command
- Forums.opensuse.org/showthread.php/458696-mailx-is-not-working
- forums.fedoraforum.org/archive/index.php/t-225852.html
- certificate.fyicenter.com/398_Mozilla_CertUtil_Mozilla_certutil_-N__Create_New_cert8.db.html
- https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/tools/
- https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/tools/NSS_Tools_certutil
- http://serverfault.com/questions/498588/smtp-gmail-com-from-bash-gives-error-in-certificate-peers-certificate-issuer