1#!/bin/sh
2#
3# Display most relevant iostat bandwidth/latency numbers.  The output is
4# dependent on the name of the script/symlink used to call it.
5#
6
7helpstr="
8iostat:		Show iostat values since boot (summary page).
9iostat-1s:	Do a single 1-second iostat sample and show values.
10iostat-10s:	Do a single 10-second iostat sample and show values."
11
12script="${0##*/}"
13if [ "$1" = "-h" ] ; then
14	echo "$helpstr" | grep "$script:" | tr -s '\t' | cut -f 2-
15	exit
16fi
17
18if [ "$script" = "iostat-1s" ] ; then
19	# Do a single one-second sample
20	interval=1
21	# Don't show summary stats
22	brief="yes"
23elif [ "$script" = "iostat-10s" ] ; then
24	# Do a single ten-second sample
25	interval=10
26	# Don't show summary stats
27	brief="yes"
28fi
29
30# shellcheck disable=SC2154
31if [ -f "$VDEV_UPATH" ] ; then
32	# We're a file-based vdev, iostat doesn't work on us.  Do nothing.
33	exit
34fi
35
36if [ "$(uname)" = "FreeBSD" ]; then
37	out=$(iostat -dKx \
38		${interval:+"-w $interval"} \
39		${interval:+"-c 1"} \
40		"$VDEV_UPATH" | tail -n 2)
41else
42	out=$(iostat -kx \
43		${brief:+"-y"} \
44		${interval:+"$interval"} \
45		${interval:+"1"} \
46		"$VDEV_UPATH" | grep -v '^$' | tail -n 2)
47fi
48
49
50# Sample output (we want the last two lines):
51#
52# Linux 2.6.32-642.13.1.el6.x86_64 (centos68) 	03/09/2017 	_x86_64_	(6 CPU)
53#
54# avg-cpu:  %user   %nice %system %iowait  %steal   %idle
55#           0.00    0.00    0.00    0.00    0.00  100.00
56#
57# Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util
58# sdb               0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00   0.00
59#
60
61# Get the column names
62cols=$(echo "$out" | head -n 1)
63
64# Get the values and tab separate them to make them cut-able.
65vals=$(echo "$out" | tail -n 1 | tr -s '[:space:]' '\t')
66
67i=0
68for col in $cols ; do
69	i=$((i+1))
70	# Skip the first column since it's just the device name
71	if [ "$i" -eq 1 ]; then
72		continue
73	fi
74
75	# Get i'th value
76	val=$(echo "$vals" | cut -f "$i")
77	echo "$col=$val"
78done
79