jablonka.czprosek.czf

crusader

Subversion Repositories:
[/] [trunk/] [statistiky/] [opt/] [statistiky/] [collect_ping_new.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 1 minuta, ale RRD je uklada s petiminutovou
# granularitou, tj. agregujeme do 5
#
# za poslednich 16 hod chceme vsechny udaje => 16*60 = 960 PDP (primary data points)
# za posledni tyden chceme 5min udaje => 12*24*9 = 2592 hodnot
# za posledni mesic chceme 20minutove useky => 3*24*38 = 2736 hodnot
# za posledni rok chceme dvouhodinove useky => 12*400 = 4800 hodnot
rrdtool create ${filename}.rrd --step 60 --start `date +%s`    \
                          DS:pktloss:GAUGE:120:0:100     \
                          DS:rttmin:GAUGE:120:0:60000    \
                          DS:rttmax:GAUGE:120:0:60000    \
                          DS:rttavg:GAUGE:120:0:60000    \
                          DS:rttmdev:GAUGE:120:0:60000   \
                          RRA:AVERAGE:0.7:1:960     \
                          RRA:MAX:0.7:5:2592        \
                          RRA:MIN:0.7:5:2592        \
                          RRA:AVERAGE:0.7:5:2592    \
                          RRA:MAX:0.7:20:2736       \
                          RRA:MIN:0.7:20:2736       \
                          RRA:AVERAGE:0.7:20:2736   \
                          RRA:MAX:0.7:120:4080      \
                          RRA:MIN:0.7:120:4080      \
                          RRA:AVERAGE:0.7:120: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`

    ping -A -w $PING_DEADLINE -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 + 60 ))
done


Powered by WebSVN 2.2.1