Subversion Repositories oSiteChecker

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 marcus 1
#!/bin/bash
2
 
3
#
4
# Script that checks if given sites are up and sends an e-mail if they 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.5.0
11
# Web: http://www.osource.se
12
# Updated: 2017-08-31
13
#
14
# Requires: 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]="https://";							# Add sites that you want to monitor, can be as many as you want.
22
checkSite[2]="http://";
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? Seperate recipients with a ","
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
 
38
# Logging
39
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
logName="oSiteChecker.log"							# name of log file.
42
 
43
#### Do not edit anything below unless you know what you are doing. ####
44
 
45
check_sites() {
46
        # Declare $is variable
47
        is=0
48
 
49
        # Start control
50
        for i in "${checkSite[@]}"
51
        do
52
                CURL=$(curl -s --head $i)
53
                if echo $CURL | grep "200 OK" > /dev/null
54
                then
55
                        # add entry to log file if logging enabled
56
                        if [ $logStatus == "yes" ]
57
                        then
58
                                echo `date +%F` `date +%T` "$i" "Host up" >> $logPath/$logName
59
                        else
60
                                # if logging set to no send to null
61
                                echo "" > /dev/null
62
                        fi
63
                else
64
                        # Collect all sites that do not respond as they should for further processing.
65
                        siteArray[ $is ]="${i}"
66
                        (( is++ ))
67
                fi
68
        done
69
 
70
    # Check to see if any sites been reported as down.
71
        if [ ${#siteArray[@]} -ne 0 ]
72
        then
73
                # Test each failed site in a given amount of times.
74
                for ((n=1;n<=$rInterval;n++))
75
                do
76
                        # Go through failed sites again to eliminate temporary glicthes.
77
                        for i in "${siteArray[@]}"
78
                        do
79
                                CURL=$(curl -s --head $i)
80
                                if echo $CURL | grep "200 OK" > /dev/null
81
                                then
82
                                        # remove succesful sites from array.
83
                                        delete=(${i})
84
                                        siteArray=( "${siteArray[@]/$delete}" )
85
                                else
86
                                        # Keeps failed site in array.
87
                                        echo "" > /dev/null
88
                                fi
89
                        done
90
 
91
                        # Clean empty lines in siteArray
92
                        tmpArray=()
93
                        for value in "${siteArray[@]}"
94
                        do
95
                                [[ $value != "" ]] && tmpArray+=($value)
96
                        done
97
                        siteArray=("${tmpArray[@]}")
98
                        unset tmpArray
99
 
100
                        # Debug
101
                        #for i in "${siteArray[@]}"
102
                        #do
103
                        #       echo ${i}
104
                        #done
105
 
106
                        # Sleeps for an amount of time between checking site status again.
107
                        sleep $rTime
108
                done
109
        fi
110
 
111
        # notification action
112
        if [ ${#siteArray[@]} -ne 0 ]
113
        then
114
        #echo ${#siteArray[@]}
115
                # Send an email with status
116
                if [ $sendMail == "yes" ]
117
                then
118
                        # Temp path for mailbody.txt
119
                        tmpPath=/tmp
120
 
121
                        # Set mail subject
122
                        mailSubject="Connectivity test failed"
123
 
124
                        # Build mail body
125
                        echo "Hi" > $tmpPath/mailbody.txt
126
                        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 "" >> $tmpPath/mailbody.txt
129
                        for i in "${siteArray[@]}"
130
                        do
131
                                echo ${i} >> $tmpPath/mailbody.txt
132
                        done
133
                        #echo "Test - Array size:" ${#siteArray[@]} >> $tmpPath/mailbody.txt
134
                        echo "" >> $tmpPath/mailbody.txt
135
                        echo "Sent:" `date +%Y-%m-%d` `date +%H:%M` >> $tmpPath/mailbody.txt
136
                        echo "" >> $tmpPath/mailbody.txt
137
                        echo "Regards" >> $tmpPath/mailbody.txt
138
                        echo "" >> $tmpPath/mailbody.txt
139
                        echo "Your $(cat /etc/hostname) server." >> $tmpPath/mailbody.txt
140
 
141
                        # Send data
142
                        MAILRC=/dev/null mailx -n
143
                        if [ $smtpAuth = "no" ]
144
                        then
145
                                env MAILRC=/dev/null from=$mailFrom smtp=$smtpHost:$smtpPort mailx -v -n -s "$mailSubject" $mailTo  <$tmpPath/mailbody.txt
146
                        fi
147
                        if [ $smtpAuth = "yes" ]
148
                        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
                        fi
151
 
152
                        # Remove temporary mailbody.txt file
153
                        rm $tmpPath/mailbody.txt
154
                else
155
                        echo "" > /dev/null
156
                fi
157
 
158
                # add entry to log file if logging enabled
159
                if [ $logStatus == "yes" ]
160
                then
161
                        for i in "${siteArray[@]}"
162
                        do
163
                                echo `date +%F` `date +%T` ${i} "Host down" >> $logPath/$logName
164
                        done
165
                fi
166
        fi
167
}
168
 
169
mail_test() {
170
        # Temp path for mailbody.txt
171
        tmpPath=/tmp
172
 
173
        # Set mail subject
174
        mailSubject="Mail test"
175
 
176
        # Build mail body
177
        echo "Hi" > $tmpPath/mailbody.txt
178
        echo "" >> $tmpPath/mailbody.txt
179
        echo "This is a test mail to see that the mail function works." >> $tmpPath/mailbody.txt
180
        echo "" >> $tmpPath/mailbody.txt
181
        echo "Sent:" `date +%Y-%m-%d` `date +%H:%M` >> $tmpPath/mailbody.txt
182
        echo "" >> $tmpPath/mailbody.txt
183
        echo "Regards" >> $tmpPath/mailbody.txt
184
        echo "" >> $tmpPath/mailbody.txt
185
        echo "Your $(cat /etc/hostname) server." >> $tmpPath/mailbody.txt
186
 
187
        # Send data
188
        MAILRC=/dev/null mailx -n
189
        if [ $smtpAuth = "no" ]
190
        then
191
                env MAILRC=/dev/null from=$mailFrom smtp=$smtpHost:$smtpPort mailx -v -n -s "$mailSubject" $mailTo  <$tmpPath/mailbody.txt
192
        fi
193
        if [ $smtpAuth = "yes" ]
194
        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
        fi
197
 
198
        # Remove temporary mailbody.txt file
199
        rm $tmpPath/mailbody.txt
200
}
201
 
202
case $1 in
203
        --check)
204
                check_sites
205
        ;;
206
    --mail-test)
207
                mail_test
208
    ;;
209
        *)
210
                script_help
211
        ;;
212
esac