jablonka.czprosek.czf

crusader

Subversion Repositories:
[/] [trunk/] [statistiky/] [opt/] [statistiky/] [archiv/] [collect_crusader.sh.20051027] - Rev 3 Go to most recent revision

Compare with Previous - Blame - Download


#!/bin/bash3
#
#
# 2005-10-27/Libor  pridano prehazovani na WiFi zalohu pri signalu < 300
#
#

stat_dir="/var/statistiky/crusader"
mkdir -p $stat_dir

config_file=/opt/statistiky/collect_crusader.conf

if [ ! -f $config_file ]
then
    echo "Missing config file $config_file"
    exit 1;
fi

cd $stat_dir

function create()
# $1 ... netdevice
{
    local file=$1
    echo "Creating RRD archive $file"

# step = 5s
# za posledni 2,5 hodiny chceme vsechny PDP => 2,5 * 3600 / 5 = 1800 hodnot/radku
# 20s ~ agregace ze ctyr hodnot (PDP)
# za poslednich 10 hodin chceme 20s udaje => 10*3600/20 = 1800 generaci/radku
# za poslednich 30 hodin chceme 60s udaje => 30*3600/60 = 1800 generaci/radku
# 5 minut = 12 PDP za minutu * 5 minut = 60 PDP
# za posledni tyden (9dni) chceme 5minutove udaje => 12*24*9 = 2592 radku (rows)
# 20min ~ agregace z 240 PDP
# za posledni mesic (38dni) chceme 20minutove useky => 3*24*38 = 2736 radku
# dve hodiny ~ 1440 PDP
# za posledni rok chceme dvouhodinove useky => 12*400 = 4800 radku

rrdtool create ${file}.rrd --step 5 --start `date +%s`    \
                          DS:rss_avg:GAUGE:10:0:999     \
                          DS:rss_min:GAUGE:10:0:999     \
                          DS:rss_max:GAUGE:10:0:999     \
                          RRA:AVERAGE:0.7:1:1800     \
                          RRA:MAX:0.7:1:1800     \
                          RRA:MIN:0.7:1:1800     \
                          RRA:AVERAGE:0.7:4:2880     \
                          RRA:MAX:0.7:4:2880         \
                          RRA:MIN:0.7:4:2880         \
                          RRA:AVERAGE:0.7:12:1800     \
                          RRA:MAX:0.7:12:1800         \
                          RRA:MIN:0.7:12:1800         \
                          RRA:AVERAGE:0.7:60:2592    \
                          RRA:MAX:0.7:60:2592        \
                          RRA:MIN:0.7:60:2592        \
                          RRA:AVERAGE:0.7:240:2736   \
                          RRA:MAX:0.7:240:2736       \
                          RRA:MIN:0.7:240:2736       \
                          RRA:AVERAGE:0.7:1440:4800  \
                          RRA:MAX:0.7:1440:4800      \
                          RRA:MIN:0.7:1440:4800

}

function wait_to()
# $1 date for waiting to
{
    local time_now=`date +%s`;
    while [ $time_now -lt $1 ]
    do
        time_now=`date +%s`
        sleep 1;
    done
}

function activate_wifi()
{
    ip route del 10.23.13.128/24
    ip route add 10.23.13.128/24 via 10.23.13.1
    echo "Activated WiFi backup"
}
function activate_fso()
{
    ip route del 10.23.13.128/24
    ip route add 10.23.13.128/24 via 10.23.192.54
    echo "Activated FSO primary link"
}

# kolikrat byl signal nizsi nez rss_low_threshold opakovane v rade po sobe
rss_low_count=0
# pocet detekovanych slabych signalu v rade, kdy se prepoji inet na zalohu
rss_low_count_down=3
# pokud sila signalu klesne pod threshold je indikovana jako slaby signal
rss_low_threshold=300
# kolikrat byl signal vyssi nez rss_good_threshold opakovane v rade po sobe
rss_good_count=0
# pocet detekovanych dobrych signalu v rade, kdy se prepoji inet zpet na Crusader
rss_good_count_up=12
# pokud sila signalu stoupne nad threshold je indikovana jako dobry signal
rss_good_threshold=350
# stav sily signalu, muze byt 'low' nebo 'good'
# zaciname s 'good' protoze prepnuti good->low je rychlejsi a taky startup skripty
# pocitaji s optickou linkou, WiFi je zaloha
rss_status="good"
rss_status_last="good"

function rss_check_status()
# $1 je sila signalu RSS
{
    local rss=$1
    if [ $rss -ge $rss_low_threshold ]; then
        echo "RSS($rss) >= low_threshold ($rss_low_threshold), erasing rss_low_count"
        rss_low_count="0"
    fi
    if [ $rss -le $rss_good_threshold ]; then
        echo "RSS($rss) <= good_threshold ($rss_good_threshold), erasing rss_good_count"
        rss_good_count="0"
    fi
    if [ "$rss_status" == "good" ] && [ $rss -lt $rss_low_threshold ]; then
        let rss_low_count+=1
        echo "RSS($rss) < low_threshold ($rss_low_threshold), increasing rss_low_count to $rss_low_count"
    fi
    if [ "$rss_status" == "low" ] && [ $rss -gt $rss_good_threshold ]; then
        let rss_good_count+=1
        echo "RSS($rss) > good_threshold ($rss_good_threshold), increasing rss_good_count to $rss_good_count"
    fi
    if [ "$rss_status" == "good" ] && [ $rss_low_count -ge $rss_low_count_down ]; then
        echo "FSO link goes down"
        rss_status="low"
        activate_wifi
    fi
    if [ "$rss_status" == "low" ] && [ $rss_good_count -ge $rss_good_count_up ]; then
        echo "FSO link goes up"
        rss_status="good"
        activate_fso
    fi
}

start_time=`date +%s`
interval=$(( $start_time / 5 * 5 + 5 ))

while [ true ] ; do

    . ${config_file}

    device=${DEVICE[0]}
    rrdfile=${NAME[0]}0

    if [ ! -f $rrdfile.rrd ] ; then
        create $rrdfile
    fi

    soucet_rss="0"
    rss_min="1000"                      # ma to jen tri cifry, tj. max 999 => 1000 je vic nez maximum
    rss_max="0"
    pocet="0"
    read rss <${device}                 # pouze synchronizace, holt o jeden radek prijdeme
    time_now=`date +%s`
    while [ $time_now -lt $interval ]; do   # cekame na zacatek dalsiho intervalu
        time_now=`date +%s`

        read -t 1 rss rest <${device}      # tady cteme
#        echo "rss: '$rss' and the rest of line: '$rest'"
        test $rss -ge 0 2>/dev/null  # slouzi k otestovani, ze rss obsahuje numerickou hodnoru
        if [ $? -lt 2 ]             # pokud vyjde 2, pak to neni cislo a test selhal
        then
            soucet_rss=$(( $soucet_rss + $rss ))
            pocet=$(( $pocet + 1 ))
            if [ $rss -lt $rss_min ]; then
                rss_min=$rss
            fi
            if [ $rss -gt $rss_max ]; then
                rss_max=$rss
            fi
        fi
    done
    
    if [ $pocet -gt 0 ]
    then
        prumer=`echo "${soucet_rss}/${pocet}" | bc -l`
        echo "prumer: $prumer   min: $rss_min   max: $rss_max"
        rrdtool update $rrdfile.rrd -t rss_avg:rss_min:rss_max $interval:$prumer:$rss_min:$rss_max
        prumer=`echo $prumer | cut -d '.' -f 1`
        rss_check_status $prumer
    else
        echo "NaN"
        rrdtool update $rrdfile.rrd -t rss_avg:rss_min:rss_max $interval:U:U:U
        rss_check_status 0
    fi
    
    interval=$(( $interval + 5 ))

done

Powered by WebSVN 2.2.1