3 |
marcus |
1 |
#!/bin/bash
|
|
|
2 |
|
|
|
3 |
#
|
|
|
4 |
# Script that checks if given sites are up and sends an e-mail if the are not.
|
|
|
5 |
# You should not run this script on the same servers your monitoring,
|
|
|
6 |
# neither should your e-mail account reside on a server behind same firewall as the sites your checking.
|
|
|
7 |
#
|
|
|
8 |
# Developer: oSource Development
|
|
|
9 |
# DevTeam: Marcus Uddenhed
|
|
|
10 |
# Version: 0.6.1
|
|
|
11 |
# Web: http://www.osource.se
|
|
|
12 |
# Updated: 2017-10-30
|
|
|
13 |
#
|
|
|
14 |
# Requires: heirloom-mailx (http://heirloom.sourceforge.net/mailx.html)
|
|
|
15 |
#
|
|
|
16 |
# Licensed under BSDL license, see license information on our homepage.
|
|
|
17 |
#
|
|
|
18 |
|
|
|
19 |
#### Global variables ####
|
|
|
20 |
# Sites
|
|
|
21 |
checkSite[1]="http://" # Add sites that you want to monitor, can be as many as you want.
|
|
|
22 |
checkSite[2]="https://"
|
|
|
23 |
|
|
|
24 |
# Retries
|
|
|
25 |
rInterval=8 # How many times should it retry until it set the site as down.
|
|
|
26 |
rTime=20 # How many seconds shold it wait between retries.
|
|
|
27 |
|
|
|
28 |
# Mail
|
|
|
29 |
sendMail=yes # Should i send an e-mail with failed sites.
|
|
|
30 |
mailFrom="" # From whom shall this mail be sent?
|
|
|
31 |
mailTo="" # To whom shall the mail be sent?
|
|
|
32 |
smtpHost="" # The SMTP mail server that is going to process the mail.
|
|
|
33 |
smtpAuth=yes # Does the SMTP mail server require authentication(yes/no).
|
|
|
34 |
smtpUser="" # Username if smtpAuth is set to yes.
|
|
|
35 |
smtpPass="" # password if smtpAuth is set to yes.
|
|
|
36 |
smtpPort=587 # Standard for non authenticated SMTP is 25, 587 is for authenticated SMTP.
|
|
|
37 |
mailBin="heirloom-mailx" # executable file for mailx.
|
|
|
38 |
|
|
|
39 |
# Logging
|
|
|
40 |
logStatus=yes # Should we log to local logfile(yes/no).
|
|
|
41 |
logPath="/var/log" # Where should we put the log file(omit the traling slash).
|
|
|
42 |
logName="oSiteChecker.log" # name of log file.
|
|
|
43 |
|
|
|
44 |
#### Do not edit anything below unless you know what you are doing. ####
|
|
|
45 |
|
|
|
46 |
check_sites() {
|
|
|
47 |
# Declare $is variable
|
|
|
48 |
is=0
|
|
|
49 |
|
|
|
50 |
# Start control
|
|
|
51 |
for i in "${checkSite[@]}"
|
|
|
52 |
do
|
|
|
53 |
CURL=$(curl -s --head $i)
|
|
|
54 |
if echo $CURL | grep "200 OK" > /dev/null
|
|
|
55 |
then
|
|
|
56 |
# add entry to log file if logging enabled
|
|
|
57 |
if [ $logStatus == "yes" ]
|
|
|
58 |
then
|
|
|
59 |
echo `date +%F` `date +%T` "$i" "Host up" >> $logPath/$logName
|
|
|
60 |
else
|
|
|
61 |
# if logging set to no send to null
|
|
|
62 |
echo "" > /dev/null
|
|
|
63 |
fi
|
|
|
64 |
else
|
|
|
65 |
# Collect all sites that do not respond as they should for further processing.
|
|
|
66 |
siteArray[ $is ]="${i}"
|
|
|
67 |
(( is++ ))
|
|
|
68 |
fi
|
|
|
69 |
done
|
|
|
70 |
|
|
|
71 |
# Check to see if any sites been reported as down.
|
|
|
72 |
if [ ${#siteArray[@]} -ne 0 ]
|
|
|
73 |
then
|
|
|
74 |
# Test each failed site in a given amount of times.
|
|
|
75 |
for ((n=1;n<=$rInterval;n++))
|
|
|
76 |
do
|
|
|
77 |
# Go through failed sites again to eliminate temporary glicthes.
|
|
|
78 |
for i in "${siteArray[@]}"
|
|
|
79 |
do
|
|
|
80 |
CURL=$(curl -s --head $i)
|
|
|
81 |
if echo $CURL | grep "200 OK" > /dev/null
|
|
|
82 |
then
|
|
|
83 |
# remove succesful sites from array.
|
|
|
84 |
delete=(${i})
|
|
|
85 |
siteArray=( "${siteArray[@]/$delete}" )
|
|
|
86 |
else
|
|
|
87 |
# Keeps failed site in array.
|
|
|
88 |
echo "" > /dev/null
|
|
|
89 |
fi
|
|
|
90 |
done
|
|
|
91 |
|
|
|
92 |
# Clean empty lines in siteArray
|
|
|
93 |
tmpArray=()
|
|
|
94 |
for value in "${siteArray[@]}"
|
|
|
95 |
do
|
|
|
96 |
[[ $value != "" ]] && tmpArray+=($value)
|
|
|
97 |
done
|
|
|
98 |
siteArray=("${tmpArray[@]}")
|
|
|
99 |
unset tmpArray
|
|
|
100 |
|
|
|
101 |
# Debug
|
|
|
102 |
#for i in "${siteArray[@]}"
|
|
|
103 |
#do
|
|
|
104 |
# echo ${i}
|
|
|
105 |
#done
|
|
|
106 |
|
|
|
107 |
# Sleeps for an amount of time between checking site status again.
|
|
|
108 |
sleep $rTime
|
|
|
109 |
done
|
|
|
110 |
fi
|
|
|
111 |
|
|
|
112 |
# notification action
|
|
|
113 |
if [ ${#siteArray[@]} -ne 0 ]
|
|
|
114 |
then
|
|
|
115 |
#echo ${#siteArray[@]}
|
|
|
116 |
# Send an email with status
|
|
|
117 |
if [ $sendMail == "yes" ]
|
|
|
118 |
then
|
|
|
119 |
# Temp path for mailbody.txt
|
|
|
120 |
tmpPath=/tmp
|
|
|
121 |
|
|
|
122 |
# Set mail subject
|
|
|
123 |
mailSubject="Connectivity test failed"
|
|
|
124 |
|
|
|
125 |
# Build mail body
|
|
|
126 |
echo "Hi" > $tmpPath/mailbody.txt
|
|
|
127 |
echo "" >> $tmpPath/mailbody.txt
|
|
|
128 |
echo "One or more servers is not responding as the should, see complete list below." >> $tmpPath/mailbody.txt
|
|
|
129 |
echo "" >> $tmpPath/mailbody.txt
|
|
|
130 |
for i in "${siteArray[@]}"
|
|
|
131 |
do
|
|
|
132 |
echo ${i} >> $tmpPath/mailbody.txt
|
|
|
133 |
done
|
|
|
134 |
#echo "Test - Array size:" ${#siteArray[@]} >> $tmpPath/mailbody.txt
|
|
|
135 |
echo "" >> $tmpPath/mailbody.txt
|
|
|
136 |
echo "Sent:" `date +%Y-%m-%d` `date +%H:%M` >> $tmpPath/mailbody.txt
|
|
|
137 |
echo "" >> $tmpPath/mailbody.txt
|
|
|
138 |
echo "Regards" >> $tmpPath/mailbody.txt
|
|
|
139 |
echo "" >> $tmpPath/mailbody.txt
|
|
|
140 |
echo "Your $(cat /etc/hostname) server." >> $tmpPath/mailbody.txt
|
|
|
141 |
|
|
|
142 |
# Send data
|
|
|
143 |
MAILRC=/dev/null $mailBin -n
|
|
|
144 |
if [ $smtpAuth = "no" ]
|
|
|
145 |
then
|
|
|
146 |
env MAILRC=/dev/null from=$mailFrom smtp=$smtpHost:$smtpPort $mailBin -v -n -s "$mailSubject" $mailTo <$tmpPath/mailbody.txt
|
|
|
147 |
fi
|
|
|
148 |
if [ $smtpAuth = "yes" ]
|
|
|
149 |
then
|
|
|
150 |
env 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.txt
|
|
|
151 |
fi
|
|
|
152 |
|
|
|
153 |
# Remove temporary mailbody.txt file
|
|
|
154 |
rm $tmpPath/mailbody.txt
|
|
|
155 |
else
|
|
|
156 |
echo "" > /dev/null
|
|
|
157 |
fi
|
|
|
158 |
|
|
|
159 |
# add entry to log file if logging enabled
|
|
|
160 |
if [ $logStatus == "yes" ]
|
|
|
161 |
then
|
|
|
162 |
for i in "${siteArray[@]}"
|
|
|
163 |
do
|
|
|
164 |
echo `date +%F` `date +%T` ${i} "Host down" >> $logPath/$logName
|
|
|
165 |
done
|
|
|
166 |
fi
|
|
|
167 |
fi
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
mail_test() {
|
|
|
171 |
# Temp path for mailbody.txt
|
|
|
172 |
tmpPath=/tmp
|
|
|
173 |
|
|
|
174 |
# Set mail subject
|
|
|
175 |
mailSubject="Mail test"
|
|
|
176 |
|
|
|
177 |
# Build mail body
|
|
|
178 |
echo "Hi" > $tmpPath/mailbody.txt
|
|
|
179 |
echo "" >> $tmpPath/mailbody.txt
|
|
|
180 |
echo "This is a test mail to see that the mail function works." >> $tmpPath/mailbody.txt
|
|
|
181 |
echo "" >> $tmpPath/mailbody.txt
|
|
|
182 |
echo "Sent:" `date +%Y-%m-%d` `date +%H:%M` >> $tmpPath/mailbody.txt
|
|
|
183 |
echo "" >> $tmpPath/mailbody.txt
|
|
|
184 |
echo "Regards" >> $tmpPath/mailbody.txt
|
|
|
185 |
echo "" >> $tmpPath/mailbody.txt
|
|
|
186 |
echo "Your $(cat /etc/hostname) server." >> $tmpPath/mailbody.txt
|
|
|
187 |
|
|
|
188 |
# Send data
|
|
|
189 |
MAILRC=/dev/null $mailBin -n
|
|
|
190 |
if [ $smtpAuth = "no" ]
|
|
|
191 |
then
|
|
|
192 |
env MAILRC=/dev/null from=$mailFrom smtp=$smtpHost:$smtpPort $mailBin -v -n -s "$mailSubject" $mailTo <$tmpPath/mailbody.txt
|
|
|
193 |
fi
|
|
|
194 |
if [ $smtpAuth = "yes" ]
|
|
|
195 |
then
|
|
|
196 |
env 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.txt
|
|
|
197 |
fi
|
|
|
198 |
|
|
|
199 |
# Remove temporary mailbody.txt file
|
|
|
200 |
rm $tmpPath/mailbody.txt
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
script_help() {
|
|
|
204 |
echo "Usage: oSiteChecker.sh <parameter>"
|
|
|
205 |
echo ""
|
|
|
206 |
echo "Parameters:"
|
|
|
207 |
echo "--check Does a test connection to pre-configured sites and test to see if they are responding."
|
|
|
208 |
echo "--mail-test Does a test on the mail function to see if all mail related settings are okay."
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
case $1 in
|
|
|
212 |
--check)
|
|
|
213 |
check_sites
|
|
|
214 |
;;
|
|
|
215 |
--mail-test)
|
|
|
216 |
mail_test
|
|
|
217 |
;;
|
|
|
218 |
*)
|
|
|
219 |
script_help
|
|
|
220 |
;;
|
|
|
221 |
esac
|