Subversion Repositories oSiteChecker

Rev

Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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