Files
easy-web-mon/monitor.sh
T
2014-12-18 23:42:05 +01:00

124 lines
3.2 KiB
Bash
Executable File

#!/bin/bash
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
PSTATUS=$(echo $?)
}
function indextest {
wget -q -T 8 -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