User Tools

Site Tools


Sidebar


Tags Cloud
web_monitoring

This is an old revision of the document!


Скрипт для проверки доступности вебсервера (с SMS)

Работает под debian.

# mkdir -p /opt/scripts/monitoring/active_alerts

Скрипт ложить в /opt/scripts/monitoring/checker.sh

#!/bin/bash

SCRIPT_DIR=/opt/scripts/monitoring
LOG_FILE=${SCRIPT_DIR}/status.log
PHONES="80501234567 80507654321"
CURDATE=`/bin/date +"%s"`;

function alert
{
    for i in $PHONES;
    do  
        echo "$1 $2 $3" | /usr/sbin/sendmail -fmonitoring@monitoring ${i}@sms.mysmsgate.com;
    done
}

function check
{
    cat $SCRIPT_DIR/targets |  while read ip server;
    do  
        /usr/bin/wget -T 5 --tries=3 -q -O - http://${ip}/.do_not_remove.php | grep SUCCESS >/dev/null;
        if [[ $? == 0 ]]; then
            if [[ -e $SCRIPT_DIR/active_alerts/$server ]]; then
                cat $SCRIPT_DIR/active_alerts/$server | while read startdate;
                do  
                    DATE=`/bin/date +"%F %T"`;
                    let "i =  $CURDATE - $startdate";
                    lenght=`/bin/date -u +"%T" --date=@${i}`;
                    alert "$DATE" $server "is UP, downtime is $lenght";
                    echo "$DATE" $server "is UP, downtime is $lenght" > $LOG_FILE
                    rm -f $SCRIPT_DIR/active_alerts/$server
                done
            fi
        else
            if [[ -e $SCRIPT_DIR/active_alerts/$server ]]; then
                MINUTE=`/bin/date +"%M"`
                if [[ $MINUTE == 00 ]]; then
                    cat $SCRIPT_DIR/active_alerts/$server | while read startdate;
                do  
                    DATE=`/bin/date +"%F %T"`;
                    let "i =  $CURDATE - $startdate";
                    lenght=`/bin/date -u +"%T" --date=@${i}`;
                    alert "$DATE" $server "down for $lenght";
                done
                fi
            else
                echo $CURDATE > $SCRIPT_DIR/active_alerts/$server;
                DATE=`/bin/date +"%F %T"`;
                alert "$DATE" $server "is DOWN";
                echo "$DATE" $server "is UP, downtime is $lenght" > $LOG_FILE
            
            fi
        fi
    done
}

check
web_monitoring.1387127155.txt.gz · Last modified: 2013/12/15 17:05 by kyxap