jablonka.czprosek.czf

crusader

Subversion Repositories:
[/] [trunk/] [statistiky/] [opt/] [statistiky/] [collect_ping.sh] - Blame information for rev 2

 

Line No. Rev Author Line
11simandl#!/bin/bash
2 
3PATH=/opt/rrdtool/bin:$PATH:/sbin:/usr/sbin
4 
5data_dir="/var/statistiky/ping"
6mkdir -p $data_dir
7 
8config_file=/opt/statistiky/collect_ping.conf
9logdir=/var/log/statistiky
10if [ ! -d ${logdir} ]
11then
12 mkdir -p ${logdir}
13fi
14logfile=${logdir}/ping.log
15 
16if [ ! -f $config_file ]
17then
18 echo "Config file $config_file not found"
19 exit 1;
20fi
21 
22cd ${data_dir}
23 
24function create()
25{
26 local filename=$1
27 echo "creating new ${filename}.rrd"
28#
29# sbirame s periodou 5 minut, tj. 12 PDP za hodinu
30#
31# za posledni tyden chceme vsechny 5min udaje => 12*24*9 = 2592 hodnot
32# za posledni mesic chceme 20minutove useky (agg 4) => 3*24*38 = 2736 hodnot
33# za posledni rok chceme dvouhodinove useky (agg 24) => 12*400 = 4800 hodnot
34rrdtool create ${filename}.rrd --step 300 --start `date +%s` \
35 DS:pktloss:GAUGE:500:0:100 \
36 DS:rttmin:GAUGE:500:0:60000 \
37 DS:rttmax:GAUGE:500:0:60000 \
38 DS:rttavg:GAUGE:500:0:60000 \
39 DS:rttmdev:GAUGE:500:0:60000 \
40 RRA:AVERAGE:0.7:1:2592 \
41 RRA:MAX:0.7:4:2736 \
42 RRA:MIN:0.7:4:2736 \
43 RRA:AVERAGE:0.7:4:2736 \
44 RRA:MAX:0.7:24:4080 \
45 RRA:MIN:0.7:24:4080 \
46 RRA:AVERAGE:0.7:24:4800
47}
48 
49function wait_to()
50# $1 date for waiting to
51{
52 local time_now=`date +%s`;
53 while [ $time_now -lt $1 ]
54 do
55 time_now=`date +%s`
56 sleep 1;
57 done
58}
59 
60if [ ! -d ${data_dir} ]; then
61 mkdir -p ${data_dir}
62fi
63 
64function check_rrd_file()
65{
66 local filename=$1
67 echo "Looking for ${filename}.rrd"
68 if [ ! -f ${filename}.rrd ] ; then
69 create ${filename}
70 sleep 1;
71 fi
72}
73 
74start_time=`date +%s`
75maininterval=$(( $start_time / 60 * 60 ))
76 
77function ip_to_name()
78{
79 local target=$1
80 echo $target | tr '.' '_'
81}
82 
83function do_ping()
84{
85 local target=$1
86 local filename=`ip_to_name $target`
87 local tmpfile="/tmp/stat_ping_${filename}"
88 
89 check_rrd_file $filename
90 timenow=`date +%s`
91 
92# -w $PING_DEADLINE - nefunguje, kdyz linka chybuje, viz ping(8)
93 ping -A -c $PING_COUNT -q ${target} >${tmpfile}
94 
95 errors="0"
96 dup="0"
97 if [ $? -ne 2 ]
98 then
99 pkt_loss_line=`grep "packet loss" ${tmpfile} | awk '{print $6;}' | tr -d '%'`
100 pkt_loss_line=`grep "packet loss" ${tmpfile}`
101 if [ "`echo $pkt_loss_line | grep duplicates`" = "" ] && [ "`echo $pkt_loss_line | grep errors`" = "" ]
102 then
103 pkt_loss=`echo $pkt_loss_line | awk '{print $6;}' | tr -d '%'`
104 fi
105 if [ "`echo $pkt_loss_line | grep duplicates`" != "" ] && [ "`echo $pkt_loss_line | grep errors`" = "" ]
106 then
107 dup=`echo $pkt_loss_line | awk '{print $6;}'`
108 pkt_loss=`echo $pkt_loss_line | awk '{print $8;}' | tr -d '%'`
109 fi
110 if [ "`echo $pkt_loss_line | grep duplicates`" = "" ] && [ "`echo $pkt_loss_line | grep errors`" != "" ]
111 then
112 errors=`echo $pkt_loss_line | awk '{print $6;}'`
113 pkt_loss=`echo $pkt_loss_line | awk '{print $8;}' | tr -d '%'`
114 fi
115 if [ "`echo $pkt_loss_line | grep duplicates`" != "" ] && [ "`echo $pkt_loss_line | grep errors`" != "" ]
116 then
117 dup=`echo $pkt_loss_line | awk '{print $6;}'`
118 errors=`echo $pkt_loss_line | awk '{print $8;}'`
119 pkt_loss=`echo $pkt_loss_line | awk '{print $10;}' | tr -d '%'`
120 fi
121 rtt_line=`grep "^rtt min/avg/max/mdev" ${tmpfile} | awk '{print $4;}'`
122 
123 rttmin=`echo ${rtt_line} | cut -d '/' -f 1`
124 rttavg=`echo ${rtt_line} | cut -d '/' -f 2`
125 rttmax=`echo ${rtt_line} | cut -d '/' -f 3`
126 rttmdev=`echo ${rtt_line} | cut -d '/' -f 4`
127 
128 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
129 rrdtool update ${filename}.rrd ${timenow}:${pkt_loss}:${rttmin}:${rttmax}:${rttavg}:${rttmdev}
130 fi
131}
132 
133 
134while [ true ]
135do
136 . $config_file
137 
138 echo "Wait to $maininterval"
139 interval=$maininterval
140 wait_to $interval
141 echo "Time now: `date +%s`"
142 
143 for dest in $PING_DESTINATIONS
144 do
145 
146 do_ping $dest
147 
148 done
149 maininterval=$(( $maininterval + 300 ))
150done
151 

Powered by WebSVN 2.2.1