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