small_scripts / prettyping.sh

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#!/bin/bash
#
# Written by Denilson Figueiredo de Sa <denilsonsa@gmail.com>
# 2010-11-05 - "ping" changed output from icmp_seq to icmp_req. Fixed.
# 2008-04-16 - Small bug fixes, small improvement to parameter filtering.
# 2008-02-10 - Started writing the second version. Added some live
#              statistics alongside with the colored chars.
# 2008-01-16 - Updated version. Removed [[ bash-ism. Now this script
#              also works on dash.
# 2008-01-12 - First version written and released.

# TODO: print the destination (also) at the bottom bar. Useful after leaving
# the script running for quite some time.
#
# TODO: Test the behavior of this script upon receiving out-of-order packets, like these:
#   http://www.blug.linux.no/rfc1149/pinglogg.txt
#
# TODO: Implement "msg repeating", like...
#   ping: sendmsg: Network is unreachable
#   (repeated xx times)
# where xx gets updated for every repeated line
#
# TODO: Implement audible ping.
#
# TODO: Add a command-line parameter for interactive/log mode.
# TODO: Implement interactive/log modes.
#
# TODO: Update the help message.
#
# TODO: Autodetect the width of printf numbers, so they will always line up correctly.
#
# TODO? How will prettyping behave if it receives a duplicate response?

print_help() {
	cat << EOF
Usage: $MYNAME <standard ping parameters>

TODO: Update me

This script will run the standard "ping" program and will substitute each ping
response line by a colored dot.
EOF
}

# Thanks to people at #bash who pointed me at
# http://bash-hackers.org/wiki/doku.php/scripting/posparams
parse_arguments() {
	USE_COLORS=1
	LAST_N=25

	PING_PARAMS=( -n )

	while [[ $# != 0 ]] ; do
		case "$1" in
			-h | -help | --help )
				print_help
				exit
				;;

			# Forbidden ping parameters within prettyping:
			-f )
				echo "$MYNAME: You can't use the -f (flood) option."
				exit 1
				;;
			-R )
				# -R prints extra information at each ping response.
				echo "$MYNAME: You can't use the -R (record route) option."
				exit 1
				;;
			-q )
				echo "$MYNAME: You can't use the -q (quiet) option."
				exit 1
				;;
			-n )
				# -n prevents ping from doing reverse DNS lookup, speeding
				# the startup up a bit. prettyping will always use this option.
				;;
			-v )
				# -n enables verbose output. However, it seems the output with
				# or without this option is the same. Anyway, prettyping will
				# strip this parameter.
				;;
			# Note:
			#  Small values for -s parameter prevents ping from being able to
			#  calculate RTT.

			# New parameters:
			-a )
				# TODO: Implement audible ping for responses or for missing packets
				;;
			--color   ) USE_COLORS=1 ;;
			--nocolor ) USE_COLORS=0 ;;
			--last ) LAST_N="$2" ; shift ;;
			#TODO: Check if this parameter is really a number.

			* )
				PING_PARAMS+=("$1")
				;;
		esac
		shift
	done
}

MYNAME=`basename "$0"`
parse_arguments "$@"


export LC_ALL=C

# Warning! Ugly code ahead!
# The code is so ugly that the comments explaining it are
# bigger than the code itself!
#
# Suppose this:
#
#   cmd_a | cmd_b &
#
# I need the PID of cmd_a. How can I get it?
# In bash, $! will give me the PID of cmd_b.
#
# So, I came up with this ugly solution: open a subshell, like this:
#
# (
# 	cmd_a &
# 	echo "This is the PID I want $!"
# 	wait
# ) | cmd_b


# Ignore Ctrl+C here.
# If I don't do this, this shell script is killed before
# ping and gawk can finish their work.
trap '' 2

# Now the ugly code.
(
	ping "${PING_PARAMS[@]}" &
	# Commented out, because it looks like this line is not needed
	#trap "kill -2 $! ; exit 1" 2  # Catch Ctrl+C here
	wait
) 2>&1 | gawk '
# Weird that awk does not come with abs(), so I need to implement it.
function abs(x)
{
	return ( (x < 0) ? -x : x )
}

# Currently, this function is called once, at the beginning of this
# script, but it is also possible to call this more than once, to
# handle window size changes while this program is running.
#
# Local variables MUST be declared in argument list, else they are
# seen as global. Ugly, but that is how awk works.
function get_terminal_size(SIZE,SIZEA)
{
	if( HAS_STTY )
	{
		if( (STTY_CMD | getline SIZE) == 1 )
		{
			split(SIZE, SIZEA, " ")
			LINES   = SIZEA[1]
			COLUMNS = SIZEA[2]
		}
		else
			HAS_STTY = 0
		close(STTY_CMD)
	}
}

############################################################
# Functions related to cursor handling

# Function called whenever a non-dotted line is printed.
#
# It will move the cursor to the line next to the statistics and
# restore the default color.
function other_line_is_printed()
{
	if( IS_PRINTING_DOTS )
		printf( ESC_DEFAULT ESC_NEXTLINE ESC_NEXTLINE "\n" )
	IS_PRINTING_DOTS = 0
	CURR_COL = 0
}

# Prints the newlines required for the live statistics.
#
# I need to print some newlines and then return the cursor back
# to its position to make sure the terminal will scroll.
function print_newlines_if_needed()
{
	# COLUMNS-1 because I want to avoid bugs with the cursor at the last column
	if( CURR_COL >= COLUMNS-1 )
		CURR_COL = 0
	if( CURR_COL == 0 )
	{
		if( IS_PRINTING_DOTS )
			printf( "\n" )
		#printf( "\n" "\n" ESC_PREVLINE ESC_PREVLINE ESC_ERASELINE )
		printf( "\n" "\n" ESC_CURSORUP ESC_CURSORUP ESC_ERASELINE )
	}
	CURR_COL++
	IS_PRINTING_DOTS = 1
}

############################################################
# Functions related to the data structure of "Last N" statistics.

# Clears the data structure.
function clear(d)
{
	d["index"] = 0  # The next position to store a value
	d["size"]  = 0  # The array size, goes up to LASTNMAX
}

# This function stored the value to the passed data structure.
# The data structure holds at most LAST_N values. When it is full,
# a new value overwrite the oldest one.
function store(d,value)
{
	d[d["index"]] = value
	d["index"]++
	if( d["index"] >= d["size"] )
	{
		if( d["size"] < LAST_N )
			d["size"]++
		else
			d["index"] = 0
	}
}

############################################################
# Functions related to processing the received response

function process_rtt(rtt)
{
	# Overall statistics
	last_rtt = rtt
	total_rtt += rtt
	if( last_seq == 0 )
		min_rtt = max_rtt = rtt
	else
	{
		if( rtt < min_rtt ) min_rtt = rtt
		if( rtt > max_rtt ) max_rtt = rtt
	}

	# "Last N" statistics
	store(lastn_rtt,rtt)
}

############################################################
# Functions related to printing statistics

function print_overall()
{
	printf( "%2d/%3d (%2d%%) lost; %4.0f/" ESC_BOLD "%4.0f" ESC_DEFAULT "/%4.0fms; last: %4.0fms",
		lost,
		lost+received,
		(lost*100/(lost+received)),
		min_rtt,
		(total_rtt/received),
		max_rtt,
		last_rtt )
}

function print_last_n(i, sum, min, avg, max, diffs)
{
	# Calculate and print the lost packets statistics
	sum = 0
	for( i=0 ; i<lastn_lost["size"] ; i++ )
		sum += lastn_lost[i]
	printf( "%2d/%3d (%2d%%) lost; ",
		sum,
		lastn_lost["size"],
		sum*100/lastn_lost["size"] )

	# Calculate the min/avg/max rtt times
	sum = diffs = 0
	min = max = lastn_rtt[0]
	for( i=0 ; i<lastn_rtt["size"] ; i++ )
	{
		sum += lastn_rtt[i]
		if( lastn_rtt[i] < min ) min = lastn_rtt[i]
		if( lastn_rtt[i] > max ) max = lastn_rtt[i]
	}
	avg = sum/lastn_rtt["size"]

	# Calculate mdev (mean absolute deviation)
	for( i=0 ; i<lastn_rtt["size"] ; i++ )
		diffs += abs(lastn_rtt[i] - avg)
	diffs /= lastn_rtt["size"]

	# Print the rtt statistics
	printf( "%4.0f/" ESC_BOLD "%4.0f" ESC_DEFAULT "/%4.0f/%4.0fms (last %d)",
		min,
		avg,
		max,
		diffs,
		lastn_rtt["size"] )
}

############################################################
# Initializations
BEGIN{
	# Easy way to get each value from ping output
	FS = "="

	############################################################
	# General internal variables

	# This is needed to keep track of lost packets
	last_seq = 0

	# Variables to keep the screen clean
	IS_PRINTING_DOTS = 0
	CURR_COL = 0

	############################################################
	# Variables related to "overall" statistics
	received = 0
	lost = 0
	total_rtt = 0
	min_rtt = 0
	max_rtt = 0
	last_rtt = 0

	############################################################
	# Variables related to "last N" statistics
	LAST_N = '$LAST_N'

	# Data structures for the "last N" statistics
	clear(lastn_lost)
	clear(lastn_rtt)

	############################################################
	# Terminal height and width 

	# These are sane defaults, in case we cannot query the actual terminal size
	LINES    = 24
	COLUMNS  = 80

	# Auto-detecting the terminal size
	HAS_STTY = 1
	STTY_CMD = "stty size --file=/dev/tty 2> /dev/null"
	get_terminal_size()
	if( COLUMNS <= 50 )
		print "Warning: terminal width is too small."

	############################################################
	# ANSI escape codes

	# Color escape codes.
	# Fortunately, awk defaults any unassigned variable to an empty string.
	if('$USE_COLORS')
	{
		ESC_DEFAULT = "\033[0m"
		ESC_BOLD    = "\033[1m"
		ESC_GRAY    = "\033[1;30m"
		ESC_RED     = "\033[1;31m"
		ESC_GREEN   = "\033[1;32m"
		ESC_YELLOW  = "\033[1;33m"
		ESC_BLUE    = "\033[1;34m"
		ESC_MAGENTA = "\033[1;35m"
		ESC_CYAN    = "\033[1;36m"
		ESC_WHITE   = "\033[1;37m"
	}
	# Other escape codes, see:
	# http://en.wikipedia.org/wiki/ANSI_escape_code
	# http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
	ESC_NEXTLINE   = "\n"
	ESC_CURSORUP   = "\033[A"
	ESC_SCROLLUP   = "\033[S"
	ESC_SCROLLDOWN = "\033[T"
	ESC_ERASELINE  = "\033[2K"
	ESC_SAVEPOS    = "\0337"
	ESC_UNSAVEPOS  = "\0338"

	# I am avoiding these escapes as they are not listed in:
	# http://vt100.net/docs/vt100-ug/chapter3.html
	#ESC_PREVLINE   = "\033[F"
	#ESC_SAVEPOS    = "\033[s"
	#ESC_UNSAVEPOS  = "\033[u"

	# I am avoiding this to improve compatibility with (older versions of) tmux
	#ESC_NEXTLINE   = "\033[E"
}

############################################################
# Main loop
{
	if( $0 ~ /^[0-9]+ bytes from .*: icmp_[rs]eq=[0-9]+ ttl=[0-9]+ time=[0-9.]+ *ms/ )
	{
		# This must be called before incrementing the last_seq variable!
		process_rtt(int($4))

		seq = int($2)

		while( last_seq < seq-1 )
		{
			# Lost a packet
			print_newlines_if_needed()
			printf( ESC_RED "!" )

			last_seq++
			lost++
			store(lastn_lost,1)
		}

		# Received a packet
		print_newlines_if_needed()
		printf( ESC_GREEN "." )

		last_seq++
		received++
		store(lastn_lost,0)

		printf( ESC_SAVEPOS ESC_DEFAULT )

		printf( ESC_NEXTLINE ESC_ERASELINE )
		print_overall()
		printf( ESC_NEXTLINE ESC_ERASELINE )
		print_last_n()

		printf( ESC_UNSAVEPOS )

		# Not really needed, uncomment if things get buggy
		#fflush()
	}
	else
	{
		other_line_is_printed()
		printf( "%s\n", $0 )
	}
}'
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.