Rev 2 | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/bin/bash## Script that checks if given sites are up and sends an e-mail if the are not.# You should not run this script on the same servers your monitoring,# neither should your e-mail account reside on a server behind same firewall as the sites your checking.## Developer: oSource Development# DevTeam: Marcus Uddenhed# Version: 0.6.1# Web: http://www.osource.se# Updated: 2017-10-30## Requires: heirloom-mailx (http://heirloom.sourceforge.net/mailx.html)## Licensed under BSDL license, see license information on our homepage.##### Global variables ##### SitescheckSite[1]="http://" # Add sites that you want to monitor, can be as many as you want.checkSite[2]="https://"# RetriesrInterval=8 # How many times should it retry until it set the site as down.rTime=20 # How many seconds shold it wait between retries.sendMail=yes # Should i send an e-mail with failed sites.mailFrom="" # From whom shall this mail be sent?mailTo="" # To whom shall the mail be sent?smtpHost="" # The SMTP mail server that is going to process the mail.smtpAuth=yes # Does the SMTP mail server require authentication(yes/no).smtpUser="" # Username if smtpAuth is set to yes.smtpPass="" # password if smtpAuth is set to yes.smtpPort=587 # Standard for non authenticated SMTP is 25, 587 is for authenticated SMTP.mailBin="heirloom-mailx" # executable file for mailx.# LogginglogStatus=yes # Should we log to local logfile(yes/no).logPath="/var/log" # Where should we put the log file(omit the traling slash).logName="oSiteChecker.log" # name of log file.#### Do not edit anything below unless you know what you are doing. ####check_sites() {# Declare $is variableis=0# Start controlfor i in "${checkSite[@]}"doCURL=$(curl -s --head $i)if echo $CURL | grep "200 OK" > /dev/nullthen# add entry to log file if logging enabledif [ $logStatus == "yes" ]thenecho `date +%F` `date +%T` "$i" "Host up" >> $logPath/$logNameelse# if logging set to no send to nullecho "" > /dev/nullfielse# Collect all sites that do not respond as they should for further processing.siteArray[ $is ]="${i}"(( is++ ))fidone# Check to see if any sites been reported as down.if [ ${#siteArray[@]} -ne 0 ]then# Test each failed site in a given amount of times.for ((n=1;n<=$rInterval;n++))do# Go through failed sites again to eliminate temporary glicthes.for i in "${siteArray[@]}"doCURL=$(curl -s --head $i)if echo $CURL | grep "200 OK" > /dev/nullthen# remove succesful sites from array.delete=(${i})siteArray=( "${siteArray[@]/$delete}" )else# Keeps failed site in array.echo "" > /dev/nullfidone# Clean empty lines in siteArraytmpArray=()for value in "${siteArray[@]}"do[[ $value != "" ]] && tmpArray+=($value)donesiteArray=("${tmpArray[@]}")unset tmpArray# Debug#for i in "${siteArray[@]}"#do# echo ${i}#done# Sleeps for an amount of time between checking site status again.sleep $rTimedonefi# notification actionif [ ${#siteArray[@]} -ne 0 ]then#echo ${#siteArray[@]}# Send an email with statusif [ $sendMail == "yes" ]then# Temp path for mailbody.txttmpPath=/tmp# Set mail subjectmailSubject="Connectivity test failed"# Build mail bodyecho "Hi" > $tmpPath/mailbody.txtecho "" >> $tmpPath/mailbody.txtecho "One or more servers is not responding as the should, see complete list below." >> $tmpPath/mailbody.txtecho "" >> $tmpPath/mailbody.txtfor i in "${siteArray[@]}"doecho ${i} >> $tmpPath/mailbody.txtdone#echo "Test - Array size:" ${#siteArray[@]} >> $tmpPath/mailbody.txtecho "" >> $tmpPath/mailbody.txtecho "Sent:" `date +%Y-%m-%d` `date +%H:%M` >> $tmpPath/mailbody.txtecho "" >> $tmpPath/mailbody.txtecho "Regards" >> $tmpPath/mailbody.txtecho "" >> $tmpPath/mailbody.txtecho "Your $(cat /etc/hostname) server." >> $tmpPath/mailbody.txt# Send dataMAILRC=/dev/null $mailBin -nif [ $smtpAuth = "no" ]thenenv MAILRC=/dev/null from=$mailFrom smtp=$smtpHost:$smtpPort $mailBin -v -n -s "$mailSubject" $mailTo <$tmpPath/mailbody.txtfiif [ $smtpAuth = "yes" ]thenenv MAILRC=/dev/null from=$mailFrom smtp=$smtpHost:$smtpPort smtp-auth-user=$smtpUser smtp-auth-password=$smtpPass smtp-use-starttls=yes ssl-verify=ignore smtp-auth=login $mailBin -n -s "$mailSubject" $mailTo <$tmpPath/mailbody.txtfi# Remove temporary mailbody.txt filerm $tmpPath/mailbody.txtelseecho "" > /dev/nullfi# add entry to log file if logging enabledif [ $logStatus == "yes" ]thenfor i in "${siteArray[@]}"doecho `date +%F` `date +%T` ${i} "Host down" >> $logPath/$logNamedonefifi}mail_test() {# Temp path for mailbody.txttmpPath=/tmp# Set mail subjectmailSubject="Mail test"# Build mail bodyecho "Hi" > $tmpPath/mailbody.txtecho "" >> $tmpPath/mailbody.txtecho "This is a test mail to see that the mail function works." >> $tmpPath/mailbody.txtecho "" >> $tmpPath/mailbody.txtecho "Sent:" `date +%Y-%m-%d` `date +%H:%M` >> $tmpPath/mailbody.txtecho "" >> $tmpPath/mailbody.txtecho "Regards" >> $tmpPath/mailbody.txtecho "" >> $tmpPath/mailbody.txtecho "Your $(cat /etc/hostname) server." >> $tmpPath/mailbody.txt# Send dataMAILRC=/dev/null $mailBin -nif [ $smtpAuth = "no" ]thenenv MAILRC=/dev/null from=$mailFrom smtp=$smtpHost:$smtpPort $mailBin -v -n -s "$mailSubject" $mailTo <$tmpPath/mailbody.txtfiif [ $smtpAuth = "yes" ]thenenv MAILRC=/dev/null from=$mailFrom smtp=$smtpHost:$smtpPort smtp-auth-user=$smtpUser smtp-auth-password=$smtpPass smtp-use-starttls=yes ssl-verify=ignore smtp-auth=login $mailBin -n -s "$mailSubject" $mailTo <$tmpPath/mailbody.txtfi# Remove temporary mailbody.txt filerm $tmpPath/mailbody.txt}script_help() {echo "Usage: oSiteChecker.sh <parameter>"echo ""echo "Parameters:"echo "--check Does a test connection to pre-configured sites and test to see if they are responding."echo "--mail-test Does a test on the mail function to see if all mail related settings are okay."}case $1 in--check)check_sites;;--mail-test)mail_test;;*)script_help;;esac