-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path30-zpool-detailed
168 lines (136 loc) · 4.71 KB
/
30-zpool-detailed
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
#!/usr/bin/env bash
# NAME: 30-zpool-detailed
#
# DESCRIPTION: Report the Zpool percent used, capacity and free space. By
# default it will show 2 levels of datasets. This can be
# adjusted with the "zfs_list_DS_depth" variable. Datasets
# smaller than 1% used are not displayed to reduce clutter.
# This can be adjust with "skip_ds_smaller_than".
#
# Originally Based on work from Mike Roach (https://github.com/mroach)
#
# AUTHOR : Richard J. DURSO
# DATE : 01/13/2025
# VERSION : 1.3
##############################################################################
#!/usr/bin/env bash
set -euo pipefail
# How many levels of datasets to report on.
zfs_list_DS_depth=2
# Capture ZFS dataset list by requested depth
readarray -t zfs_list_DS < <(zfs list -Hp -t filesystem,volume -o space -d"${zfs_list_DS_depth}")
# Skip datasets at less than 1% usage to reduce clutter.
skip_ds_smaller_than=1
size_format=iec
bar_width=60
warn_free_pct=20
red="\e[1;31m"
green="\e[1;32m"
dim="\e[2m"
reset="\e[0m"
ds_char="▪"
snap_char="◆"
other_char="■"
free_char="□"
function format_bytes() {
numfmt --to="${size_format}" --format="%.01f" "$1"
}
function pct() {
awk -v n="$1" -v d="$2" 'BEGIN{ print int(n*100.0/d) }'
}
function bar() {
ds_b="$1"
snap_b="$2"
other_b="$3"
ds_width=$(( (ds_b * bar_width) / 100))
snap_width=$(( (snap_b * bar_width) / 100))
other_width=$(( (other_b * bar_width) / 100 ))
rem=$(( bar_width - ds_width - snap_width - other_width ))
pct_free=$(( 100 - ds_width - snap_width - other_width ))
echo -ne "${dim}[${reset}"
if [ "$pct_free" -lt "$warn_free_pct" ]; then
echo -ne "$red"
else
echo -ne "$green"
fi
if [ $ds_width -gt 0 ]; then
printf "${ds_char}%.0s" $(seq 1 $ds_width)
fi
if [ $snap_width -gt 0 ]; then
printf "${snap_char}%.0s" $(seq 1 $snap_width)
fi
if [ $other_width -gt 0 ]; then
printf "${other_char}%.0s" $(seq 1 $other_width)
fi
if [ $rem -gt 0 ]; then
printf "${reset}${dim}${free_char}%.0s" $(seq 1 $rem)
# To pad with spaces, do this instead:
#printf "${dim}%*s" $rem
fi
echo -ne "${reset}${dim}]${reset}"
}
# Filter out datasets with a percent used smaller than "skip_ds_smaller_than" and
# determine the column with needed to show the pool and datasets based on depth
# of datasets to show while not considering datasets filtered out.
function remove_unused_DS() {
local index=0
local length=0
col_width=0
for line in "${zfs_list_DS[@]}"; do
IFS=$'\t' read -r -a props <<< "$line"
name="${props[0]}"
avail="${props[1]}"
used="${props[2]}"
capacity=$(( used + avail ))
used_pct=$(pct "$used" "$capacity")
if [ "$used_pct" -lt "$skip_ds_smaller_than" ]; then
# Small dataset remove from array
unset 'zfs_list_DS[index]'
else
# Get length of this dataset name
length="${#name}"
# See if dataset name is longer then the previous longest name
(( length > col_width )) && col_width=$length
fi
((++index))
done
}
# Filter out small datasets and calculate col_width to use in output
remove_unused_DS
# zpool status
printf "\nzpool status:%s\n" "$(zpool status -x | sed -e 's/^/ /')"
echo
for line in "${zfs_list_DS[@]}"; do
IFS=$'\t' read -r -a props <<< "$line"
name="${props[0]}"
avail="${props[1]}"
used="${props[2]}"
usedsnap="${props[3]}"
usedds="${props[4]}"
used_other=$(( used - usedsnap - usedds ))
capacity=$(( used + avail ))
used_pct=$(pct "$used" "$capacity")
ds_used_pct=$(pct "$usedds" "$capacity")
snap_used_pct=$(pct "$usedsnap" "$capacity")
other_used_pct=$(pct "$used_other" "$capacity")
#echo "$name avail=$avail used=$used usnap=$usedsnap uds=$usedds cap=$capacity dp=$ds_used_pct sp=$snap_used_pct"
# Maybe remove the pool name
local_name="${name#*/}"
if [ "$local_name" = "$name" ]; then
# pool name line
pool_length="${#local_name}"
printf "%-*s" "$((col_width - 2))" "$name"
else
# dataset line
printf "%-*s" "$((col_width - 2))" " $local_name"
fi
bar "$ds_used_pct" "$snap_used_pct" "$other_used_pct"
#printf "Used %6s of %6s (%6s free)\n" "$(format_bytes "$used")" "$(format_bytes "$capacity")" "$(format_bytes "$avail")"
printf "%3s%% used of %6s (%6s free)\n" "$used_pct" "$(format_bytes "$capacity")" "$(format_bytes "$avail")"
done
# Print Character Legend
printf "%-*s" "$((col_width - pool_length +1))" ""
echo -ne " ${dim}[${reset}${green}${ds_char}${reset}${dim}]${reset}=Datasets,"
echo -ne " ${dim}[${reset}${green}${snap_char}${reset}${dim}]${reset}=Snapshots,"
echo -ne " ${dim}[${reset}${green}${other_char}${reset}${dim}]${reset}=Other,"
echo -e " ${dim}[${reset}${free_char}${reset}${dim}]${reset}=Free Space"