Short description for repo.or.cz.
[snmpsar.git] / pingmon.sh
blob36c8fecf511873261e6627ea48332643ed81c7f6
1 #!/bin/bash
3 # vi: set softtabstop=4 shiftwidth=4 tabstop=8 expandtab:
5 # Pings a list of hosts and saves the summary to the directory given in the
6 # first argument.
8 usage()
10 echo "usage: $0 outdir host1 [host2 [...]]" 1>&2
11 exit 1
14 outdir=$1
15 shift
16 hosts="$@"
18 if [ -n "$outdir" ] && [ -n "$hosts" ]
19 then
21 else
22 usage
25 if ! [ -d "$outdir" ]
26 then
27 echo "output directory $outdir does not exist" 1>&2
28 exit 1
31 filestamp="$(date +%Y-%m-%d)"
32 timestamp="$(date +%H:%M:%S/%s)"
33 file="$outdir/pingmon.$filestamp"
35 if ! [ -f "$file" ]
36 then
37 echo -e "\t\t\tHOST\t\tloss%\trttavg" > "$file"
40 for h in $hosts
42 ping -q -c 50 -i0.5 "$h" | tail -2 > "$outdir/pending.$h" &
43 sleep 0.1
44 done
46 wait
48 echo >> "$file"
49 for h in $hosts
51 p="$outdir/pending.$h"
53 # 20 packets transmitted, 20 received, 0% packet loss, time 9500ms
54 # rtt min/avg/max/mdev = 0.132/0.143/0.157/0.013 ms
56 pktloss=$(grep "packet loss" "$p" | sed -e 's,^.* \([0-9.]\+\)%.*$,\1,')
58 rttavg=$(grep rtt "$p" |
59 sed -e 's,^.*\([0-9.]\+\)/\([0-9.]\+\)/\([0-9.]\+\)/\([0-9.]\+\).*$,\2,')
60 echo -e "$timestamp\t$h\t$pktloss\t$rttavg" >> "$file"
62 rm -f "$p"
63 done