jablonka.czprosek.czf

crusader

Subversion Repositories:
[/] [trunk/] [statistiky/] [opt/] [statistiky/] [collect_crusader.sh.20080607] - Rev 1

Compare with Previous - Blame - Download


#!/bin/bash3
#
#
# 2006-11-22/Libor  Predelano cteni RSS pomoci nove aplikace napsane v C pro snizeni
#                   zateze CPU, ktera na VIA C3 800MHz ve stavajici podobe dosahovala
#                   az 100%.
#
# 2005-11-27/Libor  Predelano cteni seriaku. Ted si ho otevre jako filedescriptor
#                   a cte z nej jako ze souboru. Na Marvinovi se tim (snad) uplne
#                   odstranily chly do nuly, ktere byly nejspis zpusobeny
#                   rychlosti (pomalosti) Marvina. (800MHz VIA C3)
# 2005-11-02/Libor  konfiguracni skripty na prehazovani primarni FSO <-> backup WiFi
#                   logovani do /var/statistiky/crusader/crusader.log
#                   bash3, nevim proc, ale na Marvinovi mi to s dvojkou nechodilo kvuliva
#                   porovnavani desetinnych cisel pomoci if [ $neco -le $rss ]...
# 2005-10-27/Libor  pridano prehazovani na WiFi zalohu pri signalu < 300
#
#

PATH=$PATH:/opt/statistiky/bin

if [ $# -eq 1 ]; then
    crusader_number=$1
fi
if [ $# -gt 1 ]; then
    echo "ERROR: more than 1 option" >&2
fi
if [ $# -lt 1 ]; then
    crusader_number=0
fi

stat_dir="/var/statistiky/crusader"
logfile="$stat_dir/crusader.log"
mkdir -p $stat_dir

config_file=/opt/statistiky/collect_crusader.conf
script_set_main=/opt/statistiky/crusader_set_main.sh
script_set_backup=/opt/statistiky/crusader_set_backup.sh

function Log()
# $1 ... message
{
    timenow=`date +"%Y-%m-%d %T"`
    echo "LOG: $timenow    $1"
    echo "$timenow    $1" >>$logfile
}

Log "Crusader start ====="
Log "Using config file: $config_file"

if [ ! -f $config_file ]
then
    Log "Missing config file $config_file, exiting."
    exit 1;
fi

if [ ! -x $script_set_main ]; then
    Log "Script set up main is not executable: $script_set_main"
    exit 1;
fi
if [ ! -x $script_set_backup ]; then
    Log "Script set up main is not executable: $script_set_backup"
    exit 1;
fi

cd $stat_dir

function create()
# $1 ... netdevice
{
    local file=$1
    Log "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

}

# zastarala: ted cekame pomoci crusader_read_rss
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_backup()
{
    $script_set_backup
    echo "Activated WiFi backup"
}
function activate_fso()
{
    $script_set_main
    echo "Activated FSO primary link"
}

function read_remote_status()
{
    remote_rss_status=`grep ^RSS: /var/statistiky/crusader/remote_site_status | awk '{ print $2;}'`
    echo "Remote RSS status: $remote_rss_status"
}

function write_status_to_remote()
{
    Log "    Writing status (RSS: $rss_status) to remote side"
    ssh libor@10.23.64.169 "echo \"RSS: $rss_status\" >/var/statistiky/crusader/remote_site_status"
}

function rss_check_status()
# $1 ... jmeno rusaderu (napr. crusader0)
# $2 je sila signalu RSS
{
    local name=$1
    local rss=$2
    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_counter_go_backup ]; then
        echo "RSS goes down"
        rss_status="low"
        Log "Crusader $crusader_number: goes down, RSS: low ($rss)"
        write_status_to_remote
    fi
    if [ "$rss_status" == "low" ] && [ $rss_good_count -ge $rss_good_counter_go_main ]; then
        echo "RSS goes up"
        rss_status="good"
        Log "Crusader $crusader_number: goes up, RSS: good ($rss)"
        write_status_to_remote
    fi

    if [ "$link_status" == "FSO" ] && [ "$rss_status" == "low" ]
    then
        Log "Link to backup"
        activate_backup
        link_status="backup"
    fi
    if [ "$link_status" == "backup" ] && [ "$rss_status" == "good" ] && [ "$remote_rss_status" == "good" ]
    then
        Log "Link to FSO"
        activate_fso
        link_status="FSO"
    fi

    read_remote_status

    if [ "$link_status" == "FSO" ] && [ "$remote_rss_status" == "low" ]
    then
        Log "Link to backup"
        activate_backup
        link_status="backup"
        write_status_to_remote
    fi
}

# Pocatecni nastaveni linky FSO/backup
function setup_initial_link_status()
{
    if [ $rss_avg -gt $rss_good_threshold ]; then
        rss_status="good"
        rss_status_last="good"
        link_status="unknown"
    else
        rss_status="low"
        rss_status_last="low"
        link_status="unknown"
    fi
    write_status_to_remote
    read_remote_status

    if [ "$rss_status" == "good" ] && [ "$remote_rss_status" == "good" ]
    then
        Log "Link to FSO"
        activate_fso
        link_status="FSO"
    else
        Log "Link to backup"
        activate_backup
        link_status="backup"
    fi
}

function load_config()
{
    . ${config_file}
    rss_low_threshold=${RSS_LOW_THRESHOLD[$crusader_number]}
    rss_good_threshold=${RSS_GOOD_THRESHOLD[$crusader_number]}
    rss_low_counter_go_backup=${RSS_LOW_COUNTER_TO_GO_BACKUP[$crusader_number]}
    rss_good_counter_go_main=${RSS_GOOD_COUNTER_TO_GO_MAIN[$crusader_number]}
    device=${DEVICE[$crusader_number]}
    name=${NAME[$crusader_number]}${crusader_number}
}

print_config()
{
    Log "Crusader $crusader_number"
    Log "    RSS low threshold = $rss_low_threshold"
    Log "    RSS good threshold = $rss_good_threshold"
    Log "    RSS low counter go backup = $rss_low_counter_go_backup"
    Log "    RSS good counter go main = $rss_good_counter_go_main"
    Log "    Seriak: $device"
}

# Inicializace
load_config
print_config
j=0
rv=2

rss_line=`crusader_read_rss ${device} +2 2>/dev/null`
rss_avg=`echo $rss_line | awk '{ print $2; }'`
if [ -z "$rss_avg" ]; then
    rss_avg="0"
fi
Log "    Sila signalu pri startu (RSS): $rss_avg"

# Prvotni nastaveni linky pred hlavni smyckou
setup_initial_link_status

# kolikrat byl signal nizsi nez rss_low_threshold opakovane v rade po sobe
rss_low_counter=0
# kolikrat byl signal vyssi nez rss_good_threshold opakovane v rade po sobe
rss_good_counter=0

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

# Hlavni smycka
while [ true ] ; do

    load_config
    
    rrdfile=${name}
    if [ ! -f $rrdfile.rrd ] ; then
        create $rrdfile
    fi

    rss_line=`crusader_read_rss ${device} ${interval} 2>/dev/null`
    echo "RSS LINE: $rss_line"
    pocet=`echo $rss_line | awk '{ print $1; }'`
    if [ -z "$pocet" ]; then
        pocet="0"
    fi
    rss_avg=`echo $rss_line | awk '{ print $2; }'`
    rss_max=`echo $rss_line | awk '{ print $3; }'`
    rss_min=`echo $rss_line | awk '{ print $4; }'`
    if [ -z "$rss_avg" ]; then
        rss_avg="0"
    fi
    if [ -z "$rss_max" ]; then
        rss_max="0"
    fi
    if [ -z "$rss_min" ]; then
        rss_min="0"
    fi

    if [ $pocet -gt 0 ]
    then
        echo "Pocet vzorku: $pocet   RSS avg: $rss_avg   min: $rss_min   max: $rss_max"
        rrdtool update $rrdfile.rrd -t rss_avg:rss_min:rss_max $interval:$rss_avg:$rss_min:$rss_max
        rss_check_status ${name} $rss_avg
    else
        echo "NaN"
        rrdtool update $rrdfile.rrd -t rss_avg:rss_min:rss_max $interval:U:U:U
        rss_check_status ${name} 0
    fi
    
    interval=$(( $interval + 5 ))

    echo ""
done

Powered by WebSVN 2.2.1