jablonka.czprosek.czf

crusader

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

Compare with Previous - Blame - Download


#!/bin/bash

PATH=/opt/rrdtool/bin:$PATH:/sbin:/usr/sbin

data_dir="/var/statistiky/ping"
mkdir -p $data_dir

config_file=/opt/statistiky/collect_ping.conf
logdir=/var/log/statistiky
if [ ! -d ${logdir} ]
then
    mkdir -p ${logdir}
fi
logfile=${logdir}/ping.log

if [ ! -f $config_file ]
then
    echo "Config file $config_file not found"
    exit 1;
fi

cd ${data_dir}

function create()
{
    local filename=$1
    echo "creating new ${filename}.rrd"
#
# sbirame s periodou 5 minut, tj. 12 PDP za hodinu
#
# za posledni tyden chceme vsechny 5min udaje => 12*24*9 = 2592 hodnot
# za posledni mesic chceme 20minutove useky (agg 4) => 3*24*38 = 2736 hodnot
# za posledni rok chceme dvouhodinove useky (agg 24) => 12*400 = 4800 hodnot
rrdtool create ${filename}.rrd --step 300 --start `date +%s`    \
                          DS:pktloss:GAUGE:500:0:100     \
                          DS:rttmin:GAUGE:500:0:60000    \
                          DS:rttmax:GAUGE:500:0:60000    \
                          DS:rttavg:GAUGE:500:0:60000    \
                          DS:rttmdev:GAUGE:500:0:60000   \
                          RRA:AVERAGE:0.7:1:2592    \
                          RRA:MAX:0.7:4:2736       \
                          RRA:MIN:0.7:4:2736       \
                          RRA:AVERAGE:0.7:4:2736   \
                          RRA:MAX:0.7:24:4080      \
                          RRA:MIN:0.7:24:4080      \
                          RRA:AVERAGE:0.7:24: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
}

if [ ! -d ${data_dir} ]; then
    mkdir -p ${data_dir}
fi

function check_rrd_file()
{
    local filename=$1
    echo "Looking for ${filename}.rrd"
    if [ ! -f ${filename}.rrd ] ; then
        create ${filename}
        sleep 1;
    fi
}

start_time=`date +%s`
maininterval=$(( $start_time / 60 * 60 ))

function ip_to_name()
{
    local target=$1
    echo $target | tr '.' '_'
}

function do_ping()
{
    local target=$1
    local filename=`ip_to_name $target`
    local tmpfile="/tmp/stat_ping_${filename}"
        
    check_rrd_file $filename
    timenow=`date +%s`

# -w $PING_DEADLINE - nefunguje, kdyz linka chybuje, viz ping(8)
    ping -A  -c $PING_COUNT -q ${target} >${tmpfile}

    errors="0"
    dup="0"
    if [ $? -ne 2 ]
    then
        pkt_loss_line=`grep "packet loss" ${tmpfile} | awk '{print $6;}' | tr -d '%'`
        pkt_loss_line=`grep "packet loss" ${tmpfile}`
        if [ "`echo $pkt_loss_line | grep duplicates`" = "" ] && [ "`echo $pkt_loss_line | grep errors`" = "" ]
        then
            pkt_loss=`echo $pkt_loss_line | awk '{print $6;}' | tr -d '%'`
        fi
        if [ "`echo $pkt_loss_line | grep duplicates`" != "" ] && [ "`echo $pkt_loss_line | grep errors`" = "" ]
        then
            dup=`echo $pkt_loss_line | awk '{print $6;}'`
            pkt_loss=`echo $pkt_loss_line | awk '{print $8;}' | tr -d '%'`
        fi
        if [ "`echo $pkt_loss_line | grep duplicates`" = "" ] && [ "`echo $pkt_loss_line | grep errors`" != "" ]
        then
            errors=`echo $pkt_loss_line | awk '{print $6;}'`
            pkt_loss=`echo $pkt_loss_line | awk '{print $8;}' | tr -d '%'`
        fi
        if [ "`echo $pkt_loss_line | grep duplicates`" != "" ] && [ "`echo $pkt_loss_line | grep errors`" != "" ]
        then
            dup=`echo $pkt_loss_line | awk '{print $6;}'`
            errors=`echo $pkt_loss_line | awk '{print $8;}'`
            pkt_loss=`echo $pkt_loss_line | awk '{print $10;}' | tr -d '%'`
        fi
        rtt_line=`grep "^rtt min/avg/max/mdev" ${tmpfile} | awk '{print $4;}'`

        rttmin=`echo ${rtt_line} | cut -d '/' -f 1`
        rttavg=`echo ${rtt_line} | cut -d '/' -f 2`
        rttmax=`echo ${rtt_line} | cut -d '/' -f 3`
        rttmdev=`echo ${rtt_line} | cut -d '/' -f 4`

        echo "IP ${target} Packet loss: ${pkt_loss} RTT min: ${rttmin} max: ${rttmax} avg: ${rttavg} mdev: ${rttmdev} dup: ${dup} errors: ${errors}" >>/var/log/statistiky/ping.log
        rrdtool update ${filename}.rrd ${timenow}:${pkt_loss}:${rttmin}:${rttmax}:${rttavg}:${rttmdev}
    fi
}


while [ true ]
do
    . $config_file
    
    echo "Wait to $maininterval"
    interval=$maininterval
    wait_to $interval
    echo "Time now: `date +%s`"

    for dest in $PING_DESTINATIONS
    do

        do_ping $dest
    
    done
    maininterval=$(( $maininterval + 300 ))
done


Powered by WebSVN 2.2.1