![]() ![]() |
crusader |
Subversion Repositories: |
Compare with Previous - Blame - Download
#!/bin/bashPATH=/opt/rrdtool/bin:$PATH:/sbin:/usr/sbindata_dir="/var/statistiky/ping"mkdir -p $data_dirconfig_file=/opt/statistiky/collect_ping.conflogdir=/var/log/statistikyif [ ! -d ${logdir} ]thenmkdir -p ${logdir}filogfile=${logdir}/ping.logif [ ! -f $config_file ]thenecho "Config file $config_file not found"exit 1;ficd ${data_dir}function create(){local filename=$1echo "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 hodnotrrdtool 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 ]dotime_now=`date +%s`sleep 1;done}if [ ! -d ${data_dir} ]; thenmkdir -p ${data_dir}fifunction check_rrd_file(){local filename=$1echo "Looking for ${filename}.rrd"if [ ! -f ${filename}.rrd ] ; thencreate ${filename}sleep 1;fi}start_time=`date +%s`maininterval=$(( $start_time / 60 * 60 ))function ip_to_name(){local target=$1echo $target | tr '.' '_'}function do_ping(){local target=$1local filename=`ip_to_name $target`local tmpfile="/tmp/stat_ping_${filename}"check_rrd_file $filenametimenow=`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 ]thenpkt_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`" = "" ]thenpkt_loss=`echo $pkt_loss_line | awk '{print $6;}' | tr -d '%'`fiif [ "`echo $pkt_loss_line | grep duplicates`" != "" ] && [ "`echo $pkt_loss_line | grep errors`" = "" ]thendup=`echo $pkt_loss_line | awk '{print $6;}'`pkt_loss=`echo $pkt_loss_line | awk '{print $8;}' | tr -d '%'`fiif [ "`echo $pkt_loss_line | grep duplicates`" = "" ] && [ "`echo $pkt_loss_line | grep errors`" != "" ]thenerrors=`echo $pkt_loss_line | awk '{print $6;}'`pkt_loss=`echo $pkt_loss_line | awk '{print $8;}' | tr -d '%'`fiif [ "`echo $pkt_loss_line | grep duplicates`" != "" ] && [ "`echo $pkt_loss_line | grep errors`" != "" ]thendup=`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 '%'`firtt_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.logrrdtool update ${filename}.rrd ${timenow}:${pkt_loss}:${rttmin}:${rttmax}:${rttavg}:${rttmdev}fi}while [ true ]do. $config_fileecho "Wait to $maininterval"interval=$mainintervalwait_to $intervalecho "Time now: `date +%s`"for dest in $PING_DESTINATIONSdodo_ping $destdonemaininterval=$(( $maininterval + 300 ))done