Files
easy-web-mon/monitor.sh
T

151 lines
4.4 KiB
Bash
Executable File

#!/bin/bash
########################################################################
# Copyright (C) 2015 Max Mehl <mail@mehl.mx>
########################################################################
#
# 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/>.
#
########################################################################
#
# A bash application to monitor websites via ping and index comparison
# In case of error it can send email notifications. See config.cfg.sample
#
#######################################################################
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 {
TOEMAIL="$TOEMAIL";
FREMAIL="$FREMAIL";
SUBJECT="[EWM] $1";
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 "";
cat $TMP | "$SENDMAILPATH" -t;
rm $TMP;
}
function pingtest {
#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
PSTATUS=$(echo $?)
}
function indextest {
wget -q -T 15 -t 5 -U $(cat monitor-user-agent.txt) -O $TEMPDIR/"$CURHOST".html http://"$CURHOST"
if [ -e sites/"$CURHOST".ignore ]; then
CURIGN=$(cat sites/"$CURHOST".ignore)
else
CURIGN="VeRy-UnLiKeLy-StRiNg-To-MaTcH"
fi
diff -qBb -I "$CURIGN" sites/"$CURHOST".orig $TEMPDIR/"$CURHOST".html
ISTATUS=$(echo $?)
}
if [ ! -e count.txt ]; then
touch $COUNTFILE
fi
source $COUNTFILE
RUN=$(expr $RUN + 1)
> $COUNTFILE
echo "[INFO] Starting Run $RUN -- $(date)" >> $LOGFILE
if [ ! -e $TEMPDIR ]; then
mkdir $TEMPDIR
fi
for ((i = 0; i < ${#NAME[*]}; i++)); do
CURNAME=${NAME[$i]}
CURHOST=${HOST[$i]}
## TEST PING
pingtest $CURHOST
if [ "$PSTATUS" != 0 ]; then
ERROR[$i]=$(expr ${ERROR[$i]} + 1)
echo "[WARNING] Ping to $CURHOST failed" >> $LOGFILE
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
fi
## 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
done
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