-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzfsrepquota
executable file
·102 lines (95 loc) · 2.56 KB
/
zfsrepquota
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
#!/usr/bin/env bash
# Print ZFS filesystem user disk quotas in a format similar to Linux "repquota".
# Used disk space is reported in GBytes.
# Note: ZFS has no concept of "soft quota limits".
# The zfs userspace command is only permitted for the root user, therefore
# copy this script to the /usr/local/sbin/ folder.
# CONFIGURE THIS:
# Defile the quota warning limit (percentage)
export warn_limit=80
# Command usage:
function usage()
{
cat <<EOF
Usage: $0 [-u username] [-h] [filesystem]
where:
-u username: Print only user <username>
-h: Print this help information
filesystem: specified filesystem (default: all)
EOF
}
username=""
while getopts "u:h" options; do
case $options in
u ) export username=$OPTARG
;;
h|? ) usage
exit 1;;
esac
done
shift $((OPTIND-1))
if [[ $# -eq 1 ]]
then
# Check the specified ZFS volume argument as $1
if [[ "`/usr/sbin/zfs list | grep $1`" == "" ]]
then
echo Invalid ZFS filesystem name: $1
exit -1
fi
zfsVolumes=$1
else
# All ZFS volumes
zfsVolumes=$(/usr/sbin/zfs list -H -o name)
fi
for fs in $zfsVolumes
do
/usr/sbin/zfs userspace $fs -pH -o name,quota,used,objquota,objused | awk -vlimit=$warn_limit -vfs=$fs -vusername=$username '
BEGIN {
# Table header
fsheader="Filesystem"
fslen=length(fsheader)
if (length(fs) > fslen) fslen=length(fs)
for (i=0; i<fslen; i++) fsline=fsline "-" # Line below "Filesystem"
header1=sprintf("%*-s %*-s -- Space Quota Pct. Files Quota Pct.", 8 , "User", fslen, fsheader)
header2=sprintf("%*s %*s -- ----- ----- ---- ----- ----- ----", 8 , "--------", fslen, fsline)
# Print global header only if username was not specified
if (length(username)==0) {
print header1
print header2
}
GB=1024*1024*1024
}
{ name=$1
# Check if username was specified and matches
if (length(username)>0 && name != username) next
quota=$2
used=$3
usedpercent=0
overused="-"
filesquota=$4
filesused=$5
filepercent=0
overfiles="-"
# Print percentages after the quotas
usedpercent= 0
if (quota>0) {
usedpercent= (used*100) / quota
if (usedpercent>limit) overused="+"
}
filepercent= 0
if (filesquota>0) {
filepercent= (filesused*100) / filesquota
if (filepercent>limit) overfiles="+"
}
if (used>0 || filesused>0) {
# Print a header if username was specified
if (length(username)>0) {
print header1
print header2
}
printf("%-8s %*-s %1s%1s %9.1fG %9.0fG %6.1f %9d %9d %6.1f\n",
name, fslen, fs, overused, overfiles, used/GB, quota/GB, usedpercent,
filesused, filesquota, filepercent)
}
}'
done