2015-07-07 15:27:08 +03:00
|
|
|
#!/bin/bash
|
|
|
|
|
########################################################################
|
2015-07-07 12:50:56 +03:00
|
|
|
# Copyright (C) 2015 Max Mehl <mail@mehl.mx>
|
2015-07-07 15:27:08 +03:00
|
|
|
########################################################################
|
2015-07-07 12:50:56 +03:00
|
|
|
#
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
#
|
2015-07-07 15:27:08 +03:00
|
|
|
########################################################################
|
|
|
|
|
#
|
|
|
|
|
# A bash application to monitor websites via ping and index comparison
|
|
|
|
|
# In case of error it can send email notifications. See config.cfg.sample
|
|
|
|
|
#
|
|
|
|
|
#######################################################################
|
2014-12-08 16:04:37 +01:00
|
|
|
|
|
|
|
|
cd "$(dirname "$(readlink -f "$0")")"
|
|
|
|
|
# Test if config.cfg exists and set needed variables
|
|
|
|
|
if [ ! -e config.cfg ]; then echo "Missing config.cfg file. Edit and rename config.cfg.sample"; exit 1; fi
|
|
|
|
|
source config.cfg
|
|
|
|
|
|
|
|
|
|
function fappend {
|
|
|
|
|
echo -e "$2">>$1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function mailsend {
|
2014-12-08 16:11:45 +01:00
|
|
|
TOEMAIL="$TOEMAIL";
|
|
|
|
|
FREMAIL="$FREMAIL";
|
2014-12-08 16:31:27 +01:00
|
|
|
SUBJECT="[EWM] $1";
|
2014-12-08 16:04:37 +01:00
|
|
|
MSGBODY1="$2"
|
|
|
|
|
MSGBODY2="$3"
|
|
|
|
|
TMP=`mktemp`
|
|
|
|
|
|
|
|
|
|
fappend $TMP "From: $FREMAIL";
|
|
|
|
|
fappend $TMP "To: $TOEMAIL";
|
|
|
|
|
fappend $TMP "Reply-To: $FREMAIL";
|
|
|
|
|
fappend $TMP "Subject: $SUBJECT";
|
|
|
|
|
fappend $TMP "";
|
|
|
|
|
fappend $TMP "$MSGBODY1";
|
|
|
|
|
fappend $TMP "";
|
|
|
|
|
fappend $TMP "$MSGBODY2";
|
|
|
|
|
fappend $TMP "";
|
2014-12-08 16:11:45 +01:00
|
|
|
cat $TMP | "$SENDMAILPATH" -t;
|
2014-12-08 16:04:37 +01:00
|
|
|
rm $TMP;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function pingtest {
|
2015-01-08 23:08:09 +01:00
|
|
|
#ping -c3 -W3 -q $1 >> /dev/null
|
|
|
|
|
#timeout 5 bash -c 'echo "ping" > /dev/tcp/$1/80'; echo $?
|
|
|
|
|
|
|
|
|
|
nc -z -w3 $1 80 > /dev/null
|
|
|
|
|
|
2014-12-09 13:53:06 +01:00
|
|
|
PSTATUS=$(echo $?)
|
2014-12-08 16:04:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function indextest {
|
2015-04-07 13:33:55 +03:00
|
|
|
wget -q -T 15 -t 5 -U $(cat monitor-user-agent.txt) -O $TEMPDIR/"$CURHOST".html http://"$CURHOST"
|
2014-12-08 16:04:37 +01:00
|
|
|
|
|
|
|
|
if [ -e sites/"$CURHOST".ignore ]; then
|
|
|
|
|
CURIGN=$(cat sites/"$CURHOST".ignore)
|
|
|
|
|
else
|
|
|
|
|
CURIGN="VeRy-UnLiKeLy-StRiNg-To-MaTcH"
|
|
|
|
|
fi
|
|
|
|
|
|
2014-12-09 13:53:06 +01:00
|
|
|
diff -qBb -I "$CURIGN" sites/"$CURHOST".orig $TEMPDIR/"$CURHOST".html
|
|
|
|
|
ISTATUS=$(echo $?)
|
2014-12-08 16:04:37 +01:00
|
|
|
}
|
|
|
|
|
|
2014-12-09 13:53:06 +01:00
|
|
|
if [ ! -e count.txt ]; then
|
|
|
|
|
touch $COUNTFILE
|
|
|
|
|
fi
|
|
|
|
|
source $COUNTFILE
|
|
|
|
|
RUN=$(expr $RUN + 1)
|
|
|
|
|
> $COUNTFILE
|
2014-12-09 14:06:44 +01:00
|
|
|
echo "[INFO] Starting Run $RUN -- $(date)" >> $LOGFILE
|
2014-12-09 14:04:49 +01:00
|
|
|
|
2014-12-09 13:53:06 +01:00
|
|
|
|
|
|
|
|
if [ ! -e $TEMPDIR ]; then
|
|
|
|
|
mkdir $TEMPDIR
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
2014-12-08 16:04:37 +01:00
|
|
|
for ((i = 0; i < ${#NAME[*]}; i++)); do
|
|
|
|
|
CURNAME=${NAME[$i]}
|
|
|
|
|
CURHOST=${HOST[$i]}
|
|
|
|
|
|
|
|
|
|
## TEST PING
|
|
|
|
|
pingtest $CURHOST
|
2014-12-09 13:53:06 +01:00
|
|
|
if [ "$PSTATUS" != 0 ]; then
|
|
|
|
|
ERROR[$i]=$(expr ${ERROR[$i]} + 1)
|
|
|
|
|
echo "[WARNING] Ping to $CURHOST failed" >> $LOGFILE
|
2014-12-08 16:04:37 +01:00
|
|
|
|
2014-12-09 13:53:06 +01:00
|
|
|
if [ "${ERROR[$i]}" -le "$MAXERROR" ]; then
|
|
|
|
|
mailsend "Ping error on $CURHOST" \
|
|
|
|
|
"Ping was unsuccessful" \
|
|
|
|
|
"Check http://$CURHOST"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
ERROR[$i]=$(expr ${ERROR[$i]} + 0)
|
|
|
|
|
echo "[INFO] Ping to $CURHOST successful" >> $LOGFILE
|
2014-12-08 16:04:37 +01:00
|
|
|
fi
|
|
|
|
|
|
2014-12-09 13:53:06 +01:00
|
|
|
## TEST FOR CHANGED INDEX (only if ping was successful)
|
|
|
|
|
if [ "$PSTATUS" = 0 ]; then
|
|
|
|
|
indextest
|
|
|
|
|
if [ "$ISTATUS" != 0 ]; then
|
|
|
|
|
ERROR[$i]=$(expr ${ERROR[$i]} + 1)
|
|
|
|
|
echo "[WARNING] Index check for $CURHOST failed" >> $LOGFILE
|
|
|
|
|
diff -U 0 -Bb -I "$CURIGN" sites/"$CURHOST".orig $TEMPDIR/"$CURHOST".html > $TEMPDIR/"$CURHOST".diff
|
|
|
|
|
|
|
|
|
|
if [ "${ERROR[$i]}" -le "$MAXERROR" ]; then
|
|
|
|
|
mailsend "Index error on $CURHOST" \
|
|
|
|
|
"Index on $CURHOST differs from original index: \n\n$(cat $TEMPDIR/$CURHOST.diff)" \
|
|
|
|
|
"Update original index: $SSHCONN"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
ERROR[$i]=$(expr ${ERROR[$i]} + 0)
|
|
|
|
|
echo "[INFO] Index check for $CURHOST successful" >> $LOGFILE
|
|
|
|
|
fi
|
|
|
|
|
fi
|
2014-12-08 16:04:37 +01:00
|
|
|
done
|
|
|
|
|
|
2014-12-09 13:53:06 +01:00
|
|
|
if [ -e $TEMPDIR ]; then
|
|
|
|
|
rm -rf $TEMPDIR
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ "$RUN" -le "$RESET" ]; then
|
|
|
|
|
echo "RUN=$RUN" >> $COUNTFILE
|
|
|
|
|
for ((i = 0; i < ${#ERROR[*]}; i++)); do
|
|
|
|
|
echo "ERROR[$i]="${ERROR[$i]}"" >> $COUNTFILE
|
|
|
|
|
done
|
|
|
|
|
else
|
|
|
|
|
echo "RUN=0" >> $COUNTFILE
|
|
|
|
|
for ((i = 0; i < ${#ERROR[*]}; i++)); do
|
|
|
|
|
echo "ERROR[$i]=0" >> $COUNTFILE
|
|
|
|
|
> $LOGFILE
|
|
|
|
|
done
|
|
|
|
|
fi
|
2014-12-08 16:04:37 +01:00
|
|
|
|