|
| 1 | +#/bin/bash |
| 2 | + |
| 3 | +# Lightweight bash script that processes ping's output in realtime and |
| 4 | +# suppresses normal ping responses and prints instead only on packets lost |
| 5 | +# and errors. It handles SIGQUIT correctly so that it can be used to invoke |
| 6 | +# pings summary lines mid run. It also allows for the ping summary |
| 7 | +# footer to be printed at the end. |
| 8 | + |
| 9 | +# This script focus's on being fork light. It only forks ping once to do the |
| 10 | +# actuall ICMP work, and date per loss block. Everything else is bash internals. |
| 11 | + |
| 12 | +# Note the time stamps reported when an ICMP reply is recieved, and the lost |
| 13 | +# block is calculated. |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +# Authors Chris Browning <[email protected]> |
| 20 | + |
| 21 | +# Copyright 2013 Lightwire Limited |
| 22 | + |
| 23 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 24 | +# you may not use this file except in compliance with the License. |
| 25 | +# You may obtain a copy of the License at |
| 26 | + |
| 27 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 28 | + |
| 29 | +# Unless required by applicable law or agreed to in writing, software |
| 30 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 31 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 32 | +# See the License for the specific language governing permissions and |
| 33 | +# limitations under the License. |
| 34 | + |
| 35 | + |
| 36 | +last=0 |
| 37 | +prev=0 |
| 38 | +printit=0 |
| 39 | + |
| 40 | + |
| 41 | +ping $@ | while read i |
| 42 | +trap "" SIGQUIT |
| 43 | +trap "" SIGINT |
| 44 | +do |
| 45 | + # Deal with printing summary at the end |
| 46 | + if [ $printit -eq 1 ]; then |
| 47 | + |
| 48 | + # Add any additional loss based on summary data |
| 49 | + val=${i// packets transmitted*/} |
| 50 | + if [[ $val != *[!0-9]* ]]; then |
| 51 | + if [ -n "$val" ]; then |
| 52 | + if [ "$val" -ne "$last" ]; then |
| 53 | + echo "`date +'%F %T'`: Lost $((val-last)) additional packets at quit ($((last+1)) to $((val)))" |
| 54 | + fi |
| 55 | + fi |
| 56 | + fi |
| 57 | + |
| 58 | + # Header finished exit |
| 59 | + if [ -z "$i" ]; then |
| 60 | + exit 0 |
| 61 | + fi |
| 62 | + |
| 63 | + # Print summary data |
| 64 | + echo $i |
| 65 | + continue |
| 66 | + fi |
| 67 | + |
| 68 | + # Pull the request id |
| 69 | + val=${i//* icmp_req=/} |
| 70 | + val=${val// */} |
| 71 | + |
| 72 | + # Detect and deal with the Ping Header |
| 73 | + if [ "$val" == "PING" ]; then |
| 74 | + continue |
| 75 | + fi |
| 76 | + |
| 77 | + # Detect and deal with start of Ping Summary |
| 78 | + if [ -z "$val" ]; then |
| 79 | + echo $i |
| 80 | + printit=1 |
| 81 | + continue |
| 82 | + fi |
| 83 | + |
| 84 | + # Deal with ICMP error responses (print them to screen) |
| 85 | + if [[ $val != *[!0-9]* ]]; then |
| 86 | + # Normal response |
| 87 | + if [ "$((val-1))" -ne "$last" ]; then |
| 88 | + echo "`date +'%F %T'`: Lost $((val-1-last)) packets ($((last+1)) to $((val-1)))" |
| 89 | + fi |
| 90 | + last=$val |
| 91 | + else |
| 92 | + echo $i |
| 93 | + fi |
| 94 | +done |
0 commit comments