From bf63000ff785242f5b0e5b8142d37374422c5f1b Mon Sep 17 00:00:00 2001 From: mxmehl Date: Tue, 9 Dec 2014 13:53:06 +0100 Subject: [PATCH] add max error and improve general behaviour --- .gitignore | 6 ++-- config.cfg.sample | 13 +++++++ monitor.sh | 86 +++++++++++++++++++++++++++++++++++------------ 3 files changed, 81 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index 7f5fa1b..8ceef0f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ config.cfg -*.html -*.diff sites/*.orig sites/*.ignore +sites/*.html +temp +count.log +monitor.log diff --git a/config.cfg.sample b/config.cfg.sample index c27caeb..4b5cda8 100644 --- a/config.cfg.sample +++ b/config.cfg.sample @@ -2,6 +2,19 @@ TOEMAIL=destination@email.com FREMAIL=sender@email.com SENDMAILPATH=/usr/sbin/sendmail +LOGFILE=monitor.log +COUNTFILE=count.log +TEMPDIR=temp + +# This link is added if an index error appears. +# You can use update-index-ssh-link.sh to open ssh:// links +SSHCONN=ssh://user@host.tld + +# Maximum $MAXERROR mails if an error occurs. +# Will be reset after $RESET runs of script +MAXERROR=3 +RESET=24 + NAME[0]=shortname1 HOST[0]=example.com diff --git a/monitor.sh b/monitor.sh index 13ce470..1604fd0 100755 --- a/monitor.sh +++ b/monitor.sh @@ -31,12 +31,12 @@ function mailsend { } function pingtest { - ping -c3 -W3 -q $1 - STATUS=$(echo $?) + ping -c3 -W3 -q $1 >> /dev/null + PSTATUS=$(echo $?) } function indextest { - wget -q -T 5 -t 2 -U $(cat monitor-user-agent.txt) -O "$CURHOST".html http://"$CURHOST" + wget -q -T 5 -t 2 -U $(cat monitor-user-agent.txt) -O $TEMPDIR/"$CURHOST".html http://"$CURHOST" if [ -e sites/"$CURHOST".ignore ]; then CURIGN=$(cat sites/"$CURHOST".ignore) @@ -44,36 +44,78 @@ function indextest { CURIGN="VeRy-UnLiKeLy-StRiNg-To-MaTcH" fi - diff -qBb -I $CURIGN sites/"$CURHOST".orig "$CURHOST".html - STATUS=$(echo $?) - + 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 + +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 [ "$STATUS" != 0 ]; then - mailsend "Ping error on $CURHOST" \ - "Ping was unsuccessful" \ - "Check http://$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 - indextest - if [ "$STATUS" != 0 ]; then - diff -U 0 -Bb -I $CURIGN sites/"$CURHOST".orig "$CURHOST".html > "$CURHOST".diff - - mailsend "Index error on $CURHOST" \ - "Index on $CURHOST differs from original index: \n\n$(cat $CURHOST.diff)" \ - "Update original index: ssh://$CURNAME" - - rm "$CURHOST".diff + ## 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 - rm "$CURHOST".html - 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